Skip to content

Commit

Permalink
test: sort out core - Page
Browse files Browse the repository at this point in the history
  • Loading branch information
billyyyyy3320 committed Mar 10, 2020
1 parent 76da780 commit 440010c
Showing 1 changed file with 134 additions and 123 deletions.
257 changes: 134 additions & 123 deletions packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,147 +7,158 @@ const {
readFile
} = require('./util')

describe('Page', () => {
let app
let computed

beforeAll(async () => {
app = new App()
await app.process()
computed = new app.ClientComputedMixinConstructor()
})
let app
let computed

test('pure route', async () => {
const page = new Page({ path: '/' }, app)
beforeAll(async () => {
app = new App()
await app.process()
computed = new app.ClientComputedMixinConstructor()
})

expect(page.path).toBe('/')
expect(page.regularPath).toBe('/')
async function setupPage (options, processOption = {}) {
const page = new Page(options, app)
await page.process({ computed, ...processOption })
return page
}

await page.process({ computed })
test('pure route', async () => {
const page = await setupPage({ path: '/' })

expect(page.path).toBe('/')
expect(page.regularPath).toBe('/')
})
expect(page.path).toBe('/')
expect(page.regularPath).toBe('/')
expect(page.frontmatter).toEqual({})
})

test('pure route - encodeURI', async () => {
const path = '/尤/'
const page = new Page({ path }, app)
test('pure route - encodeURI', async () => {
const path = '/尤/'
const page = await setupPage({ path })

expect(page.path).toBe(encodeURI(path))
expect(page.regularPath).toBe(encodeURI(path))
})
expect(page.path).toBe(encodeURI(path))
expect(page.regularPath).toBe(encodeURI(path))
expect(page.frontmatter).toEqual({})
})

test('pure route - custom frontmatter', async () => {
const frontmatter = { title: 'alpha' }
const page = new Page({
path: '/',
frontmatter
}, app)
expect(page.frontmatter).toBe(frontmatter)
test('pure route - custom frontmatter', async () => {
const frontmatter = { title: 'alpha' }
const page = await setupPage({
path: '/',
frontmatter
})

test('pure route - enhancers', async () => {
const frontmatter = { title: 'alpha' }
const page = new Page({
path: '/',
frontmatter
}, app)

expect(page.frontmatter.title).toBe('alpha')

const enhancers = [
{
name: 'plugin-a',
value: page => {
page.frontmatter.title = 'beta'
}
expect(page.frontmatter.title).toBe(frontmatter.title)
})

test('pure route - enhancers', async () => {
const frontmatter = { title: 'alpha' }
const enhancers = [
{
name: 'plugin-a',
value: page => {
page.frontmatter.title = 'beta'
}
]
await page.process({ computed, enhancers })
}
]
const page = await setupPage({ path: '/', frontmatter }, { enhancers })

expect(page.frontmatter.title).toBe('beta')
})
expect(page.frontmatter.title).toBe('beta')
})

test('markdown page - pointing to a markdown file', async () => {
const { relative, filePath } = getDocument('README.md')
const page = new Page({ filePath, relative }, app)
test('markdown page - pointing to a markdown file', async () => {
const { relative, filePath } = getDocument('README.md')
const markdown = getMarkdown()
const page = await setupPage({ filePath, relative }, { markdown })

expect(page._filePath).toBe(filePath)
expect(page.regularPath).toBe('/')
expect(page.path).toBe('/')
expect(page.frontmatter).toEqual({})
expect(page._filePath).toBe(filePath)
expect(page.regularPath).toBe('/')
expect(page.path).toBe('/')
expect(page.frontmatter).toEqual({})

const markdown = getMarkdown()
await page.process({ computed, markdown })
const content = await readFile(filePath)

expect(page.title).toBe('Home')
const content = await readFile(filePath)
expect(page._content).toBe(content)
expect(page._strippedContent).toBe(content)
})
expect(page._content).toBe(content)
expect(page._strippedContent).toBe(content)
})

test('markdown page - pointing to a markdown file with frontmatter', async () => {
const { relative, filePath } = getDocument('alpha.md')
const title = 'VuePress Alpha' // from fixture
const markdown = getMarkdown()
const page = await setupPage({ filePath, relative }, { markdown })

expect(page._filePath).toBe(filePath)
expect(page.regularPath).toBe('/alpha.html')
expect(page.path).toBe('/alpha.html')
expect(page.frontmatter.title).toBe(title)
expect(page._content.startsWith('---')).toBe(true)
expect(page._strippedContent.startsWith('---')).toBe(false)
})

test('enhancer - should loop over sync enhancers', async () => {
const page = await setupPage({ path: '/' })
const enhancers = [
{
name: 'foo',
value: jest.fn()
},
{
name: 'foo',
value: jest.fn()
}
]
await page.enhance(enhancers)

return enhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
})

test('markdown page - pointing to a markdown file with frontmatter', async () => {
const { relative, filePath } = getDocument('alpha.md')
const page = new Page({ filePath, relative }, app)
test('enhancer - should loop over sync and async enhancers', async () => {
const page = await setupPage({ path: '/' })
const enhancers = [
{
name: 'foo',
value: jest.fn()
},
{
name: 'foo',
value: jest.fn()
}
]
const mixedEnhancers = [...enhancers, {
name: 'blog',
value: jest.fn().mockResolvedValue({})
}]
await page.enhance(mixedEnhancers)

return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
})

expect(page._filePath).toBe(filePath)
expect(page.regularPath).toBe('/alpha.html')
expect(page.path).toBe('/alpha.html')
expect(page.frontmatter).toEqual({})
test('enhancer - should log and throw an error when enhancing fails', async () => {
global.console.log = jest.fn()
const pluginName = 'error-plugin'

const markdown = getMarkdown()
await page.process({ computed, markdown })
const page = await setupPage({ path: '/' })
const error = { errorMessage: 'this is an error message' }

expect(page.title).toBe(page.frontmatter.title)
expect(page._content.startsWith('---')).toBe(true)
expect(page._strippedContent.startsWith('---')).toBe(false)
})
await expect(page.enhance([{
name: pluginName,
value: jest.fn().mockRejectedValue(error)
}])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`)

describe('enhance - ', () => {
let page
let enhancers

beforeEach(() => {
page = new Page({ path: '/' }, app)
enhancers = [
{
name: 'foo',
value: jest.fn()
},
{
name: 'bar',
value: jest.fn()
}
]
global.console.log = jest.fn()
})

test('should loop over sync enhancers', async () => {
await page.enhance(enhancers)

return enhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
})

test('should loop over sync and async enhancers', async () => {
const mixedEnhancers = [...enhancers, {
name: 'blog',
value: jest.fn().mockResolvedValue({})
}]
await page.enhance(mixedEnhancers)

return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
})

test('should log and throw an error when enhancing fails', async () => {
const error = { errorMessage: 'this is an error message' }
const pluginName = 'error-plugin'

await expect(page.enhance([{
name: pluginName,
value: jest.fn().mockRejectedValue(error)
}])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`)

expect(console.log).toHaveBeenCalledWith(error)
})
})
expect(console.log).toHaveBeenCalledWith(error)
// TODO should throw error
})

// TODO Permalink
// TODO Title
// TODO I18n
// TODO Meta
// TODO Add a page with explicit content
// TODO Excerpt
// TODO SFC
// TODO Headers

// TODO get date
// TODO get strippedFilename
// TODO get slug
// TODO get filename
// TODO get dirname

0 comments on commit 440010c

Please sign in to comment.