Skip to content

Commit

Permalink
test(sbb-header): add visual tests (#2820)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideMininni-Fincons authored Jun 27, 2024
1 parent 3eaee9c commit aa07987
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["sbb-header-button renders the component as a button with icon Light DOM"] =
snapshots["sbb-header-button renders DOM"] =
`<sbb-header-button
aria-label="a11y label"
data-action=""
Expand All @@ -18,9 +18,9 @@ snapshots["sbb-header-button renders the component as a button with icon Light D
Action
</sbb-header-button>
`;
/* end snapshot sbb-header-button renders the component as a button with icon Light DOM */
/* end snapshot sbb-header-button renders DOM */

snapshots["sbb-header-button renders the component as a button with icon Shadow DOM"] =
snapshots["sbb-header-button renders Shadow DOM"] =
`<span class="sbb-action-base sbb-header-button">
<span class="sbb-header-action__wrapper">
<span class="sbb-header-action__icon">
Expand All @@ -41,9 +41,9 @@ snapshots["sbb-header-button renders the component as a button with icon Shadow
</span>
</span>
`;
/* end snapshot sbb-header-button renders the component as a button with icon Shadow DOM */
/* end snapshot sbb-header-button renders Shadow DOM */

snapshots["sbb-header-button renders the component as a button with icon A11y tree Chrome"] =
snapshots["sbb-header-button renders A11y tree Chrome"] =
`<p>
{
"role": "WebArea",
Expand All @@ -57,9 +57,9 @@ snapshots["sbb-header-button renders the component as a button with icon A11y tr
}
</p>
`;
/* end snapshot sbb-header-button renders the component as a button with icon A11y tree Chrome */
/* end snapshot sbb-header-button renders A11y tree Chrome */

snapshots["sbb-header-button renders the component as a button with icon A11y tree Firefox"] =
snapshots["sbb-header-button renders A11y tree Firefox"] =
`<p>
{
"role": "document",
Expand All @@ -73,5 +73,5 @@ snapshots["sbb-header-button renders the component as a button with icon A11y tr
}
</p>
`;
/* end snapshot sbb-header-button renders the component as a button with icon A11y tree Firefox */
/* end snapshot sbb-header-button renders A11y tree Firefox */

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SbbHeaderButtonElement } from './header-button.js';
import './header-button.js';

