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-select): add visual tests #2878

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions src/elements/select/select.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from '@storybook/web-components';
import isChromatic from 'chromatic/isChromatic';
import type { TemplateResult } from 'lit';
import { html, nothing } from 'lit';
import { html } from 'lit';
import type { StyleInfo } from 'lit/directives/style-map.js';
import { styleMap } from 'lit/directives/style-map.js';

Expand Down Expand Up @@ -241,7 +241,7 @@ const textBlock = (text: string | null = null): TemplateResult => {
the form field, but it must always be covered by the select overlay.
</span>
`
: nothing}
: text}
</div>
`;
};
Expand Down
251 changes: 251 additions & 0 deletions src/elements/select/select.visual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
import { html, nothing, type TemplateResult } from 'lit';

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

import '../form-error.js';
import '../form-field.js';
import '../option.js';
import './select.js';

describe('sbb-select', () => {
const valueEllipsis: string = 'This label name is so long that it needs ellipsis to fit.';
const defaultArgs = {
borderless: false,
negative: false,
floatingLabel: false,
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
disableOption: false,
withOptionGroup: false,
disableGroup: false,
withEllipsis: false,
value: undefined as string | string[] | undefined,
multiple: false,
disabled: false,
required: false,
readonly: false,
};

const createOptions = (
disableOption: boolean,
group: string | boolean,
selectValue: string | string[] | undefined = undefined,
): TemplateResult[] => {
return new Array(5).fill(null).map((_, i) => {
const value = group ? `Option ${i + 1} ${' - ' + group}` : `Option ${i + 1}`;
const selected = Array.isArray(selectValue)
? selectValue.includes(value)
: selectValue === value;
return html`
<sbb-option value=${value} ?disabled=${disableOption && i < 2} ?selected=${selected}>
${value}
</sbb-option>
`;
});
};

const createOptionsGroup = (
disableOption: boolean,
disableGroup: boolean,
): TemplateResult => html`
<sbb-optgroup label="Group 1" ?disabled=${disableGroup}>
${createOptions(disableOption, '1')}
</sbb-optgroup>
<sbb-optgroup label="Group 2"> ${createOptions(disableOption, '2')} </sbb-optgroup>
`;

const template = ({
borderless,
negative,
floatingLabel,
disableOption,
withOptionGroup,
disableGroup,
withEllipsis,
...args
}: typeof defaultArgs): TemplateResult => {
if (args.multiple && args.value) {
args.value = [args.value as string];
}
return html`
<div>
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
<sbb-form-field
?borderless=${borderless}
?negative=${negative}
?floating-label=${floatingLabel}
>
<label>Select</label>
<sbb-select
value=${args.value || nothing}
?multiple=${args.multiple}
?disabled=${args.disabled}
?required=${args.required}
?readonly=${args.readonly}
class=${args.required ? 'sbb-invalid' : nothing}
>
${withEllipsis
? html` <sbb-option value=${valueEllipsis} selected="">
${valueEllipsis}
</sbb-option>`
: nothing}
${withOptionGroup
? createOptionsGroup(disableOption, disableGroup)
: createOptions(disableOption, false, args.value)}
</sbb-select>
${args.required ? html`<sbb-form-error>Error</sbb-form-error>` : nothing}
</sbb-form-field>
</div>
`;
};

describeViewports({ viewports: ['zero', 'medium'], viewportHeight: 400 }, () => {
for (const negative of [false, true]) {
for (const visualDiffState of [visualDiffDefault, visualDiffFocus]) {
it(
`state=above negative=${negative} ${visualDiffState.name}`,
visualDiffState.with(async (setup) => {
await setup.withFixture(
html`
<div style="position: absolute; inset-block-end: 2rem; width: calc(100% - 4rem);">
DavideMininni-Fincons marked this conversation as resolved.
Show resolved Hide resolved
${template({ ...defaultArgs, negative })}
</div>
`,
{
minHeight: '400px',
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
},
);
setup.withPostSetupAction(() => {
const select = setup.snapshotElement.querySelector('sbb-select')!;
select.open();
});
}),
);
}
}
});

describeViewports({ viewports: ['zero', 'medium'] }, () => {
for (const negative of [false, true]) {
for (const visualDiffState of [visualDiffDefault, visualDiffFocus]) {
it(
`state=${visualDiffState.name} negative=${negative}`,
visualDiffState.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative }), {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
}),
);
}

it(
`state=required negative=${negative}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative, required: true }), {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
}),
);

it(
`state=disabled negative=${negative}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative, disabled: true }), {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
}),
);

it(
`state=readonly negative=${negative}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative, readonly: true }), {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
}),
);

it(
`state=borderless negative=${negative}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative, borderless: true }), {
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
}),
);

for (const multiple of [false, true]) {
it(
`negative=${negative} multiple=${multiple}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(template({ ...defaultArgs, negative, multiple }), {
minHeight: '400px',
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
});
setup.withPostSetupAction(() => {
const select = setup.snapshotElement.querySelector('sbb-select')!;
select.open();
});
}),
);

it(
`negative=${negative} multiple=${multiple} withEllipsis=true`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(
template({ ...defaultArgs, negative, multiple, withEllipsis: true }),
{
minHeight: '600px',
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
},
);
setup.withPostSetupAction(() => {
const select = setup.snapshotElement.querySelector('sbb-select')!;
select.open();
});
}),
);

it(
`negative=${negative} multiple=${multiple} disableOption=true`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(
template({ ...defaultArgs, negative, multiple, disableOption: true }),
{
minHeight: '400px',
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
},
);
setup.withPostSetupAction(() => {
const select = setup.snapshotElement.querySelector('sbb-select')!;
select.open();
});
}),
);

for (const disableGroup of [false, true]) {
it(
`negative=${negative} multiple=${multiple} withOptionGroup=true disableGroup=${disableGroup}`,
visualDiffDefault.with(async (setup) => {
await setup.withFixture(
template({
...defaultArgs,
negative,
multiple,
disableGroup,
withOptionGroup: true,
}),
{
minHeight: '800px',
backgroundColor: negative ? 'var(--sbb-color-black)' : undefined,
},
);
setup.withPostSetupAction(() => {
const select = setup.snapshotElement.querySelector('sbb-select')!;
select.open();
});
}),
);
}
}
}
});
});
Loading