|
| 1 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | + |
| 4 | +import { expect, test } from '@playwright/test'; |
| 5 | +import process from 'process'; |
| 6 | + |
| 7 | +import { PREVIEW_STORY_TIMEOUT, waitForPreviewReady } from './helpers.ts'; |
| 8 | + |
| 9 | +const storybookUrl = process.env.STORYBOOK_URL || 'http://localhost:6006'; |
| 10 | +const runsAgainstDevServer = !['build', 'static'].includes(process.env.STORYBOOK_TYPE || 'dev'); |
| 11 | + |
| 12 | +const codeDir = process.cwd(); |
| 13 | +const buttonSourcePath = join(codeDir, 'core/src/components/components/Button/Button.tsx'); |
| 14 | +const storyPath = '/story/button-component--base'; |
| 15 | +const hotUpdatePropName = 'e2eDocgenHotUpdateProp'; |
| 16 | +const hotUpdatePropSource = ` |
| 17 | + /** |
| 18 | + * E2E-only docgen hot update marker. |
| 19 | + */ |
| 20 | + ${hotUpdatePropName}?: 'before' | 'after'; |
| 21 | +`; |
| 22 | + |
| 23 | +// Start the internal dev server with STORYBOOK_EXPERIMENTAL_DOCGEN_SERVER=true before running this |
| 24 | +// spec. CI sets that env var in the internal Storybook e2e job. |
| 25 | +let originalButtonSource: string | undefined; |
| 26 | + |
| 27 | +async function restoreFile(path: string, contents: string) { |
| 28 | + if ((await readFile(path, 'utf8')) !== contents) { |
| 29 | + await writeFile(path, contents, 'utf8'); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function addHotUpdateProp() { |
| 34 | + const current = await readFile(buttonSourcePath, 'utf8'); |
| 35 | + if (current.includes(hotUpdatePropName)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const marker = ' shortcut?: API_KeyCollection;\n'; |
| 40 | + expect(current, `Could not find ButtonProps insertion marker in ${buttonSourcePath}`).toContain( |
| 41 | + marker |
| 42 | + ); |
| 43 | + |
| 44 | + await writeFile(buttonSourcePath, current.replace(marker, `${marker}${hotUpdatePropSource}`)); |
| 45 | +} |
| 46 | + |
| 47 | +test.describe('docgen open service hot updates', () => { |
| 48 | + test.describe.configure({ mode: 'serial' }); |
| 49 | + test.setTimeout(90_000); |
| 50 | + |
| 51 | + test.beforeAll(async () => { |
| 52 | + test.skip(!runsAgainstDevServer, 'Docgen hot updates require the dev server file watcher.'); |
| 53 | + |
| 54 | + originalButtonSource = await readFile(buttonSourcePath, 'utf8'); |
| 55 | + }); |
| 56 | + |
| 57 | + test.afterAll(async () => { |
| 58 | + if (originalButtonSource) { |
| 59 | + await restoreFile(buttonSourcePath, originalButtonSource); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + test('updates manager Controls when a component prop type changes without navigation', async ({ |
| 64 | + page, |
| 65 | + }) => { |
| 66 | + await restoreFile(buttonSourcePath, originalButtonSource!); |
| 67 | + |
| 68 | + await page.goto(`${storybookUrl}/?path=${storyPath}`); |
| 69 | + await expect |
| 70 | + .poll( |
| 71 | + () => |
| 72 | + page.evaluate(() => |
| 73 | + Boolean( |
| 74 | + ( |
| 75 | + globalThis as { |
| 76 | + FEATURES?: { experimentalDocgenServer?: boolean }; |
| 77 | + } |
| 78 | + ).FEATURES?.experimentalDocgenServer |
| 79 | + ) |
| 80 | + ), |
| 81 | + { timeout: PREVIEW_STORY_TIMEOUT } |
| 82 | + ) |
| 83 | + .toBe(true); |
| 84 | + await waitForPreviewReady(page); |
| 85 | + await expect( |
| 86 | + page.frameLocator('#storybook-preview-iframe').getByRole('button', { name: 'Button' }) |
| 87 | + ).toBeVisible({ timeout: PREVIEW_STORY_TIMEOUT }); |
| 88 | + |
| 89 | + await page.getByRole('tab', { name: 'Controls' }).click(); |
| 90 | + const controlsPanel = page.getByRole('tabpanel', { name: 'Controls' }); |
| 91 | + await expect( |
| 92 | + controlsPanel.getByRole('cell', { name: 'ariaLabel', exact: true }).first() |
| 93 | + ).toBeVisible({ |
| 94 | + timeout: PREVIEW_STORY_TIMEOUT, |
| 95 | + }); |
| 96 | + await expect(controlsPanel.getByText(hotUpdatePropName)).toHaveCount(0); |
| 97 | + |
| 98 | + try { |
| 99 | + await addHotUpdateProp(); |
| 100 | + |
| 101 | + await expect( |
| 102 | + controlsPanel.getByRole('cell', { name: hotUpdatePropName, exact: true }).first() |
| 103 | + ).toBeVisible({ timeout: PREVIEW_STORY_TIMEOUT }); |
| 104 | + } finally { |
| 105 | + await restoreFile(buttonSourcePath, originalButtonSource!); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments