import 'jest-puppeteer'; import { type Page } from 'puppeteer'; import { getStatusStyle, getTokenColor, gotoStory } from '../../../../helpers/test'; import { MESSAGE_EMPHASES, MESSAGE_EMPHASIS, type MessageEmphasis } from '../../src/constants/message-emphasis'; import { MESSAGE_STATUS, MESSAGE_STATUSES, type MessageStatus } from '../../src/constants/message-status'; const TRANSPARENT = 'rgba(0, 0, 0, 0)'; type Rendering = { background: string, border: string, icon: string, text: string }; type SpecRoles = { high: [string, string, string], medium: [string, string, string], low: string }; // The semantic roles each emphasis reads. High and medium are [background, border, text], both lined with the subtle // status border: high on the subtle status surface, medium on the raised surface. Low has a transparent background and // border, and the status text. const SPEC: Record = { [MESSAGE_STATUS.accent]: { high: ['surface-accent-subtle', 'border-accent-subtle', 'content-on-accent'], low: 'content-accent', medium: ['surface-raised', 'border-accent-subtle', 'content-accent'] }, [MESSAGE_STATUS.critical]: { high: ['surface-critical-subtle', 'border-critical-subtle', 'content-on-critical'], low: 'content-critical', medium: ['surface-raised', 'border-critical-subtle', 'content-critical'] }, [MESSAGE_STATUS.information]: { high: ['surface-information-subtle', 'border-information-subtle', 'content-on-information'], low: 'content-information', medium: ['surface-raised', 'border-information-subtle', 'content-information'] }, [MESSAGE_STATUS.neutral]: { high: ['surface-neutral-subtle', 'border-neutral-subtle', 'content-on-neutral'], low: 'content-neutral', medium: ['surface-raised', 'border-neutral-subtle', 'content-neutral'] }, [MESSAGE_STATUS.success]: { high: ['surface-success-subtle', 'border-success-subtle', 'content-on-success'], low: 'content-success', medium: ['surface-raised', 'border-success-subtle', 'content-success'] }, [MESSAGE_STATUS.warning]: { high: ['surface-warning-subtle', 'border-warning-subtle', 'content-on-warning'], low: 'content-warning', medium: ['surface-raised', 'border-warning-subtle', 'content-warning'] }, }; async function getExpectedRendering(page: Page, status: MessageStatus, emphasis: MessageEmphasis): Promise { const { high, low, medium } = SPEC[status]; const color = (role: string): Promise => getTokenColor(page, `--ods-semantic-color-${role}`); switch (emphasis) { case MESSAGE_EMPHASIS.low: { const text = await color(low); return { background: TRANSPARENT, border: `1px solid ${TRANSPARENT}`, icon: text, text }; } default: { const [background, border, text] = await Promise.all((emphasis === MESSAGE_EMPHASIS.high ? high : medium).map(color)); return { background, border: `1px solid ${border}`, icon: text, text }; } } } async function getRendering(page: Page, testId: string): Promise { const element = await page.waitForSelector(`[data-testid="${testId}"]`); return element!.evaluate((el: Element) => { const style = getComputedStyle(el); const border = style.borderTopStyle === 'none' ? 'none' : `${style.borderTopWidth} ${style.borderTopStyle} ${style.borderTopColor}`; return { background: style.backgroundColor, border, icon: getComputedStyle(el.querySelector('[data-ods="message-icon"]')!).color, text: style.color, }; }); } async function getCloseButtonStyle(page: Page, testId: string): Promise { const element = await page.waitForSelector(`[data-testid="${testId}"] button`); return element!.evaluate((el: Element) => { const style = getComputedStyle(el); return [style.backgroundColor, style.borderTopColor, style.color, style.height].join(' | '); }); } const PAIRS = MESSAGE_STATUSES.flatMap((status) => MESSAGE_EMPHASES.map((emphasis) => [status, emphasis] as const)); describe('Message rendering', () => { it('should render the web component', async() => { await gotoStory(page, 'rendering/render'); expect(await page.waitForSelector('[data-testid="render"]')).not.toBeNull(); expect(await page.waitForSelector('[data-ods="message"]')).not.toBeNull(); expect(await page.waitForSelector('[data-ods="message-body"]')).not.toBeNull(); expect(await page.waitForSelector('[data-ods="message-icon"]')).not.toBeNull(); }); describe('custom style', () => { it('should render with custom style applied', async() => { await gotoStory(page, 'rendering/custom-style'); const message = await page.waitForSelector('[data-testid="custom-style"]'); const height = await message?.evaluate((el: Element) => el.getBoundingClientRect().height); expect(height).toBe(42); }); }); describe('statuses', () => { it('should style every status, unlike an unknown status', async() => { await gotoStory(page, 'rendering/statuses'); const unknownStyle = await getStatusStyle(page, 'status-none'); const styles = await Promise.all(MESSAGE_STATUSES.map((status) => getStatusStyle(page, `status-${status}`))); const unstyledStatuses = MESSAGE_STATUSES.filter((_, index) => styles[index] === unknownStyle); expect(unstyledStatuses).toEqual([]); }); }); describe('emphases', () => { beforeAll(async() => { await gotoStory(page, 'rendering/emphases'); }); it('should render medium by default, for every status', async() => { await gotoStory(page, 'rendering/emphases'); const defaultStyles = await Promise.all(MESSAGE_STATUSES.map((status) => getStatusStyle(page, `${status}-default`))); const mediumStyles = await Promise.all(MESSAGE_STATUSES.map((status) => getStatusStyle(page, `${status}-${MESSAGE_EMPHASIS.medium}`))); expect(defaultStyles).toEqual(mediumStyles); }); it('should render an unknown emphasis as medium', async() => { const unknownStyles = await Promise.all(MESSAGE_STATUSES.map((status) => getStatusStyle(page, `${status}-unknown`))); const mediumStyles = await Promise.all(MESSAGE_STATUSES.map((status) => getStatusStyle(page, `${status}-${MESSAGE_EMPHASIS.medium}`))); expect(unknownStyles).toEqual(mediumStyles); }); it('should apply the spec roles to the background, border, text and icon of every status and emphasis', async() => { const renderings = await Promise.all(PAIRS.map(([status, emphasis]) => getRendering(page, `${status}-${emphasis}`))); const expectations = await Promise.all(PAIRS.map(([status, emphasis]) => getExpectedRendering(page, status, emphasis))); const mismatches = PAIRS .map(([status, emphasis], index) => ({ actual: renderings[index], expected: expectations[index], testId: `${status}-${emphasis}` })) .filter(({ actual, expected }) => JSON.stringify(actual) !== JSON.stringify(expected)); expect(mismatches).toEqual([]); }); it('should render the same close button in every status and emphasis', async() => { const styles = await Promise.all(PAIRS.map(([status, emphasis]) => getCloseButtonStyle(page, `${status}-${emphasis}`))); expect(new Set(styles).size).toBe(1); }); }); });