describe(`sbb-header-button`, () => {
describe('renders the component as a button with icon', () => {
describe('renders', () => {
let element: SbbHeaderButtonElement;

beforeEach(async () => {
Expand All @@ -25,7 +25,7 @@ describe(`sbb-header-button`, () => {
`);
});

it('Light DOM', async () => {
it('DOM', async () => {
await expect(element).dom.to.be.equalSnapshot();
});

Expand Down
3 changes: 1 addition & 2 deletions src/elements/header/header-button/header-button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { withActions } from '@storybook/addon-actions/decorator';
import type { InputType } from '@storybook/types';
import type { Meta, StoryObj, ArgTypes, Args, Decorator } from '@storybook/web-components';
import { html, type TemplateResult } from 'lit';
import { styleMap } from 'lit/directives/style-map.js';

import { sbbSpread } from '../../../storybook/helpers/spread.js';

Expand All @@ -14,7 +13,7 @@ const TemplateSingle = (args: Args): TemplateResult => html`
`;

const TemplateMultiple = (args: Args): TemplateResult => html`
<div style=${styleMap({ display: 'flex', gap: '2rem' })}>
<div style="display: flex; gap: 2rem;">
<sbb-header-button ${sbbSpread(args)}>${args.text} 1</sbb-header-button>
<sbb-header-button ${sbbSpread(args)}>${args.text} 2</sbb-header-button>
<sbb-header-button ${sbbSpread(args)}>${args.text} 3</sbb-header-button>
Expand Down
20 changes: 20 additions & 0 deletions src/elements/header/header-button/header-button.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { html } from 'lit';

import { describeViewports, visualDiffStandardStates } from '../../core/testing/private.js';

import './header-button.js';

describe(`sbb-header-button`, () => {
describeViewports({ viewports: ['zero', 'medium'] }, () => {
for (const state of visualDiffStandardStates) {
it(
state.name,
state.with(async (setup) => {
await setup.withFixture(html`
<sbb-header-button icon-name="hamburger-menu-small">Menu</sbb-header-button>
`);
}),
);
}
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["sbb-header-link renders the component as a button with icon Light DOM"] =
snapshots["sbb-header-link renders DOM"] =
`<sbb-header-link
accessibility-label="a11y label"
data-action=""
Expand All @@ -15,9 +15,9 @@ snapshots["sbb-header-link renders the component as a button with icon Light DOM
Action
</sbb-header-link>
`;
/* end snapshot sbb-header-link renders the component as a button with icon Light DOM */
/* end snapshot sbb-header-link renders DOM */

snapshots["sbb-header-link renders the component as a button with icon Shadow DOM"] =
snapshots["sbb-header-link renders Shadow DOM"] =
`<a
aria-label="a11y label"
class="sbb-action-base sbb-header-link"
Expand Down Expand Up @@ -47,9 +47,9 @@ snapshots["sbb-header-link renders the component as a button with icon Shadow DO
</sbb-screen-reader-only>
</a>
`;
/* end snapshot sbb-header-link renders the component as a button with icon Shadow DOM */
/* end snapshot sbb-header-link renders Shadow DOM */

snapshots["sbb-header-link renders the component as a button with icon A11y tree Chrome"] =
snapshots["sbb-header-link renders A11y tree Chrome"] =
`<p>
{
"role": "WebArea",
Expand All @@ -63,9 +63,9 @@ snapshots["sbb-header-link renders the component as a button with icon A11y tree
}
</p>
`;
/* end snapshot sbb-header-link renders the component as a button with icon A11y tree Chrome */
/* end snapshot sbb-header-link renders A11y tree Chrome */

snapshots["sbb-header-link renders the component as a button with icon A11y tree Firefox"] =
snapshots["sbb-header-link renders A11y tree Firefox"] =
`<p>
{
"role": "document",
Expand All @@ -80,5 +80,5 @@ snapshots["sbb-header-link renders the component as a button with icon A11y tree
}
</p>
`;
/* end snapshot sbb-header-link renders the component as a button with icon A11y tree Firefox */
/* end snapshot sbb-header-link renders A11y tree Firefox */

4 changes: 2 additions & 2 deletions src/elements/header/header-link/header-link.snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SbbHeaderLinkElement } from './header-link.js';
import './header-link.js';

describe(`sbb-header-link`, () => {
describe('renders the component as a button with icon', () => {
describe('renders', () => {
let element: SbbHeaderLinkElement;

beforeEach(async () => {
Expand All @@ -23,7 +23,7 @@ describe(`sbb-header-link`, () => {
);
});

it('Light DOM', async () => {
it('DOM', async () => {
await expect(element).dom.to.be.equalSnapshot();
});

Expand Down
3 changes: 1 addition & 2 deletions src/elements/header/header-link/header-link.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { InputType } from '@storybook/types';
import type { Meta, StoryObj, ArgTypes, Args, Decorator } from '@storybook/web-components';
import type { TemplateResult } from 'lit';
import { html } from 'lit';
import { styleMap } from 'lit/directives/style-map.js';

import { sbbSpread } from '../../../storybook/helpers/spread.js';

Expand All @@ -15,7 +14,7 @@ const TemplateSingle = (args: Args): TemplateResult => html`
`;

const TemplateMultiple = (args: Args): TemplateResult => html`
<div style=${styleMap({ display: 'flex', gap: '2rem' })}>
<div style="display: flex; gap: 2rem;">
<sbb-header-link ${sbbSpread(args)}>${args.text} 1</sbb-header-link>
<sbb-header-link ${sbbSpread(args)}>${args.text} 2</sbb-header-link>
<sbb-header-link ${sbbSpread(args)}>${args.text} 3</sbb-header-link>
Expand Down
20 changes: 20 additions & 0 deletions src/elements/header/header-link/header-link.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { html } from 'lit';

import { describeViewports, visualDiffStandardStates } from '../../core/testing/private.js';

import './header-link.js';

describe(`sbb-header-link`, () => {
describeViewports({ viewports: ['zero', 'medium'] }, () => {
for (const state of visualDiffStandardStates) {
it(
state.name,
state.with(async (setup) => {
await setup.withFixture(html`
<sbb-header-link icon-name="hamburger-menu-small" href="#">Menu</sbb-header-link>
`);
}),
);
}
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["sbb-header renders"] =
snapshots["sbb-header renders DOM"] =
`<sbb-header>
</sbb-header>
`;
/* end snapshot sbb-header renders DOM */

snapshots["sbb-header renders Shadow DOM"] =
`<header class="sbb-header">
<div class="sbb-header__wrapper">
<slot>
Expand All @@ -15,7 +21,7 @@ snapshots["sbb-header renders"] =
</div>
</header>
`;
/* end snapshot sbb-header renders */
/* end snapshot sbb-header renders Shadow DOM */

snapshots["sbb-header renders actions and logo DOM"] =
`<sbb-header>
Expand Down
17 changes: 13 additions & 4 deletions src/elements/header/header/header.snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import './header.js';
import '../header-link.js';

describe(`sbb-header`, () => {
it('renders', async () => {
const root = await fixture(html`<sbb-header></sbb-header>`);
describe('renders', () => {
let element: SbbHeaderElement;

beforeEach(async () => {
element = await fixture(html`<sbb-header></sbb-header>`);
});

expect(root).dom.to.be.equal(`<sbb-header></sbb-header>`);
await expect(root).shadowDom.to.be.equalSnapshot();
it('DOM', async () => {
await expect(element).dom.to.be.equalSnapshot();
});

it('Shadow DOM', async () => {
await expect(element).shadowDom.to.be.equalSnapshot();
});
});

describe('renders actions and logo', () => {
Expand Down
66 changes: 66 additions & 0 deletions src/elements/header/header/header.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { html, type TemplateResult } from 'lit';

import { describeViewports, visualDiffDefault } from '../../core/testing/private.js';

import './header.js';
import '../header-link.js';
import '../header-button.js';
import '../../menu.js';

describe(`sbb-header`, () => {
const loremIpsumTemplate = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet malesuada augue. Morbi
eget tristique nisl, sit amet dapibus erat. Donec tempor, metus et aliquam ultrices, nulla mi
mollis urna, a lacinia mauris risus mattis massa. Quisque cursus sollicitudin enim in
malesuada. Maecenas nec hendrerit augue. Duis porttitor mattis molestie. Sed imperdiet velit
at dui ultrices, viverra scelerisque nisi dapibus. Nulla urna lectus, gravida eu dapibus vel,
mattis non turpis. Nunc interdum et justo sed faucibus. Vestibulum interdum commodo mi, sed
eleifend odio posuere in. Nunc non dui venenatis, eleifend est ut, varius odio. Quisque augue
ante, mollis eu lorem id, commodo cursus risus.
`;

const template = ({ expanded }: { expanded?: boolean }): TemplateResult => html`
<style>
${' .last-element {display: none;} '}
${' @media screen and (width >= 840px) { .last-element { display: block; } }'}
${' @media screen and (width < 1023px) { .sbb-header-spacer { display: none; } }'}
</style>
<sbb-header ?expanded=${expanded}>
<sbb-header-button icon-name="hamburger-menu-small" expand-from="small">
Menu
</sbb-header-button>
<div class="sbb-header-spacer"></div>
<sbb-header-link href="https://www.sbb.ch" target="_blank" icon-name="magnifying-glass-small"
>Search
</sbb-header-link>
<sbb-header-button icon-name="user-small" class="sbb-header-shrinkable">
Christina Müller
</sbb-header-button>
<sbb-header-button icon-name="globe-small" class="last-element"> English </sbb-header-button>
</sbb-header>
<div class=${expanded ? 'sbb-page-spacing-expanded' : 'sbb-page-spacing'}>
${loremIpsumTemplate}
</div>
`;

describeViewports({ viewports: ['zero', 'ultra'], viewportHeight: 300 }, () => {
for (const expanded of [true, false]) {
it(
`expanded=${expanded}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ expanded }), { padding: '0' });
}),
);
}

it(
`scroll`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({}), { padding: '0' });

// Scroll page down
window.scrollTo(0, document.body.scrollHeight);
}),
);
});
});

0 comments on commit aa07987

Please sign in to comment.