Skip to content

Commit 440010c

Browse files
committed
test: sort out core - Page
1 parent 76da780 commit 440010c

File tree

1 file changed

+134
-123
lines changed
  • packages/@vuepress/core/lib/node/__tests__/prepare

1 file changed

+134
-123
lines changed

packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js

Lines changed: 134 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -7,147 +7,158 @@ const {
77
readFile
88
} = require('./util')
99

10-
describe('Page', () => {
11-
let app
12-
let computed
13-
14-
beforeAll(async () => {
15-
app = new App()
16-
await app.process()
17-
computed = new app.ClientComputedMixinConstructor()
18-
})
10+
let app
11+
let computed
1912

20-
test('pure route', async () => {
21-
const page = new Page({ path: '/' }, app)
13+
beforeAll(async () => {
14+
app = new App()
15+
await app.process()
16+
computed = new app.ClientComputedMixinConstructor()
17+
})
2218

23-
expect(page.path).toBe('/')
24-
expect(page.regularPath).toBe('/')
19+
async function setupPage (options, processOption = {}) {
20+
const page = new Page(options, app)
21+
await page.process({ computed, ...processOption })
22+
return page
23+
}
2524

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

28-
expect(page.path).toBe('/')
29-
expect(page.regularPath).toBe('/')
30-
})
28+
expect(page.path).toBe('/')
29+
expect(page.regularPath).toBe('/')
30+
expect(page.frontmatter).toEqual({})
31+
})
3132

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

36-
expect(page.path).toBe(encodeURI(path))
37-
expect(page.regularPath).toBe(encodeURI(path))
38-
})
37+
expect(page.path).toBe(encodeURI(path))
38+
expect(page.regularPath).toBe(encodeURI(path))
39+
expect(page.frontmatter).toEqual({})
40+
})
3941

