Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(sbb-file-selector): add visual tests #2812

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* @web/test-runner snapshot v1 */
export const snapshots = {};

snapshots["sbb-file-selector renders default"] =
snapshots["sbb-file-selector renders DOM"] =
`<sbb-file-selector size="m">
</sbb-file-selector>
`;
/* end snapshot sbb-file-selector renders DOM */

snapshots["sbb-file-selector renders Shadow DOM"] =
`<div class="sbb-file-selector">
<div class="sbb-file-selector__input-container">
<label>
Expand Down Expand Up @@ -32,9 +38,18 @@ snapshots["sbb-file-selector renders default"] =
</div>
</div>
`;
/* end snapshot sbb-file-selector renders default */
/* end snapshot sbb-file-selector renders Shadow DOM */

snapshots["sbb-file-selector renders with dropzone area and size s"] =
snapshots["sbb-file-selector renders with dropzone area and size s DOM"] =
`<sbb-file-selector
size="s"
variant="dropzone"
>
</sbb-file-selector>
`;
/* end snapshot sbb-file-selector renders with dropzone area and size s DOM */

snapshots["sbb-file-selector renders with dropzone area and size s Shadow DOM"] =
`<div class="sbb-file-selector">
<div class="sbb-file-selector__input-container">
<label>
Expand Down Expand Up @@ -82,46 +97,54 @@ snapshots["sbb-file-selector renders with dropzone area and size s"] =
</div>
</div>
`;
/* end snapshot sbb-file-selector renders with dropzone area and size s */
/* end snapshot sbb-file-selector renders with dropzone area and size s Shadow DOM */

snapshots["sbb-file-selector A11y tree Chrome"] =
snapshots["sbb-file-selector renders with dropzone area and size s A11y tree Chrome"] =
`<p>
{
"role": "WebArea",
"name": "",
"children": [
{
"role": "text",
"name": "Drag & Drop your files here"
},
{
"role": "text",
"name": "Choose a file"
},
{
"role": "button",
"name": "Choose a file",
"name": "Drag & Drop your files here Choose a file",
"value": "No file chosen"
}
]
}
</p>
`;
/* end snapshot sbb-file-selector A11y tree Chrome */
/* end snapshot sbb-file-selector renders with dropzone area and size s A11y tree Chrome */

snapshots["sbb-file-selector A11y tree Firefox"] =
snapshots["sbb-file-selector renders with dropzone area and size s A11y tree Firefox"] =
`<p>
{
"role": "document",
"name": "",
"children": [
{
"role": "text leaf",
"name": "Drag & Drop your files here"
},
{
"role": "text leaf",
"name": "Choose a file"
},
{
"role": "button",
"name": "Choose a file Browse… …"
"name": "Drag & Drop your files here Choose a file Browse… …"
}
]
}
</p>
`;
/* end snapshot sbb-file-selector A11y tree Firefox */
/* end snapshot sbb-file-selector renders with dropzone area and size s A11y tree Firefox */

54 changes: 32 additions & 22 deletions src/elements/file-selector/file-selector.snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@ import { expect } from '@open-wc/testing';
import { html } from 'lit/static-html.js';

import { fixture, testA11yTreeSnapshot } from '../core/testing/private.js';
import { waitForLitRender } from '../core/testing.js';

import type { SbbFileSelectorElement } from './file-selector.js';
import './file-selector.js';

describe(`sbb-file-selector`, () => {
it('renders default', async () => {
const root = await fixture(html`<sbb-file-selector></sbb-file-selector>`);

expect(root).dom.to.be.equal(`
<sbb-file-selector size='m'>
</sbb-file-selector>
`);
await expect(root).shadowDom.to.be.equalSnapshot();
});
describe('renders', () => {
let element: SbbFileSelectorElement;

it('renders with dropzone area and size s', async () => {
const root = await fixture(
html`<sbb-file-selector variant="dropzone" size="s"></sbb-file-selector>`,
);
beforeEach(async () => {
element = await fixture(html`<sbb-file-selector></sbb-file-selector>`);
});

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

expect(root).dom.to.be.equal(`
<sbb-file-selector variant='dropzone' size='s'>
</sbb-file-selector>
`);
await expect(root).shadowDom.to.be.equalSnapshot();
it('Shadow DOM', async () => {
await expect(element).shadowDom.to.be.equalSnapshot();
});
});

// We skip safari because it has an inconsistent behavior on ci environment
testA11yTreeSnapshot(html`<sbb-file-selector></sbb-file-selector>`, undefined, { safari: true });
describe('renders with dropzone area and size s', () => {
let element: SbbFileSelectorElement;

beforeEach(async () => {
element = await fixture(
html`<sbb-file-selector variant="dropzone" size="s"></sbb-file-selector>`,
);
});

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

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

// We skip safari because it has an inconsistent behavior on ci environment
testA11yTreeSnapshot(undefined, undefined, { safari: true });
});
});
96 changes: 96 additions & 0 deletions src/elements/file-selector/file-selector.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { html, nothing } from 'lit';

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

import '../form-error.js';
import './file-selector.js';
import type { SbbFileSelectorElement } from '@sbb-esta/lyne-elements/file-selector/file-selector';
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved

describe(`sbb-file-selector`, () => {
function addFilesToComponentInput(elem: SbbFileSelectorElement): void {
const dataTransfer: DataTransfer = new DataTransfer();
for (let i: number = 0; i < 5; i++) {
dataTransfer.items.add(
new File([`Hello world - ${i}`], `hello${i}.txt`, {
type: 'text/plain',
lastModified: new Date(i).getMilliseconds(),
}),
);
}
const input: HTMLInputElement = elem.shadowRoot!.querySelector<HTMLInputElement>('input')!;
input.files = dataTransfer.files;
input.dispatchEvent(new Event('change'));
}

let root: HTMLElement;

const states = {
variant: ['default', 'dropzone'],
state: [
{ disabled: false, error: false },
{ disabled: true, error: false },
{ disabled: false, error: true },
],
};

const sizes = {
variant: ['default', 'dropzone'],
size: ['s', 'm'],
};

describeViewports({ viewports: ['small', 'medium'] }, () => {
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
describeEach(states, ({ variant, state }) => {
beforeEach(async function () {
root = await visualRegressionFixture(html`
<sbb-file-selector
id="fs"
title-content="Title"
?multiple=${true}
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
variant=${variant}
?disabled=${state.disabled}
></sbb-file-selector>
${state.error
? html`<sbb-form-error slot="error">There has been an error.</sbb-form-error>`
: nothing}
`);
});

it(
visualDiffDefault.name,
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
visualDiffDefault.with((setup) => {
if (!state.disabled && state.error) {
addFilesToComponentInput(root.querySelector('#fs')!);
}
setup.withSnapshotElement(root);
}),
);
});

describeEach(sizes, ({ variant, size }) => {
beforeEach(async function () {
root = await visualRegressionFixture(html`
<sbb-file-selector
id="fs"
title-content="Title"
?multiple=${true}
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
variant=${variant}
size=${size}
></sbb-file-selector>
`);
});

it(
visualDiffDefault.name,
visualDiffDefault.with((setup) => {
addFilesToComponentInput(root.querySelector('#fs')!);
setup.withSnapshotElement(root);
}),
);
});
});
});
Loading