|
| 1 | +import { expect, test } from '@playwright/test' |
| 2 | +import { BUNDLER, IS_DEV } from '../../utils/env' |
| 3 | +import { readSourceMarkdown, writeSourceMarkdown } from '../../utils/source' |
| 4 | + |
| 5 | +const updateMarkdownContent = async (): Promise<void> => { |
| 6 | + const content = await readSourceMarkdown('composables/on-content-updated.md') |
| 7 | + await writeSourceMarkdown( |
| 8 | + 'composables/on-content-updated.md', |
| 9 | + `${content}\n\nUpdated content`, |
| 10 | + ) |
| 11 | +} |
| 12 | + |
| 13 | +const restoreMarkdownContent = async (): Promise<void> => { |
| 14 | + await writeSourceMarkdown( |
| 15 | + 'composables/on-content-updated.md', |
| 16 | + '## title\n\ncontent\n', |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +test.afterAll(async () => { |
| 21 | + await restoreMarkdownContent() |
| 22 | +}) |
| 23 | + |
| 24 | +test('should call content hook on mounted', async ({ page }) => { |
| 25 | + await page.goto('composables/on-content-updated.html') |
| 26 | + const mountedLocator = page.locator( |
| 27 | + '.markdown-content-hooks .markdown-content-mounted', |
| 28 | + ) |
| 29 | + await expect(mountedLocator).toHaveText( |
| 30 | + 'mounted: /composables/on-content-updated.html 1', |
| 31 | + ) |
| 32 | + |
| 33 | + // update content but mounted hook should not be called twice |
| 34 | + await updateMarkdownContent() |
| 35 | + await expect(mountedLocator).toHaveText( |
| 36 | + 'mounted: /composables/on-content-updated.html 1', |
| 37 | + ) |
| 38 | +}) |
| 39 | + |
| 40 | +test('should call content hook on beforeUnmount', async ({ page }) => { |
| 41 | + await page.goto('composables/on-content-updated.html') |
| 42 | + |
| 43 | + const beforeUnmountLocator = page.locator( |
| 44 | + '.markdown-content-hooks .markdown-content-beforeUnmount', |
| 45 | + ) |
| 46 | + |
| 47 | + await page.locator('.e2e-theme-nav ul > li > a').nth(0).click() |
| 48 | + |
| 49 | + await expect(beforeUnmountLocator).toHaveText('beforeUnmount: /') |
| 50 | +}) |
| 51 | + |
| 52 | +/** |
| 53 | + * Updated hooks are only supported for use in development environments. |
| 54 | + * In CI environments, under both Linux and Windows, using Vite fails to correctly trigger hooks. |
| 55 | + */ |
| 56 | +if (IS_DEV && BUNDLER !== 'vite') { |
| 57 | + test('should call content hook on updated', async ({ page }) => { |
| 58 | + await page.goto('composables/on-content-updated.html') |
| 59 | + const updatedLocator = page.locator( |
| 60 | + '.markdown-content-hooks .markdown-content-updated', |
| 61 | + ) |
| 62 | + |
| 63 | + await updateMarkdownContent() |
| 64 | + await expect(updatedLocator).toHaveText(`updatedCount: 1`) |
| 65 | + |
| 66 | + await updateMarkdownContent() |
| 67 | + await expect(updatedLocator).toHaveText(`updatedCount: 2`) |
| 68 | + }) |
| 69 | +} |
0 commit comments