Skip to content

Commit 44fcf61

Browse files
committed
test: add hmr e2e tests
1 parent 484b064 commit 44fcf61

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed

e2e/cypress.config.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
import { defineConfig } from 'cypress'
2+
import { fs, getDirname, path } from 'vuepress/utils'
3+
4+
const __dirname = getDirname(import.meta.url)
5+
const resolveSourceMarkdownPath = (...args: string[]): string =>
6+
path.resolve(__dirname, 'docs', ...args)
27

38
export default defineConfig({
49
e2e: {
510
baseUrl: 'http://localhost:9080',
611
specPattern: 'tests/**/*.cy.ts',
12+
setupNodeEvents(on) {
13+
on('task', {
14+
'hmr:title': async () => {
15+
const hmrTitleSourceMarkdownPath =
16+
resolveSourceMarkdownPath('hmr/title.md')
17+
const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8')
18+
await fs.writeFile(
19+
hmrTitleSourceMarkdownPath,
20+
content.replace('# HMR Title', '# Updated Title'),
21+
)
22+
return true
23+
},
24+
'hmr:title:restore': async () => {
25+
const hmrTitleSourceMarkdownPath =
26+
resolveSourceMarkdownPath('hmr/title.md')
27+
const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8')
28+
await fs.writeFile(
29+
hmrTitleSourceMarkdownPath,
30+
content.replace('# Updated Title', '# HMR Title'),
31+
)
32+
return true
33+
},
34+
'hmr:frontmatter': async () => {
35+
const hmrFrontmatterSourceMarkdownPath =
36+
resolveSourceMarkdownPath('hmr/frontmatter.md')
37+
const content = await fs.readFile(
38+
hmrFrontmatterSourceMarkdownPath,
39+
'utf-8',
40+
)
41+
await fs.writeFile(
42+
hmrFrontmatterSourceMarkdownPath,
43+
content.replace('foo: HMR foo', 'foo: Updated foo'),
44+
)
45+
return true
46+
},
47+
'hmr:frontmatter:restore': async () => {
48+
const hmrFrontmatterSourceMarkdownPath =
49+
resolveSourceMarkdownPath('hmr/frontmatter.md')
50+
const content = await fs.readFile(
51+
hmrFrontmatterSourceMarkdownPath,
52+
'utf-8',
53+
)
54+
await fs.writeFile(
55+
hmrFrontmatterSourceMarkdownPath,
56+
content.replace('foo: Updated foo', 'foo: HMR foo'),
57+
)
58+
return true
59+
},
60+
})
61+
},
762
},
863
env: {
964
E2E_BASE: process.env.E2E_BASE ?? '/',

e2e/docs/hmr/frontmatter.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
foo: HMR foo
3+
---
4+
5+
## link to title
6+
7+
[title](./title.md)
8+
9+
## rendered foo
10+
11+
{{ $frontmatter.foo }}

e2e/docs/hmr/title.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# HMR Title
2+
3+
## link to frontmatter
4+
5+
[frontmatter](./frontmatter.md)
6+
7+
## rendered title
8+
9+
{{ $page.title }}

e2e/tests/hmr/frontmatter.cy.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
if (Cypress.env('E2E_COMMAND') === 'dev') {
2+
it('should update frontmatter correctly', () => {
3+
cy.visit('/hmr/frontmatter.html')
4+
cy.get('.e2e-theme-content #rendered-foo + p').should(
5+
'have.text',
6+
'HMR foo',
7+
)
8+
9+
cy.task('hmr:frontmatter')
10+
.then(() => {
11+
cy.get('.e2e-theme-content #rendered-foo + p').should(
12+
'have.text',
13+
'Updated foo',
14+
)
15+
})
16+
.then(() => cy.task('hmr:frontmatter:restore'))
17+
.then(() => {
18+
cy.get('.e2e-theme-content #rendered-foo + p').should(
19+
'have.text',
20+
'HMR foo',
21+
)
22+
})
23+
})
24+
}

e2e/tests/hmr/navigation.cy.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
if (Cypress.env('E2E_COMMAND') === 'dev') {
2+
it('should update title and frontmatter correctly after navigation', () => {
3+
cy.visit('/hmr/title.html')
4+
cy.title().should('include', 'HMR Title')
5+
cy.get('.e2e-theme-content #rendered-title + p').should(
6+
'have.text',
7+
'HMR Title',
8+
)
9+
10+
// update title page
11+
cy.task('hmr:title')
12+
.then(() => {
13+
cy.title().should('include', 'Updated Title')
14+
cy.get('.e2e-theme-content #rendered-title + p').should(
15+
'have.text',
16+
'Updated Title',
17+
)
18+
})
19+
// navigate to frontmatter page
20+
.then(() => {
21+
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
22+
cy.get('.e2e-theme-content #rendered-foo + p').should(
23+
'have.text',
24+
'HMR foo',
25+
)
26+
})
27+
// update frontmatter page
28+
.then(() => cy.task('hmr:frontmatter'))
29+
.then(() => {
30+
cy.get('.e2e-theme-content #rendered-foo + p').should(
31+
'have.text',
32+
'Updated foo',
33+
)
34+
})
35+
// navigate to title page
36+
.then(() => {
37+
cy.get('.e2e-theme-content #link-to-title + p > a').click()
38+
cy.get('.e2e-theme-content #rendered-title + p').should(
39+
'have.text',
40+
'Updated Title',
41+
)
42+
})
43+
// restore title page
44+
.then(() => cy.task('hmr:title:restore'))
45+
.then(() => {
46+
cy.title().should('include', 'HMR Title')
47+
cy.get('.e2e-theme-content #rendered-title + p').should(
48+
'have.text',
49+
'HMR Title',
50+
)
51+
})
52+
// navigate to frontmatter page
53+
.then(() => {
54+
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
55+
cy.get('.e2e-theme-content #rendered-foo + p').should(
56+
'have.text',
57+
'Updated foo',
58+
)
59+
})
60+
// restore frontmatter page
61+
.then(() => cy.task('hmr:frontmatter:restore'))
62+
.then(() => {
63+
cy.get('.e2e-theme-content #rendered-foo + p').should(
64+
'have.text',
65+
'HMR foo',
66+
)
67+
})
68+
// navigate to title page
69+
.then(() => {
70+
cy.get('.e2e-theme-content #link-to-title + p > a').click()
71+
cy.get('.e2e-theme-content #rendered-title + p').should(
72+
'have.text',
73+
'HMR Title',
74+
)
75+
})
76+
// navigate to frontmatter page
77+
.then(() => {
78+
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
79+
cy.get('.e2e-theme-content #rendered-foo + p').should(
80+
'have.text',
81+
'HMR foo',
82+
)
83+
})
84+
})
85+
}

e2e/tests/hmr/title.cy.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
if (Cypress.env('E2E_COMMAND') === 'dev') {
2+
it('should update title correctly', () => {
3+
cy.visit('/hmr/title.html')
4+
cy.title().should('include', 'HMR Title')
5+
cy.get('.e2e-theme-content #rendered-title + p').should(
6+
'have.text',
7+
'HMR Title',
8+
)
9+
10+
cy.task('hmr:title')
11+
.then(() => {
12+
cy.title().should('include', 'Updated Title')
13+
cy.get('.e2e-theme-content #rendered-title + p').should(
14+
'have.text',
15+
'Updated Title',
16+
)
17+
})
18+
.then(() => cy.task('hmr:title:restore'))
19+
.then(() => {
20+
cy.title().should('include', 'HMR Title')
21+
cy.get('.e2e-theme-content #rendered-title + p').should(
22+
'have.text',
23+
'HMR Title',
24+
)
25+
})
26+
})
27+
}

0 commit comments

Comments
 (0)