From d7397ef7f77da79e56a6cd13efb16320e8ee68ab Mon Sep 17 00:00:00 2001 From: Davide Mininni <101575400+DavideMininni-Fincons@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:03:48 +0200 Subject: [PATCH] test(sbb-checkbox): add visual tests (#2859) --- .../checkbox-group.visual.spec.ts | 180 ++++++++++++++++++ .../checkbox-panel.snapshot.spec.snap.js | 56 +++--- .../checkbox-panel.snapshot.spec.ts | 8 +- .../checkbox-panel.visual.spec.ts | 97 ++++++++++ .../checkbox/checkbox/checkbox.visual.spec.ts | 76 ++++++++ 5 files changed, 385 insertions(+), 32 deletions(-) create mode 100644 src/elements/checkbox/checkbox-group/checkbox-group.visual.spec.ts create mode 100644 src/elements/checkbox/checkbox-panel/checkbox-panel.visual.spec.ts create mode 100644 src/elements/checkbox/checkbox/checkbox.visual.spec.ts diff --git a/src/elements/checkbox/checkbox-group/checkbox-group.visual.spec.ts b/src/elements/checkbox/checkbox-group/checkbox-group.visual.spec.ts new file mode 100644 index 0000000000..9cb13558a1 --- /dev/null +++ b/src/elements/checkbox/checkbox-group/checkbox-group.visual.spec.ts @@ -0,0 +1,180 @@ +import { html, nothing, type TemplateResult } from 'lit'; +import { repeat } from 'lit/directives/repeat.js'; + +import { + describeViewports, + visualDiffDefault, + visualDiffFocus, +} from '../../core/testing/private.js'; + +import '../../card.js'; +import '../../form-error.js'; +import '../../icon.js'; +import '../checkbox.js'; +import '../checkbox-panel.js'; +import './checkbox-group.js'; + +describe('sbb-checkbox-group', () => { + const longLabelText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer enim elit, ultricies in tincidunt + quis, mattis eu quam. Nulla sit amet lorem fermentum, molestie nunc ut, hendrerit risus. Vestibulum rutrum elit et + lacus sollicitudin, quis malesuada lorem vehicula. Suspendisse at augue quis tellus vulputate tempor. Vivamus urna + velit, varius nec est ac, mollis efficitur lorem. Quisque non nisl eget massa interdum tempus. Praesent vel feugiat + metus.`; + + const defaultArgs = { + orientation: 'horizontal', + disabled: false, + required: false, + horizontalFrom: undefined as string | undefined, + size: 'm', + label: 'Label', + iconName: undefined as string | undefined, + iconPlacement: undefined as string | undefined, + }; + + const checkboxesTemplate = ({ + orientation, + disabled, + required, + horizontalFrom, + size, + label, + iconName, + iconPlacement, + }: typeof defaultArgs): TemplateResult => html` + + ${repeat( + new Array(3), + (_, index) => html` + + ${label} ${index} + + `, + )} + ${required + ? html`This is a required field.` + : nothing} + + `; + + const panelsTemplate = ({ + orientation, + disabled, + horizontalFrom, + size, + }: typeof defaultArgs): TemplateResult => html` + + ${repeat( + new Array(2), + (_, index) => html` + + Label ${index} + Subtext + + + + CHF 40.00 + + + % + + `, + )} + + `; + + describeViewports({ viewports: ['small', 'medium'] }, () => { + for (const orientation of ['horizontal', 'vertical']) { + const args = { ...defaultArgs, orientation }; + for (const visualDiffState of [visualDiffDefault, visualDiffFocus]) { + it( + `${orientation} ${visualDiffState.name}`, + visualDiffState.with(async (setup) => { + await setup.withFixture(checkboxesTemplate(args)); + }), + ); + } + + it( + `${orientation} size=s ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(checkboxesTemplate({ ...args, size: 's' })); + }), + ); + + it( + `${orientation} disabled ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(checkboxesTemplate({ ...args, disabled: true })); + }), + ); + + it( + `${orientation} required ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(checkboxesTemplate({ ...args, required: true })); + }), + ); + + it( + `${orientation} label=long ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(checkboxesTemplate({ ...args, label: longLabelText })); + }), + ); + + for (const iconPlacement of ['start', 'end']) { + it( + `${orientation} iconPlacement=${iconPlacement} ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture( + checkboxesTemplate({ ...args, iconName: 'tickets-class-small', iconPlacement }), + ); + }), + ); + } + + it( + `${orientation} template=panel ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(panelsTemplate(args)); + }), + ); + } + + describe('horizontalFrom=medium', () => { + const args = { ...defaultArgs, orientation: 'vertical', horizontalFrom: 'medium' }; + it( + `checkbox ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(checkboxesTemplate(args)); + }), + ); + + it( + `panel ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(panelsTemplate(args)); + }), + ); + }); + }); +}); diff --git a/src/elements/checkbox/checkbox-panel/__snapshots__/checkbox-panel.snapshot.spec.snap.js b/src/elements/checkbox/checkbox-panel/__snapshots__/checkbox-panel.snapshot.spec.snap.js index 4935c50264..c82c86ab1f 100644 --- a/src/elements/checkbox/checkbox-panel/__snapshots__/checkbox-panel.snapshot.spec.snap.js +++ b/src/elements/checkbox/checkbox-panel/__snapshots__/checkbox-panel.snapshot.spec.snap.js @@ -1,7 +1,7 @@ /* @web/test-runner snapshot v1 */ export const snapshots = {}; -snapshots["sbb-checkbox-panel should render unchecked DOM"] = +snapshots["sbb-checkbox-panel renders unchecked DOM"] = ` `; -/* end snapshot sbb-checkbox-panel should render unchecked DOM */ +/* end snapshot sbb-checkbox-panel renders unchecked DOM */ -snapshots["sbb-checkbox-panel should render unchecked Shadow DOM"] = +snapshots["sbb-checkbox-panel renders unchecked Shadow DOM"] = `
@@ -44,9 +44,9 @@ snapshots["sbb-checkbox-panel should render unchecked Shadow DOM"] = `; -/* end snapshot sbb-checkbox-panel should render unchecked Shadow DOM */ +/* end snapshot sbb-checkbox-panel renders unchecked Shadow DOM */ -snapshots["sbb-checkbox-panel should render checked DOM"] = +snapshots["sbb-checkbox-panel renders checked DOM"] = ` `; -/* end snapshot sbb-checkbox-panel should render checked DOM */ +/* end snapshot sbb-checkbox-panel renders checked DOM */ -snapshots["sbb-checkbox-panel should render checked Shadow DOM"] = +snapshots["sbb-checkbox-panel renders checked Shadow DOM"] = `
@@ -91,9 +91,9 @@ snapshots["sbb-checkbox-panel should render checked Shadow DOM"] = `; -/* end snapshot sbb-checkbox-panel should render checked Shadow DOM */ +/* end snapshot sbb-checkbox-panel renders checked Shadow DOM */ -snapshots["sbb-checkbox-panel should render indeterminate DOM"] = +snapshots["sbb-checkbox-panel renders indeterminate DOM"] = ` `; -/* end snapshot sbb-checkbox-panel should render indeterminate DOM */ +/* end snapshot sbb-checkbox-panel renders indeterminate DOM */ -snapshots["sbb-checkbox-panel should render indeterminate Shadow DOM"] = +snapshots["sbb-checkbox-panel renders indeterminate Shadow DOM"] = `
@@ -137,9 +137,9 @@ snapshots["sbb-checkbox-panel should render indeterminate Shadow DOM"] = `; -/* end snapshot sbb-checkbox-panel should render indeterminate Shadow DOM */ +/* end snapshot sbb-checkbox-panel renders indeterminate Shadow DOM */ -snapshots["sbb-checkbox-panel should render unchecked disabled DOM"] = +snapshots["sbb-checkbox-panel renders unchecked disabled DOM"] = ` `; -/* end snapshot sbb-checkbox-panel should render unchecked disabled DOM */ +/* end snapshot sbb-checkbox-panel renders unchecked disabled DOM */ -snapshots["sbb-checkbox-panel should render unchecked disabled Shadow DOM"] = +snapshots["sbb-checkbox-panel renders unchecked disabled Shadow DOM"] = `
@@ -183,25 +183,26 @@ snapshots["sbb-checkbox-panel should render unchecked disabled Shadow DOM"] = `; -/* end snapshot sbb-checkbox-panel should render unchecked disabled Shadow DOM */ +/* end snapshot sbb-checkbox-panel renders unchecked disabled Shadow DOM */ -snapshots["sbb-checkbox-panel Unchecked - A11y tree Firefox"] = +snapshots["sbb-checkbox-panel Unchecked - A11y tree Chrome"] = `

{ - "role": "document", + "role": "WebArea", "name": "", "children": [ { "role": "checkbox", - "name": "​ Label" + "name": "​ Label", + "checked": false } ] }

`; -/* end snapshot sbb-checkbox-panel Unchecked - A11y tree Firefox */ +/* end snapshot sbb-checkbox-panel Unchecked - A11y tree Chrome */ -snapshots["sbb-checkbox-panel Unchecked - A11y tree Chrome"] = +snapshots["sbb-checkbox-panel Checked - A11y tree Chrome"] = `

{ "role": "WebArea", @@ -210,30 +211,29 @@ snapshots["sbb-checkbox-panel Unchecked - A11y tree Chrome"] = { "role": "checkbox", "name": "​ Label", - "checked": false + "checked": true } ] }

