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: respect state element for visual focus tests #3080

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -113,8 +113,19 @@ export const visualDiffFocus: VisualDiffState = {
with(setup: (setup: VisualDiffSetupBuilder) => void | Promise<void>): Mocha.Func {
return async function (this: Mocha.Context) {
const builder = await runSetupWithViewport(setup, this.test?.ctx?.['requestViewport']);
builder.snapshotElement.focus();

// We create a focusable element (<a>) right before the state element.
// We can tab once to land on the desired element.
const link = document.createElement(`a`);
link.href = '#';
link.classList.add('sbb-screen-reader-only');
jeripeierSBB marked this conversation as resolved.
Show resolved Hide resolved
// We need to copy the slot so we can ensure it's landing at the right position
link.slot = builder.stateElement.slot;
builder.stateElement.insertAdjacentElement('beforebegin', link);
link.focus();
await sendKeys({ press: tabKey });
link.remove();

await visualDiff(builder.snapshotElement, imageName(this.test!));
};
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { sendKeys } from '@web/test-runner-commands';
import { html, nothing } from 'lit';

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

import './datepicker-next-day.js';
Expand All @@ -20,51 +18,37 @@ describe(`sbb-datepicker-next-day`, () => {
};

describeViewports({ viewports: ['zero', 'medium'] }, () => {
it(
`standalone`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(html`<sbb-datepicker-next-day></sbb-datepicker-next-day>`);
}),
);

describeEach(cases, ({ negative, value }) => {
let root: HTMLElement;

beforeEach(async () => {
root = await visualRegressionFixture(
html`
<sbb-form-field ?negative=${negative}>
<input value=${value || nothing} />
<sbb-datepicker></sbb-datepicker>
<sbb-datepicker-next-day></sbb-datepicker-next-day>
</sbb-form-field>
`,
{ backgroundColor: negative ? 'var(--sbb-color-black)' : undefined },
);
});

for (const state of [visualDiffDefault, visualDiffFocus]) {
it(
`with form-field`,
visualDiffDefault.with(async (setup) => {
setup.withSnapshotElement(root);
`standalone ${state.name}`,
state.with(async (setup) => {
await setup.withFixture(html`<sbb-datepicker-next-day></sbb-datepicker-next-day>`);
}),
);

it(
`with form-field focus`,
visualDiffDefault.with(async (setup) => {
setup.withSnapshotElement(root);

if (value) {
// Focus input so that with a tab press it should land on next day
setup.snapshotElement.querySelector('input')!.focus();
} else {
setup.snapshotElement.focus();
}

await sendKeys({ press: tabKey });
}),
);
});
describeEach(cases, ({ negative, value }) => {
it(
`with form-field ${state.name}`,
state.with(async (setup) => {
await setup.withFixture(
html`
<sbb-form-field ?negative=${negative}>
<input value=${value || nothing} />
<sbb-datepicker></sbb-datepicker>
<sbb-datepicker-next-day></sbb-datepicker-next-day>
</sbb-form-field>
`,
{ backgroundColor: negative ? 'var(--sbb-color-black)' : undefined },
);

if (value) {
setup.withStateElement(
setup.snapshotElement.querySelector('sbb-datepicker-next-day')!,
);
}
}),
);
});
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ describe(`sbb-datepicker-previous-day`, () => {
`,
{ backgroundColor: negative ? 'var(--sbb-color-black)' : undefined },
);

if (value) {
setup.withStateElement(
setup.snapshotElement.querySelector('sbb-datepicker-previous-day')!,
);
}
}),
);
});
Expand Down
11 changes: 2 additions & 9 deletions src/elements/form-field/form-field/form-field.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { sendKeys } from '@web/test-runner-commands';
import { html, nothing, type TemplateResult } from 'lit';

import type { SbbMiniButtonElement } from '../../button/mini-button.js';
import { tabKey } from '../../core/testing/private/keys.js';
import {
describeEach,
describeViewports,
Expand Down Expand Up @@ -221,19 +218,15 @@ describe(`sbb-form-field`, () => {

it(
`slot=buttons focus`,
visualDiffDefault.with(async (setup) => {
visualDiffFocus.with(async (setup) => {
const templateResult: TemplateResult = html`${template(args)}
${buttonsAndPopover(args)}`;
await setup.withFixture(html`${formField(args, templateResult)}`, {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
focusOutlineDark: negative,
forcedColors,
});
(
setup.snapshotElement.querySelector(name)!
.nextElementSibling as SbbMiniButtonElement
).focus();
await sendKeys({ press: tabKey });
setup.withStateElement(setup.snapshotElement.querySelector('sbb-mini-button')!);
}),
);
});
Expand Down
17 changes: 9 additions & 8 deletions src/elements/header/header/header.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { sendKeys } from '@web/test-runner-commands';
import { html, type TemplateResult } from 'lit';

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

import './header.js';
import '../header-link.js';
Expand Down Expand Up @@ -64,14 +66,13 @@ describe(`sbb-header`, () => {
describe(logoOrSignet, () => {
it(
'focus',
visualDiffDefault.with(async (setup) => {
visualDiffFocus.with(async (setup) => {
await setup.withFixture(template(false, logoOrSignet === 'signet' ? 's' : 'm'), {
padding: '0',
});
setup.snapshotElement.querySelector<HTMLAnchorElement>(`a[slot='logo']`)!.focus();
// We focus the logo and then tab left and right again to activate the focus visible state on the link
await sendKeys({ press: `Shift+${tabKey}` });
await sendKeys({ press: tabKey });
setup.withStateElement(
setup.snapshotElement.querySelector<HTMLAnchorElement>(`a[slot='logo']`)!,
);
}),
);
});
Expand Down
Loading