Skip to content

test(documentation): added basic e2e tests for the new palette implementation #5338

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

Open
wants to merge 4 commits into
base: feat/tokens-integration-test
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion packages/documentation/.storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<script src="/assets/scripts/storybook-preview-events.js"></script>
<body data-color-scheme="light">
<script src="/assets/scripts/storybook-preview-events.js"></script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

light dark should be the default without having the specify a data-color-scheme attribute. Do you know why it is necessary here?

</body>
6 changes: 2 additions & 4 deletions packages/documentation/.storybook/styles/preview.scss
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// importing the complete styles package scss
@use '@swisspost/design-system-styles/core.scss' as post;
@use '@swisspost/internet-header/dist/swisspost-internet-header/swisspost-internet-header.css';
@use './components';
@use '@swisspost/design-system-styles/functions/tokens';
@use '@swisspost/design-system-styles/tokens/utilities';

tokens.$default-map: utilities.$post-color;
@use '@swisspost/internet-header/dist/swisspost-internet-header/swisspost-internet-header.css';
@use './components';

$monospace: SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;

Expand Down
159 changes: 151 additions & 8 deletions packages/documentation/cypress/e2e/components/palette.cy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,156 @@
describe('Palette', () => {
describe('Accessibility', () => {
beforeEach(() => {
cy.visit('/iframe.html?id=snapshots--palette');
cy.get('.palette-default', { timeout: 30000 }).should('be.visible');
cy.injectAxe();
interface PaletteDefinition {
name: string;
scheme: 'even' | 'swapped' | 'light' | 'dark';
}

interface ThemeDefinition {
name: string;
palettes: PaletteDefinition[];
}

const THEME_PALETTE_DEFINITIONS: ThemeDefinition[] = [
{
name: 'Post',
palettes: [
{ name: 'Default', scheme: 'even' },
{ name: 'Alternate', scheme: 'even' },
{ name: 'Accent', scheme: 'dark' },
{ name: 'Brand', scheme: 'light' },
],
},
{
name: 'Cargo',
palettes: [
{ name: 'Default', scheme: 'even' },
{ name: 'Aternate', scheme: 'even' },
{ name: 'Accent', scheme: 'light' },
{ name: 'Brand', scheme: 'dark' },
],
},
];

function testGenerator(
callback: (theme: string, scheme: string, palette: PaletteDefinition) => void,
) {
THEME_PALETTE_DEFINITIONS.forEach(theme => {
['light', 'dark'].forEach(scheme => {
theme.palettes.forEach(palette => {
callback(theme.name, scheme, palette);
});
});
});
}

function isDark(color: string) {
const rgb = color.match(/\d+/g);

if (rgb) {
const [r, g, b] = rgb.map(Number);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
return brightness < 128;
}

return false;
}

describe('Palette', () => {
testGenerator((theme, scheme, palette) => {
const THEME_STYLES_URL = `/styles/${theme.toLowerCase()}-default.css`;

describe(`${theme}: ${scheme} - Palette ${palette.name}`, () => {
beforeEach(() => {
cy.visit('/iframe.html?id=43481535-5b39-40b5-a273-478b07dc3b31--default');

cy.get('[data-color-scheme]', { timeout: 30000 })
.as('schemecontainer')
.should('be.visible');
cy.get('link[href="/styles/post-default.css"]').as('theme-css');
cy.get('.palette').as('palette');

it('Has no detectable a11y violations on load for all variants', () => {
cy.checkA11y('#root-inner');
// update theme stylesheet (if needed)
cy.get('@theme-css')
.invoke('attr', 'href')
.then(href => {
if (href !== THEME_STYLES_URL) {
cy.intercept('GET', THEME_STYLES_URL).as('load-theme-styles');
cy.get('@theme-css').invoke('attr', 'href', THEME_STYLES_URL);
cy.wait('@load-theme-styles');
}
});

// update page color-scheme
cy.get('@schemecontainer').invoke('attr', 'data-color-scheme', scheme);

// update palette variant
cy.get('@palette')
.invoke('removeClass', 'palette-default')
.invoke('addClass', `palette-${palette.name.toLowerCase()}`);
});

it(`should have a color-scheme "${palette.scheme}"`, () => {
cy.get('@schemecontainer').then($container => {
const containerStyles = window.getComputedStyle($container.get(0));
const containerScheme = containerStyles.getPropertyValue('color-scheme');

cy.get('@palette').then($palette => {
const paletteStyles = window.getComputedStyle($palette.get(0));
const paletteScheme = paletteStyles.getPropertyValue('color-scheme');

// check if palette color-scheme is set correctly, according to the page color-scheme
switch (palette.scheme) {
case 'even':
// light on light | dark on dark
expect(paletteScheme).to.equal(containerScheme);
break;
case 'swapped':
// dark on light | light on dark
expect(paletteScheme).to.not.equal(containerScheme);
break;
case 'light':
// light on light | light on dark
expect(paletteScheme).to.equal('light');
break;
case 'dark':
// dark on light | dark on dark
expect(paletteScheme).to.equal('dark');
break;
}
});
});
});

it(`should have a background- and foreground-color matching its color-scheme`, () => {
cy.get('@palette').then($palette => {
const paletteStyles = window.getComputedStyle($palette.get(0));
const paletteScheme = paletteStyles.getPropertyValue('color-scheme');
const paletteBg = paletteStyles.getPropertyValue('background-color');
const paletteFg = paletteStyles.getPropertyValue('color');

// check if palette background- and foreground-color are set correctly, according to the palette color-scheme
switch (paletteScheme) {
case 'light':
expect(isDark(paletteBg)).to.be.false;
expect(isDark(paletteFg)).to.be.true;
break;
case 'dark':
expect(isDark(paletteBg)).to.be.true;
expect(isDark(paletteFg)).to.be.false;
break;
}
});
});
});
});
});

describe('Accessibility', () => {
beforeEach(() => {
cy.visit('/iframe.html?id=snapshots--palette');
cy.get('.palette-default', { timeout: 30000 }).should('be.visible');
cy.injectAxe();
});

it('Has no detectable a11y violations on load for all variants', () => {
cy.checkA11y('#root-inner');
});
});
4 changes: 1 addition & 3 deletions packages/documentation/src/shared/snapshots/schemes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export function schemes(renderFn: (scheme: string) => TemplateResult, options: I
return html`${[...additionalSchemes, ...Object.values(COLOR_SCHEMES)]
.filter(filter)
.map(
scheme => html` <div data-color-scheme="${scheme}" class="p-16 palette-default">
${renderFn(scheme)}
</div>`,
scheme => html` <div data-color-scheme="${scheme}" class="p-16">${renderFn(scheme)}</div>`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to test how the palettes behave without a defined data-color-scheme, I have seen this article about dark mode in Cypress: https://www.cypress.io/blog/test-your-web-app-in-dark-mode

)}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default meta;
// RENDERER
function renderPalette(args: Args) {
return html`
<div class="palette-${args.palette} p-24" data-color-scheme=${args.colorScheme ?? nothing}>
<div class="palette palette-${args.palette} p-24">
<h2 class="palette-text">
I use a specific color from the palette (it might be the same as the body color).
</h2>
Expand Down
Loading