|
| 1 | +import { expect, test, type Page } from '@playwright/test'; |
| 2 | + |
| 3 | +const GALLERY = './'; |
| 4 | + |
| 5 | +// Noir is a palette design with square corners and filled cells, so every tweak |
| 6 | +// below is a visible change from how it comes. |
| 7 | +const openNoir = async (page: Page) => { |
| 8 | + await page.goto(GALLERY); |
| 9 | + await page.getByRole('button', { name: 'Inspect Noir' }).click(); |
| 10 | + const dialog = page.getByRole('dialog', { name: 'Noir design' }); |
| 11 | + await expect(dialog).toBeVisible(); |
| 12 | + return dialog; |
| 13 | +}; |
| 14 | + |
| 15 | +test('a design opens large, with the code needed to use it', async ({ page }) => { |
| 16 | + const dialog = await openNoir(page); |
| 17 | + |
| 18 | + await expect(dialog.getByRole('table', { name: /React Matrix/ })).toBeVisible(); |
| 19 | + await expect(dialog.locator('.tweak-snippet')).toHaveText( |
| 20 | + "import ReactMatrix, { noir } from 'react-data-matrix';\n\n" + |
| 21 | + '<ReactMatrix data={data} theme={noir} />' |
| 22 | + ); |
| 23 | + // The token table is generated from the theme being rendered, so it cannot |
| 24 | + // drift from it. |
| 25 | + await expect(dialog.getByRole('row', { name: /^cellVariant/ })).toContainText('fill'); |
| 26 | + |
| 27 | + await page.keyboard.press('Escape'); |
| 28 | + await expect(dialog).toBeHidden(); |
| 29 | +}); |
| 30 | + |
| 31 | +test('tweaking a design changes both the preview and the snippet', async ({ page }) => { |
| 32 | + const dialog = await openNoir(page); |
| 33 | + const cell = dialog.locator('td.rdm-cell').first(); |
| 34 | + const before = await cell.evaluate((element) => getComputedStyle(element).borderRadius); |
| 35 | + |
| 36 | + await dialog.getByRole('radio', { name: 'Pill' }).check(); |
| 37 | + await dialog.getByRole('radio', { name: 'Score' }).check(); |
| 38 | + |
| 39 | + await expect(dialog.locator('.tweak-snippet')).toContainText( |
| 40 | + "theme={{ ...noir, radius: '999px', emphasis: 'score' }}" |
| 41 | + ); |
| 42 | + expect(await cell.evaluate((element) => getComputedStyle(element).borderRadius)).not.toBe(before); |
| 43 | + await expect(dialog.getByRole('row', { name: /^radius/ })).toContainText('999px'); |
| 44 | + |
| 45 | + // "As it comes" must return the design to its published form. |
| 46 | + await dialog.getByRole('radio', { name: 'As it comes' }).first().check(); |
| 47 | + await expect(dialog.locator('.tweak-snippet')).toContainText("theme={{ ...noir, emphasis:"); |
| 48 | + await expect(dialog.locator('.tweak-snippet')).not.toContainText('radius'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('choosing cells shows what the click handler receives, newest first', async ({ page }) => { |
| 52 | + const dialog = await openNoir(page); |
| 53 | + const log = dialog.locator('.inspect-log li'); |
| 54 | + |
| 55 | + await expect(log).toHaveCount(0); |
| 56 | + await dialog.getByRole('button', { name: /Likelihood Rare, Consequence Minor/ }).click(); |
| 57 | + await expect(log).toHaveText(['Rare × Minor — low (1)']); |
| 58 | + |
| 59 | + await dialog |
| 60 | + .getByRole('button', { name: /Likelihood Almost Certain, Consequence Catastrophic/ }) |
| 61 | + .focus(); |
| 62 | + await page.keyboard.press('Enter'); |
| 63 | + await expect(log.first()).toHaveText('Almost Certain × Catastrophic — extreme (25)'); |
| 64 | + |
| 65 | + // Only the last five are kept, so the log cannot grow without bound. |
| 66 | + for (const name of await dialog.getByRole('button', { name: /Likelihood/ }).all()) { |
| 67 | + await name.click(); |
| 68 | + } |
| 69 | + await expect(log).toHaveCount(5); |
| 70 | +}); |
| 71 | + |
| 72 | +test('the inspected matrix is shown whole on a desktop screen', async ({ page }) => { |
| 73 | + test.skip((page.viewportSize()?.width ?? 0) < 1024, 'phones scroll the matrix inside the dialog'); |
| 74 | + await openNoir(page); |
| 75 | + |
| 76 | + // Either side of the one-column/two-column split: a side panel that leaves the |
| 77 | + // matrix too little room clips it, which is how it shipped the first time. |
| 78 | + for (const width of [1280, 1360, 1600, 2400]) { |
| 79 | + await page.setViewportSize({ width, height: 900 }); |
| 80 | + const clipped = await page |
| 81 | + .locator('.inspect-preview .rdm-root') |
| 82 | + .evaluate((root) => root.scrollWidth > root.clientWidth); |
| 83 | + expect(clipped, `clipped at ${width}px`).toBe(false); |
| 84 | + } |
| 85 | +}); |
| 86 | + |
| 87 | +test('the snippet can be copied to the clipboard', async ({ page, browserName, context }) => { |
| 88 | + test.skip(browserName !== 'chromium', 'clipboard permissions are Chromium-only in Playwright'); |
| 89 | + await context.grantPermissions(['clipboard-read', 'clipboard-write']); |
| 90 | + const dialog = await openNoir(page); |
| 91 | + |
| 92 | + await dialog.getByRole('button', { name: 'Copy' }).click(); |
| 93 | + |
| 94 | + await expect(dialog.getByRole('status')).toHaveText('Copied'); |
| 95 | + const clipboard = await page.evaluate(() => navigator.clipboard.readText()); |
| 96 | + expect(clipboard).toContain('<ReactMatrix data={data} theme={noir} />'); |
| 97 | +}); |
| 98 | + |
| 99 | +test('each design opens with its own tokens and an empty log', async ({ page }) => { |
| 100 | + const noir = await openNoir(page); |
| 101 | + await noir.getByRole('button', { name: /Likelihood Rare, Consequence Minor/ }).click(); |
| 102 | + await expect(noir.locator('.inspect-log li')).toHaveCount(1); |
| 103 | + await page.keyboard.press('Escape'); |
| 104 | + |
| 105 | + await page.getByRole('button', { name: 'Inspect Aurora' }).click(); |
| 106 | + const aurora = page.getByRole('dialog', { name: 'Aurora design' }); |
| 107 | + |
| 108 | + await expect(aurora.locator('.tweak-snippet')).toContainText('theme={aurora}'); |
| 109 | + await expect(aurora.locator('.inspect-log li')).toHaveCount(0); |
| 110 | +}); |
0 commit comments