`; -/* end snapshot sbb-checkbox-panel Unchecked - A11y tree Chrome */ +/* end snapshot sbb-checkbox-panel Checked - A11y tree Chrome */ -snapshots["sbb-checkbox-panel Checked - A11y tree Chrome"] = +snapshots["sbb-checkbox-panel Unchecked - A11y tree Firefox"] = `

{ - "role": "WebArea", + "role": "document", "name": "", "children": [ { "role": "checkbox", - "name": "​ Label", - "checked": true + "name": "​ Label" } ] }

`; -/* end snapshot sbb-checkbox-panel Checked - A11y tree Chrome */ +/* end snapshot sbb-checkbox-panel Unchecked - A11y tree Firefox */ snapshots["sbb-checkbox-panel Checked - A11y tree Firefox"] = `

diff --git a/src/elements/checkbox/checkbox-panel/checkbox-panel.snapshot.spec.ts b/src/elements/checkbox/checkbox-panel/checkbox-panel.snapshot.spec.ts index 6d729e4e55..63ca157f85 100644 --- a/src/elements/checkbox/checkbox-panel/checkbox-panel.snapshot.spec.ts +++ b/src/elements/checkbox/checkbox-panel/checkbox-panel.snapshot.spec.ts @@ -8,7 +8,7 @@ import { SbbCheckboxPanelElement } from './checkbox-panel.js'; describe('sbb-checkbox-panel', () => { let element: SbbCheckboxPanelElement; - describe('should render unchecked', async () => { + describe('renders unchecked', async () => { beforeEach(async () => { element = (await fixture( html` { }); }); - describe('should render checked', async () => { + describe('renders checked', async () => { beforeEach(async () => { element = (await fixture( html` { }); }); - describe('should render indeterminate', async () => { + describe('renders indeterminate', async () => { beforeEach(async () => { element = (await fixture( html` { }); }); - describe('should render unchecked disabled', async () => { + describe('renders unchecked disabled', async () => { beforeEach(async () => { element = (await fixture( html` { + const defaultArgs = { + state: 'unchecked', + disabled: false, + color: 'white', + borderless: false, + size: 'm', + }; + + const template = ({ + state, + disabled, + color, + borderless, + size, + }: typeof defaultArgs): TemplateResult => + html` + Label ${size} + Subtext + + + + + CHF 40.00 + + + + % + `; + + describeViewports({ viewports: ['zero', 'medium'] }, () => { + for (const state of ['checked', 'unchecked', 'indeterminate']) { + const args = { ...defaultArgs, state }; + for (const visualDiffState of [visualDiffDefault, visualDiffFocus]) { + it( + `state=${state} ${visualDiffState.name}`, + visualDiffState.with(async (setup) => { + await setup.withFixture(template(args)); + }), + ); + } + + it( + `state=${state} disabled ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...args, disabled: true })); + }), + ); + } + + it( + `color=milk ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...defaultArgs, color: 'milk' })); + }), + ); + + it( + `size=s ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...defaultArgs, size: 's' })); + }), + ); + + it( + `borderless ${visualDiffDefault.name}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...defaultArgs, borderless: true })); + }), + ); + }); +}); diff --git a/src/elements/checkbox/checkbox/checkbox.visual.spec.ts b/src/elements/checkbox/checkbox/checkbox.visual.spec.ts new file mode 100644 index 0000000000..d9cecff44f --- /dev/null +++ b/src/elements/checkbox/checkbox/checkbox.visual.spec.ts @@ -0,0 +1,76 @@ +import { html, nothing, type TemplateResult } from 'lit'; + +import { + describeViewports, + visualDiffDefault, + visualDiffFocus, +} from '../../core/testing/private.js'; + +import './checkbox.js'; + +describe('sbb-checkbox', () => { + const defaultArgs = { + size: 'm', + disabled: false, + iconName: undefined as string | undefined, + iconPlacement: undefined as string | undefined, + }; + + describeViewports({ viewports: ['zero', 'medium'] }, () => { + for (const state of ['checked', 'unchecked', 'indeterminate']) { + const template = ({ + size, + disabled, + iconName, + iconPlacement, + }: typeof defaultArgs): TemplateResult => html` + Label + `; + + for (const forcedColors of [false, true]) { + for (const visualDiffState of [visualDiffDefault, visualDiffFocus]) { + it( + `${state} forcedColors=${forcedColors} ${visualDiffState.name}`, + visualDiffState.with(async (setup) => { + await setup.withFixture(template(defaultArgs), { forcedColors }); + }), + ); + + it( + `${state} forcedColors=${forcedColors} disabled`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...defaultArgs, disabled: true }), { + forcedColors, + }); + }), + ); + } + } + + it( + `${state} size=s`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(template({ ...defaultArgs, size: 's' })); + }), + ); + + for (const iconPlacement of ['start', 'end']) { + it( + `${state} iconPlacement=${iconPlacement}`, + visualDiffDefault.with(async (setup) => { + const args = { ...defaultArgs, iconName: 'tickets-class-small', iconPlacement }; + await setup.withFixture(template(args)); + }), + ); + } + } + }); +});