From a1a0dd63295186dae304bd91a38ecacda4d43be9 Mon Sep 17 00:00:00 2001 From: Tommmaso Menga Date: Mon, 24 Jun 2024 16:36:45 +0200 Subject: [PATCH] test(sbb-dialog): add visual spec (#2813) --- .../dialog-actions.snapshot.spec.snap.js | 42 +++++++ .../dialog-actions.snapshot.spec.ts | 29 +++-- .../dialog-content.snapshot.spec.snap.js | 50 ++++++++ .../dialog-content.snapshot.spec.ts | 28 +++-- src/elements/dialog/dialog/dialog.spec.ts | 1 + .../dialog/dialog/dialog.visual.spec.ts | 113 ++++++++++++++++++ 6 files changed, 241 insertions(+), 22 deletions(-) create mode 100644 src/elements/dialog/dialog-content/__snapshots__/dialog-content.snapshot.spec.snap.js create mode 100644 src/elements/dialog/dialog/dialog.visual.spec.ts diff --git a/src/elements/dialog/dialog-actions/__snapshots__/dialog-actions.snapshot.spec.snap.js b/src/elements/dialog/dialog-actions/__snapshots__/dialog-actions.snapshot.spec.snap.js index d17c0f6e58..4a2d694021 100644 --- a/src/elements/dialog/dialog-actions/__snapshots__/dialog-actions.snapshot.spec.snap.js +++ b/src/elements/dialog/dialog-actions/__snapshots__/dialog-actions.snapshot.spec.snap.js @@ -33,3 +33,45 @@ snapshots["sbb-dialog-actions A11y tree Firefox"] = `; /* end snapshot sbb-dialog-actions A11y tree Firefox */ +snapshots["sbb-dialog-actions renders DOM"] = +` + +`; +/* end snapshot sbb-dialog-actions renders DOM */ + +snapshots["sbb-dialog-actions renders Shadow DOM"] = +`
+
+ + +
+
+`; +/* end snapshot sbb-dialog-actions renders Shadow DOM */ + +snapshots["sbb-dialog-actions renders A11y tree Chrome"] = +`

+ { + "role": "WebArea", + "name": "" +} +

+`; +/* end snapshot sbb-dialog-actions renders A11y tree Chrome */ + +snapshots["sbb-dialog-actions renders A11y tree Firefox"] = +`

+ { + "role": "document", + "name": "" +} +

+`; +/* end snapshot sbb-dialog-actions renders A11y tree Firefox */ + diff --git a/src/elements/dialog/dialog-actions/dialog-actions.snapshot.spec.ts b/src/elements/dialog/dialog-actions/dialog-actions.snapshot.spec.ts index 996f90d45b..6050f31571 100644 --- a/src/elements/dialog/dialog-actions/dialog-actions.snapshot.spec.ts +++ b/src/elements/dialog/dialog-actions/dialog-actions.snapshot.spec.ts @@ -2,23 +2,26 @@ import { expect } from '@open-wc/testing'; import { html } from 'lit/static-html.js'; import { fixture, testA11yTreeSnapshot } from '../../core/testing/private.js'; + +import type { SbbDialogActionsElement } from './dialog-actions.js'; import './dialog-actions.js'; describe('sbb-dialog-actions', () => { - it('renders', async () => { - const root = await fixture(html``); + describe('renders', async () => { + let root: SbbDialogActionsElement; - await expect(root).dom.to.equalSnapshot(); + beforeEach(async () => { + root = await fixture(html``); + }); - expect(root).shadowDom.to.be.equal(` -
-
- - -
-
- `); - }); + it('DOM', async () => { + await expect(root).dom.to.be.equalSnapshot(); + }); - testA11yTreeSnapshot(html``); + it('Shadow DOM', async () => { + await expect(root).shadowDom.to.be.equalSnapshot(); + }); + + testA11yTreeSnapshot(); + }); }); diff --git a/src/elements/dialog/dialog-content/__snapshots__/dialog-content.snapshot.spec.snap.js b/src/elements/dialog/dialog-content/__snapshots__/dialog-content.snapshot.spec.snap.js new file mode 100644 index 0000000000..1d242094e9 --- /dev/null +++ b/src/elements/dialog/dialog-content/__snapshots__/dialog-content.snapshot.spec.snap.js @@ -0,0 +1,50 @@ +/* @web/test-runner snapshot v1 */ +export const snapshots = {}; + +snapshots["sbb-dialog-content renders DOM"] = +` + Content + +`; +/* end snapshot sbb-dialog-content renders DOM */ + +snapshots["sbb-dialog-content renders Shadow DOM"] = +`
+ + +
+`; +/* end snapshot sbb-dialog-content renders Shadow DOM */ + +snapshots["sbb-dialog-content renders A11y tree Chrome"] = +`

+ { + "role": "WebArea", + "name": "", + "children": [ + { + "role": "text", + "name": "Content" + } + ] +} +

+`; +/* end snapshot sbb-dialog-content renders A11y tree Chrome */ + +snapshots["sbb-dialog-content renders A11y tree Firefox"] = +`

+ { + "role": "document", + "name": "", + "children": [ + { + "role": "text leaf", + "name": "Content" + } + ] +} +

+`; +/* end snapshot sbb-dialog-content renders A11y tree Firefox */ + diff --git a/src/elements/dialog/dialog-content/dialog-content.snapshot.spec.ts b/src/elements/dialog/dialog-content/dialog-content.snapshot.spec.ts index 5c4dbda497..0c1a2c5e1c 100644 --- a/src/elements/dialog/dialog-content/dialog-content.snapshot.spec.ts +++ b/src/elements/dialog/dialog-content/dialog-content.snapshot.spec.ts @@ -1,17 +1,27 @@ -import { expect, fixture } from '@open-wc/testing'; +import { expect } from '@open-wc/testing'; import { html } from 'lit/static-html.js'; + +import { fixture, testA11yTreeSnapshot } from '../../core/testing/private.js'; + +import type { SbbDialogContentElement } from './dialog-content.js'; import './dialog-content.js'; describe('sbb-dialog-content', () => { - it('renders', async () => { - const root = await fixture(html`Content`); + describe('renders', async () => { + let root: SbbDialogContentElement; + + beforeEach(async () => { + root = await fixture(html`Content`); + }); + + it('DOM', async () => { + await expect(root).dom.to.be.equalSnapshot(); + }); - expect(root).dom.to.be.equal(`Content`); + it('Shadow DOM', async () => { + await expect(root).shadowDom.to.be.equalSnapshot(); + }); - expect(root).shadowDom.to.be.equal(` -
- -
- `); + testA11yTreeSnapshot(); }); }); diff --git a/src/elements/dialog/dialog/dialog.spec.ts b/src/elements/dialog/dialog/dialog.spec.ts index d69e5cf033..ba8f025a88 100644 --- a/src/elements/dialog/dialog/dialog.spec.ts +++ b/src/elements/dialog/dialog/dialog.spec.ts @@ -442,6 +442,7 @@ describe('sbb-dialog with long content', () => { await openDialog(element); expect(element).to.have.attribute('data-state', 'opened'); + await waitForCondition(() => element.hasAttribute('data-overflows')); expect(element).to.have.attribute('data-overflows', ''); }); diff --git a/src/elements/dialog/dialog/dialog.visual.spec.ts b/src/elements/dialog/dialog/dialog.visual.spec.ts new file mode 100644 index 0000000000..8c1246040a --- /dev/null +++ b/src/elements/dialog/dialog/dialog.visual.spec.ts @@ -0,0 +1,113 @@ +import { html, nothing, type TemplateResult } from 'lit'; + +import { describeViewports, visualDiffDefault } from '../../core/testing/private.js'; + +import './dialog.js'; +import '../dialog-actions.js'; +import '../dialog-content.js'; +import '../dialog-title.js'; +import '../../link/block-link.js'; +import '../../button/button.js'; +import '../../button/secondary-button.js'; + +describe(`sbb-dialog`, () => { + const negativeCases = [false, true]; + + const dialogTitle = (backButton = true, hideOnScroll = false): TemplateResult => html` + + A describing title of the dialog + + `; + + const dialogContent = (longContent = false): TemplateResult => html` + +

+ “What really knocks me out is a book that, when you're all done reading it, you wish the + author that wrote it was a terrific friend of yours and you could call him up on the phone + whenever you felt like it. That doesn't happen much, though.” ― J.D. Salinger, The Catcher + in the Rye +

+ ${longContent + ? html` +

+ “What really knocks me out is a book that, when you're all done reading it, you wish + the author that wrote it was a terrific friend of yours and you could call him up on + the phone whenever you felt like it. That doesn't happen much, though.” ― J.D. + Salinger, The Catcher in the Rye +

+ ` + : nothing} +
+ `; + + const dialogFooter = (negative = false): TemplateResult => html` + + + Link + + Cancel + Confirm + + `; + + describeViewports({ viewports: ['zero', 'medium'], viewportHeight: 600 }, () => { + // Negative test + for (const negative of negativeCases) { + it( + `negative=${negative}`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(html` + + ${dialogTitle()} ${dialogContent()} ${dialogFooter(negative)} + + `); + const dialog = setup.snapshotElement.querySelector('sbb-dialog')!; + setup.withSnapshotElement(dialog); + dialog.open(); + }), + ); + } + + it( + `no back button`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(html` + ${dialogTitle(false)} ${dialogContent()} ${dialogFooter()} + `); + const dialog = setup.snapshotElement.querySelector('sbb-dialog')!; + setup.withSnapshotElement(dialog); + dialog.open(); + }), + ); + + it( + `no footer`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(html` + ${dialogTitle()} ${dialogContent()} + `); + const dialog = setup.snapshotElement.querySelector('sbb-dialog')!; + setup.withSnapshotElement(dialog); + dialog.open(); + }), + ); + + it( + `long content`, + visualDiffDefault.with(async (setup) => { + await setup.withFixture(html` + ${dialogTitle()} ${dialogContent(true)} ${dialogFooter()} + `); + const dialog = setup.snapshotElement.querySelector('sbb-dialog')!; + setup.withSnapshotElement(dialog); + dialog.open(); + }), + ); + }); +});