From 7dcdadcb738c6ce44638962a1b21a4766bc62af7 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Tue, 10 Sep 2024 12:30:58 +0800 Subject: [PATCH] test(e2e): add more test cases for hmr --- e2e/docs/hmr/content.md | 11 +++++ e2e/docs/hmr/frontmatter.md | 4 ++ e2e/docs/hmr/title.md | 4 ++ e2e/tests/hmr.spec.ts | 89 ++++++++++++++++++++++++++++++------- 4 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 e2e/docs/hmr/content.md diff --git a/e2e/docs/hmr/content.md b/e2e/docs/hmr/content.md new file mode 100644 index 0000000000..544f7baf1d --- /dev/null +++ b/e2e/docs/hmr/content.md @@ -0,0 +1,11 @@ +## link to frontmatter + +[frontmatter](./frontmatter.md) + +## link to title + +[title](./title.md) + +## content + +HMR content diff --git a/e2e/docs/hmr/frontmatter.md b/e2e/docs/hmr/frontmatter.md index 0dbbd4d595..62a7f3ad13 100644 --- a/e2e/docs/hmr/frontmatter.md +++ b/e2e/docs/hmr/frontmatter.md @@ -2,6 +2,10 @@ foo: HMR foo --- +## link to content + +[content](./content.md) + ## link to title [title](./title.md) diff --git a/e2e/docs/hmr/title.md b/e2e/docs/hmr/title.md index d1c472a01f..3294473fe3 100644 --- a/e2e/docs/hmr/title.md +++ b/e2e/docs/hmr/title.md @@ -1,5 +1,9 @@ # HMR Title +## link to content + +[content](./content.md) + ## link to frontmatter [frontmatter](./frontmatter.md) diff --git a/e2e/tests/hmr.spec.ts b/e2e/tests/hmr.spec.ts index f1040d9c02..6c4cca7bbf 100644 --- a/e2e/tests/hmr.spec.ts +++ b/e2e/tests/hmr.spec.ts @@ -2,11 +2,11 @@ import { expect, test } from '@playwright/test' import { IS_DEV } from '../utils/env' import { readSourceMarkdown, writeSourceMarkdown } from '../utils/source' -const hmrUpdateTitle = async (): Promise => { - const markdownContent = await readSourceMarkdown('hmr/title.md') +const hmrUpdateContent = async (): Promise => { + const markdownContent = await readSourceMarkdown('hmr/content.md') await writeSourceMarkdown( - 'hmr/title.md', - markdownContent.replace('# HMR Title', '# Updated Title'), + 'hmr/content.md', + markdownContent.replace('HMR content', 'Updated content'), ) } @@ -18,19 +18,32 @@ const hmrUpdateFrontmatter = async (): Promise => { ) } +const hmrUpdateTitle = async (): Promise => { + const markdownContent = await readSourceMarkdown('hmr/title.md') + await writeSourceMarkdown( + 'hmr/title.md', + markdownContent.replace('# HMR Title', '# Updated Title'), + ) +} + const hmrRestore = async (): Promise => { - const titleMarkdownContent = await readSourceMarkdown('hmr/title.md') + const contentMarkdownContent = await readSourceMarkdown('hmr/content.md') const frontmatterMarkdownContent = await readSourceMarkdown('hmr/frontmatter.md') + const titleMarkdownContent = await readSourceMarkdown('hmr/title.md') await writeSourceMarkdown( - 'hmr/title.md', - titleMarkdownContent.replace('# Updated Title', '# HMR Title'), + 'hmr/content.md', + contentMarkdownContent.replace('Updated content', 'HMR content'), ) await writeSourceMarkdown( 'hmr/frontmatter.md', frontmatterMarkdownContent.replace('foo: Updated foo', 'foo: HMR foo'), ) + await writeSourceMarkdown( + 'hmr/title.md', + titleMarkdownContent.replace('# Updated Title', '# HMR Title'), + ) } if (IS_DEV) { @@ -41,6 +54,16 @@ if (IS_DEV) { await hmrRestore() }) + test('should update content correctly', async ({ page }) => { + const contentLocator = page.locator('.e2e-theme-content #content + p') + + await page.goto('hmr/content.html') + + await expect(contentLocator).toHaveText('HMR content') + await hmrUpdateContent() + await expect(contentLocator).toHaveText('Updated content') + }) + test('should update frontmatter correctly', async ({ page }) => { const frontmatterLocator = page.locator( '.e2e-theme-content #rendered-foo + p', @@ -54,53 +77,85 @@ if (IS_DEV) { }) test('should update title correctly', async ({ page }) => { - const titleLocator = page.locator('.e2e-theme-content #rendered-title + p') + const titleLocator = page.locator('.e2e-theme-content h1') + const renderedTitleLocator = page.locator( + '.e2e-theme-content #rendered-title + p', + ) await page.goto('hmr/title.html') await expect(page).toHaveTitle(/HMR Title/) await expect(titleLocator).toHaveText('HMR Title') + await expect(renderedTitleLocator).toHaveText('HMR Title') await hmrUpdateTitle() await expect(page).toHaveTitle(/Updated Title/) await expect(titleLocator).toHaveText('Updated Title') + await expect(renderedTitleLocator).toHaveText('Updated Title') }) - test('should update title and frontmatter correctly after navigation', async ({ + test('should update content and frontmatter and title correctly after navigation', async ({ page, }) => { - const titleLocator = page.locator('.e2e-theme-content #rendered-title + p') + // text locators + const contentLocator = page.locator('.e2e-theme-content #content + p') const frontmatterLocator = page.locator( '.e2e-theme-content #rendered-foo + p', ) + const titleLocator = page.locator('.e2e-theme-content #rendered-title + p') + const renderedTitleLocator = page.locator( + '.e2e-theme-content #rendered-title + p', + ) + // link locators + const contentPageLinkLocator = page.locator( + '.e2e-theme-content #link-to-content + p > a', + ) + const frontmatterPageLinkLocator = page.locator( + '.e2e-theme-content #link-to-frontmatter + p > a', + ) + const titlePageLinkLocator = page.locator( + '.e2e-theme-content #link-to-title + p > a', + ) + + // start from title page await page.goto('hmr/title.html') await expect(page).toHaveTitle(/HMR Title/) await expect(titleLocator).toHaveText('HMR Title') + await expect(renderedTitleLocator).toHaveText('HMR Title') // update title page await hmrUpdateTitle() await expect(page).toHaveTitle(/Updated Title/) await expect(titleLocator).toHaveText('Updated Title') + await expect(renderedTitleLocator).toHaveText('Updated Title') // navigate to frontmatter page - await page - .locator('.e2e-theme-content #link-to-frontmatter + p > a') - .click() + await frontmatterPageLinkLocator.click() await expect(frontmatterLocator).toHaveText('HMR foo') // update frontmatter page await hmrUpdateFrontmatter() await expect(frontmatterLocator).toHaveText('Updated foo') + // navigate to content page + await contentPageLinkLocator.click() + await expect(contentLocator).toHaveText('HMR content') + + // update content page + await hmrUpdateContent() + await expect(contentLocator).toHaveText('Updated content') + // navigate to back title page - await page.locator('.e2e-theme-content #link-to-title + p > a').click() + await titlePageLinkLocator.click() await expect(page).toHaveTitle(/Updated Title/) await expect(titleLocator).toHaveText('Updated Title') // navigate to back frontmatter page - await page - .locator('.e2e-theme-content #link-to-frontmatter + p > a') - .click() + await frontmatterPageLinkLocator.click() await expect(frontmatterLocator).toHaveText('Updated foo') + + // navigate to back content page + await contentPageLinkLocator.click() + await expect(contentLocator).toHaveText('Updated content') }) }