40-
test('pure route - custom frontmatter', async () => {
41-
const frontmatter = { title: 'alpha' }
42-
const page = new Page({
43-
path: '/',
44-
frontmatter
45-
}, app)
46-
expect(page.frontmatter).toBe(frontmatter)
42+
test('pure route - custom frontmatter', async () => {
43+
const frontmatter = { title: 'alpha' }
44+
const page = await setupPage({
45+
path: '/',
46+
frontmatter
4747
})
4848

49-
test('pure route - enhancers', async () => {
50-
const frontmatter = { title: 'alpha' }
51-
const page = new Page({
52-
path: '/',
53-
frontmatter
54-
}, app)
55-
56-
expect(page.frontmatter.title).toBe('alpha')
57-
58-
const enhancers = [
59-
{
60-
name: 'plugin-a',
61-
value: page => {
62-
page.frontmatter.title = 'beta'
63-
}
49+
expect(page.frontmatter.title).toBe(frontmatter.title)
50+
})
51+
52+
test('pure route - enhancers', async () => {
53+
const frontmatter = { title: 'alpha' }
54+
const enhancers = [
55+
{
56+
name: 'plugin-a',
57+
value: page => {
58+
page.frontmatter.title = 'beta'
6459
}
65-
]
66-
await page.process({ computed, enhancers })
60+
}
61+
]
62+
const page = await setupPage({ path: '/', frontmatter }, { enhancers })
6763

68-
expect(page.frontmatter.title).toBe('beta')
69-
})
64+
expect(page.frontmatter.title).toBe('beta')
65+
})
7066

71-
test('markdown page - pointing to a markdown file', async () => {
72-
const { relative, filePath } = getDocument('README.md')
73-
const page = new Page({ filePath, relative }, app)
67+
test('markdown page - pointing to a markdown file', async () => {
68+
const { relative, filePath } = getDocument('README.md')
69+
const markdown = getMarkdown()
70+
const page = await setupPage({ filePath, relative }, { markdown })
7471

75-
expect(page._filePath).toBe(filePath)
76-
expect(page.regularPath).toBe('/')
77-
expect(page.path).toBe('/')
78-
expect(page.frontmatter).toEqual({})
72+
expect(page._filePath).toBe(filePath)
73+
expect(page.regularPath).toBe('/')
74+
expect(page.path).toBe('/')
75+
expect(page.frontmatter).toEqual({})
7976

80-
const markdown = getMarkdown()
81-
await page.process({ computed, markdown })
77+
const content = await readFile(filePath)
8278

83-
expect(page.title).toBe('Home')
84-
const content = await readFile(filePath)
85-
expect(page._content).toBe(content)
86-
expect(page._strippedContent).toBe(content)
87-
})
79+
expect(page._content).toBe(content)
80+
expect(page._strippedContent).toBe(content)
81+
})
82+
83+
test('markdown page - pointing to a markdown file with frontmatter', async () => {
84+
const { relative, filePath } = getDocument('alpha.md')
85+
const title = 'VuePress Alpha' // from fixture
86+
const markdown = getMarkdown()
87+
const page = await setupPage({ filePath, relative }, { markdown })
88+
89+
expect(page._filePath).toBe(filePath)
90+
expect(page.regularPath).toBe('/alpha.html')
91+
expect(page.path).toBe('/alpha.html')
92+
expect(page.frontmatter.title).toBe(title)
93+
expect(page._content.startsWith('---')).toBe(true)
94+
expect(page._strippedContent.startsWith('---')).toBe(false)
95+
})
96+
97+
test('enhancer - should loop over sync enhancers', async () => {
98+
const page = await setupPage({ path: '/' })
99+
const enhancers = [
100+
{
101+
name: 'foo',
102+
value: jest.fn()
103+
},
104+
{
105+
name: 'foo',
106+
value: jest.fn()
107+
}
108+
]
109+
await page.enhance(enhancers)
110+
111+
return enhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
112+
})
88113

89-
test('markdown page - pointing to a markdown file with frontmatter', async () => {
90-
const { relative, filePath } = getDocument('alpha.md')
91-
const page = new Page({ filePath, relative }, app)
114+
test('enhancer - should loop over sync and async enhancers', async () => {
115+
const page = await setupPage({ path: '/' })
116+
const enhancers = [
117+
{
118+
name: 'foo',
119+
value: jest.fn()
120+
},
121+
{
122+
name: 'foo',
123+
value: jest.fn()
124+
}
125+
]
126+
const mixedEnhancers = [...enhancers, {
127+
name: 'blog',
128+
value: jest.fn().mockResolvedValue({})
129+
}]
130+
await page.enhance(mixedEnhancers)
131+
132+
return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
133+
})
92134

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

98-
const markdown = getMarkdown()
99-
await page.process({ computed, markdown })
139+
const page = await setupPage({ path: '/' })
140+
const error = { errorMessage: 'this is an error message' }
100141

101-
expect(page.title).toBe(page.frontmatter.title)
102-
expect(page._content.startsWith('---')).toBe(true)
103-
expect(page._strippedContent.startsWith('---')).toBe(false)
104-
})
142+
await expect(page.enhance([{
143+
name: pluginName,
144+
value: jest.fn().mockRejectedValue(error)
145+
}])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`)
105146

106-
describe('enhance - ', () => {
107-
let page
108-
let enhancers
109-
110-
beforeEach(() => {
111-
page = new Page({ path: '/' }, app)
112-
enhancers = [
113-
{
114-
name: 'foo',
115-
value: jest.fn()
116-
},
117-
{
118-
name: 'bar',
119-
value: jest.fn()
120-
}
121-
]
122-
global.console.log = jest.fn()
123-
})
124-
125-
test('should loop over sync enhancers', async () => {
126-
await page.enhance(enhancers)
127-
128-
return enhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
129-
})
130-
131-
test('should loop over sync and async enhancers', async () => {
132-
const mixedEnhancers = [...enhancers, {
133-
name: 'blog',
134-
value: jest.fn().mockResolvedValue({})
135-
}]
136-
await page.enhance(mixedEnhancers)
137-
138-
return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
139-
})
140-
141-
test('should log and throw an error when enhancing fails', async () => {
142-
const error = { errorMessage: 'this is an error message' }
143-
const pluginName = 'error-plugin'
144-
145-
await expect(page.enhance([{
146-
name: pluginName,
147-
value: jest.fn().mockRejectedValue(error)
148-
}])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`)
149-
150-
expect(console.log).toHaveBeenCalledWith(error)
151-
})
152-
})
147+
expect(console.log).toHaveBeenCalledWith(error)
148+
// TODO should throw error
153149
})
150+
151+
// TODO Permalink
152+
// TODO Title
153+
// TODO I18n
154+
// TODO Meta
155+
// TODO Add a page with explicit content
156+
// TODO Excerpt
157+
// TODO SFC
158+
// TODO Headers
159+
160+
// TODO get date
161+
// TODO get strippedFilename
162+
// TODO get slug
163+
// TODO get filename
164+
// TODO get dirname

0 commit comments

Comments
 (0)