Skip to content

Commit

Permalink
test: add hmr e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 19, 2024
1 parent 484b064 commit 44fcf61
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 0 deletions.
55 changes: 55 additions & 0 deletions e2e/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,64 @@
import { defineConfig } from 'cypress'
import { fs, getDirname, path } from 'vuepress/utils'

const __dirname = getDirname(import.meta.url)
const resolveSourceMarkdownPath = (...args: string[]): string =>
path.resolve(__dirname, 'docs', ...args)

export default defineConfig({
e2e: {
baseUrl: 'http://localhost:9080',
specPattern: 'tests/**/*.cy.ts',
setupNodeEvents(on) {
on('task', {
'hmr:title': async () => {
const hmrTitleSourceMarkdownPath =
resolveSourceMarkdownPath('hmr/title.md')
const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8')
await fs.writeFile(
hmrTitleSourceMarkdownPath,
content.replace('# HMR Title', '# Updated Title'),
)
return true
},
'hmr:title:restore': async () => {
const hmrTitleSourceMarkdownPath =
resolveSourceMarkdownPath('hmr/title.md')
const content = await fs.readFile(hmrTitleSourceMarkdownPath, 'utf-8')
await fs.writeFile(
hmrTitleSourceMarkdownPath,
content.replace('# Updated Title', '# HMR Title'),
)
return true
},
'hmr:frontmatter': async () => {
const hmrFrontmatterSourceMarkdownPath =
resolveSourceMarkdownPath('hmr/frontmatter.md')
const content = await fs.readFile(
hmrFrontmatterSourceMarkdownPath,
'utf-8',
)
await fs.writeFile(
hmrFrontmatterSourceMarkdownPath,
content.replace('foo: HMR foo', 'foo: Updated foo'),
)
return true
},
'hmr:frontmatter:restore': async () => {
const hmrFrontmatterSourceMarkdownPath =
resolveSourceMarkdownPath('hmr/frontmatter.md')
const content = await fs.readFile(
hmrFrontmatterSourceMarkdownPath,
'utf-8',
)
await fs.writeFile(
hmrFrontmatterSourceMarkdownPath,
content.replace('foo: Updated foo', 'foo: HMR foo'),
)
return true
},
})
},
},
env: {
E2E_BASE: process.env.E2E_BASE ?? '/',
Expand Down
11 changes: 11 additions & 0 deletions e2e/docs/hmr/frontmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
foo: HMR foo
---

## link to title

[title](./title.md)

## rendered foo

{{ $frontmatter.foo }}
9 changes: 9 additions & 0 deletions e2e/docs/hmr/title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# HMR Title

## link to frontmatter

[frontmatter](./frontmatter.md)

## rendered title

{{ $page.title }}
24 changes: 24 additions & 0 deletions e2e/tests/hmr/frontmatter.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if (Cypress.env('E2E_COMMAND') === 'dev') {
it('should update frontmatter correctly', () => {
cy.visit('/hmr/frontmatter.html')
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'HMR foo',
)

cy.task('hmr:frontmatter')
.then(() => {
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'Updated foo',
)
})
.then(() => cy.task('hmr:frontmatter:restore'))
.then(() => {
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'HMR foo',
)
})
})
}
85 changes: 85 additions & 0 deletions e2e/tests/hmr/navigation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
if (Cypress.env('E2E_COMMAND') === 'dev') {
it('should update title and frontmatter correctly after navigation', () => {
cy.visit('/hmr/title.html')
cy.title().should('include', 'HMR Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'HMR Title',
)

// update title page
cy.task('hmr:title')
.then(() => {
cy.title().should('include', 'Updated Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'Updated Title',
)
})
// navigate to frontmatter page
.then(() => {
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'HMR foo',
)
})
// update frontmatter page
.then(() => cy.task('hmr:frontmatter'))
.then(() => {
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'Updated foo',
)
})
// navigate to title page
.then(() => {
cy.get('.e2e-theme-content #link-to-title + p > a').click()
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'Updated Title',
)
})
// restore title page
.then(() => cy.task('hmr:title:restore'))
.then(() => {
cy.title().should('include', 'HMR Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'HMR Title',
)
})
// navigate to frontmatter page
.then(() => {
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'Updated foo',
)
})
// restore frontmatter page
.then(() => cy.task('hmr:frontmatter:restore'))
.then(() => {
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'HMR foo',
)
})
// navigate to title page
.then(() => {
cy.get('.e2e-theme-content #link-to-title + p > a').click()
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'HMR Title',
)
})
// navigate to frontmatter page
.then(() => {
cy.get('.e2e-theme-content #link-to-frontmatter + p > a').click()
cy.get('.e2e-theme-content #rendered-foo + p').should(
'have.text',
'HMR foo',
)
})
})
}
27 changes: 27 additions & 0 deletions e2e/tests/hmr/title.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
if (Cypress.env('E2E_COMMAND') === 'dev') {
it('should update title correctly', () => {
cy.visit('/hmr/title.html')
cy.title().should('include', 'HMR Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'HMR Title',
)

cy.task('hmr:title')
.then(() => {
cy.title().should('include', 'Updated Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'Updated Title',
)
})
.then(() => cy.task('hmr:title:restore'))
.then(() => {
cy.title().should('include', 'HMR Title')
cy.get('.e2e-theme-content #rendered-title + p').should(
'have.text',
'HMR Title',
)
})
})
}

0 comments on commit 44fcf61

Please sign in to comment.