@@ -7,54 +7,51 @@ const {
7
7
readFile
8
8
} = require ( './util' )
9
9
10
- describe ( 'Page' , ( ) => {
11
- let app
12
- let computed
10
+ let app
11
+ let computed
13
12
14
- beforeAll ( async ( ) => {
15
- app = new App ( )
16
- await app . process ( )
17
- computed = new app . ClientComputedMixinConstructor ( )
18
- } )
13
+ beforeAll ( async ( ) => {
14
+ app = new App ( )
15
+ await app . process ( )
16
+ computed = new app . ClientComputedMixinConstructor ( )
17
+ } )
19
18
20
- test ( 'pure route' , async ( ) => {
21
- const page = new Page ( { path : '/' } , app )
19
+ async function setupPage ( options , processOption = { } ) {
20
+ const page = new Page ( options , app )
21
+ await page . process ( { computed, ...processOption } )
22
+ return page
23
+ }
22
24
23
- expect ( page . path ) . toBe ( '/' )
24
- expect ( page . regularPath ) . toBe ( '/' )
25
-
26
- await page . process ( { computed } )
25
+ describe ( 'pure route' , ( ) => {
26
+ test ( 'should get pure route' , async ( ) => {
27
+ const page = await setupPage ( { path : '/' } )
27
28
28
29
expect ( page . path ) . toBe ( '/' )
29
30
expect ( page . regularPath ) . toBe ( '/' )
31
+ expect ( page . frontmatter ) . toEqual ( { } )
30
32
} )
31
33
32
- test ( 'pure route - encodeURI ' , async ( ) => {
34
+ test ( 'should encode the path ' , async ( ) => {
33
35
const path = '/尤/'
34
- const page = new Page ( { path } , app )
36
+ const encodePath = encodeURI ( path )
37
+ const page = await setupPage ( { path } )
35
38
36
- expect ( page . path ) . toBe ( encodeURI ( path ) )
37
- expect ( page . regularPath ) . toBe ( encodeURI ( path ) )
39
+ expect ( page . path ) . toBe ( encodePath )
40
+ expect ( page . regularPath ) . toBe ( encodePath )
38
41
} )
39
42
40
- test ( 'pure route - custom frontmatter' , async ( ) => {
41
- const frontmatter = { title : 'alpha' }
42
- const page = new Page ( {
43
+ test ( 'should be able to set custom frontmatter' , async ( ) => {
44
+ const frontmatter = { foo : 'alpha' }
45
+ const page = await setupPage ( {
43
46
path : '/' ,
44
47
frontmatter
45
- } , app )
46
- expect ( page . frontmatter ) . toBe ( frontmatter )
48
+ } )
49
+
50
+ expect ( page . frontmatter . foo ) . toBe ( frontmatter . foo )
47
51
} )
48
52
49
- test ( 'pure route - enhancers' , async ( ) => {
53
+ test ( 'should be able to use enhancers' , async ( ) => {
50
54
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
55
const enhancers = [
59
56
{
60
57
name : 'plugin-a' ,
@@ -63,91 +60,240 @@ describe('Page', () => {
63
60
}
64
61
}
65
62
]
66
- await page . process ( { computed , enhancers } )
63
+ const page = await setupPage ( { path : '/' , frontmatter } , { enhancers } )
67
64
68
65
expect ( page . frontmatter . title ) . toBe ( 'beta' )
69
66
} )
67
+ } )
68
+
69
+ describe ( 'permalink' , ( ) => {
70
+ test ( 'should be able to set permalink' , async ( ) => {
71
+ const page = await setupPage ( { permalink : '/permalink' } )
72
+
73
+ expect ( page . path ) . toBe ( '/permalink' )
74
+ expect ( page . regularPath ) . toBe ( '/permalink' )
75
+ } )
76
+
77
+ test ( 'should be able to set permalink from frontmatter' , async ( ) => {
78
+ const frontmatter = { permalink : '/permalink' }
79
+ const page = await setupPage ( { path : '/' , frontmatter } )
80
+
81
+ expect ( page . path ) . toBe ( '/permalink/' )
82
+ expect ( page . regularPath ) . toBe ( '/' )
83
+ } )
84
+
85
+ test ( 'should be able to set permalink from global pattern' , async ( ) => {
86
+ const permalinkPattern = '/:year/:month/:day/:slug'
87
+ const { relative, filePath } = getDocument ( '2020-01-01-date.md' )
88
+ const markdown = getMarkdown ( )
89
+ const page = await setupPage ( { filePath, relative, permalinkPattern } , { markdown } )
90
+ expect ( page . path ) . toBe ( '/2020/01/01/date/' )
91
+ expect ( page . regularPath ) . toBe ( '/2020-01-01-date.html' )
92
+
93
+ const pageWithLocalePath = await setupPage (
94
+ { filePath, relative, permalinkPattern } ,
95
+ { computed : { setPage ( ) { } , $localePath : '/zh/' } , markdown }
96
+ )
97
+ expect ( pageWithLocalePath . path ) . toBe ( '/zh/2020/01/01/date/' )
98
+ } )
99
+ } )
70
100
71
- test ( 'markdown page - pointing to a markdown file' , async ( ) => {
101
+ describe ( 'markdown page' , ( ) => {
102
+ test ( 'should be able to pointing to a markdown file' , async ( ) => {
72
103
const { relative, filePath } = getDocument ( 'README.md' )
73
- const page = new Page ( { filePath, relative } , app )
104
+ const markdown = getMarkdown ( )
105
+ const page = await setupPage ( { filePath, relative } , { markdown } )
74
106
75
107
expect ( page . _filePath ) . toBe ( filePath )
76
108
expect ( page . regularPath ) . toBe ( '/' )
77
109
expect ( page . path ) . toBe ( '/' )
78
110
expect ( page . frontmatter ) . toEqual ( { } )
79
111
80
- const markdown = getMarkdown ( )
81
- await page . process ( { computed, markdown } )
112
+ const content = await readFile ( filePath )
113
+
114
+ expect ( page . _content ) . toBe ( content )
115
+ expect ( page . _strippedContent ) . toBe ( content )
116
+ } )
82
117
83
- expect ( page . title ) . toBe ( 'Home' )
118
+ test ( 'should be able add a page with explicit content' , async ( ) => {
119
+ const { filePath } = getDocument ( 'README.md' )
84
120
const content = await readFile ( filePath )
121
+ const markdown = getMarkdown ( )
122
+ const page = await setupPage ( { content } , { markdown } )
123
+
85
124
expect ( page . _content ) . toBe ( content )
86
125
expect ( page . _strippedContent ) . toBe ( content )
87
126
} )
88
127
89
- test ( 'markdown page - pointing to a markdown file with frontmatter ' , async ( ) => {
128
+ test ( 'should work with frontmatter when pointing to a markdown file' , async ( ) => {
90
129
const { relative, filePath } = getDocument ( 'alpha.md' )
91
- const page = new Page ( { filePath, relative } , app )
130
+ const title = 'VuePress Alpha' // from fixture
131
+ const markdown = getMarkdown ( )
132
+ const page = await setupPage ( { filePath, relative } , { markdown } )
92
133
93
134
expect ( page . _filePath ) . toBe ( filePath )
94
135
expect ( page . regularPath ) . toBe ( '/alpha.html' )
95
136
expect ( page . path ) . toBe ( '/alpha.html' )
96
- expect ( page . frontmatter ) . toEqual ( { } )
137
+ expect ( page . frontmatter . title ) . toBe ( title )
138
+ expect ( page . _content . startsWith ( '---' ) ) . toBe ( true )
139
+ expect ( page . _strippedContent . startsWith ( '---' ) ) . toBe ( false )
140
+ } )
97
141
142
+ test ( 'should extract any content above <!-- more --> as excerpt' , async ( ) => {
143
+ const { relative, filePath } = getDocument ( 'excerpt.md' )
98
144
const markdown = getMarkdown ( )
99
- await page . process ( { computed , markdown } )
145
+ const page = await setupPage ( { filePath , relative } , { markdown } )
100
146
101
- expect ( page . title ) . toBe ( page . frontmatter . title )
102
- expect ( page . _content . startsWith ( '---' ) ) . toBe ( true )
103
- expect ( page . _strippedContent . startsWith ( '---' ) ) . toBe ( false )
147
+ expect ( page . excerpt ) . toMatchSnapshot ( )
104
148
} )
105
149
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
- } )
150
+ test ( 'should extract level 2 and 3 headers by default' , async ( ) => {
151
+ const { relative, filePath } = getDocument ( 'alpha.md' )
152
+ const markdown = getMarkdown ( )
153
+ const page = await setupPage ( { filePath, relative } , { markdown } )
124
154
125
- test ( 'should loop over sync enhancers' , async ( ) => {
126
- await page . enhance ( enhancers )
155
+ expect ( page . headers ) . toMatchSnapshot ( )
156
+ } )
127
157
128
- return enhancers . map ( enhancer => expect ( enhancer . value ) . toHaveBeenCalled ( ) )
129
- } )
158
+ test ( 'should extract headers by config' , async ( ) => {
159
+ const { relative, filePath } = getDocument ( 'alpha.md' )
160
+ const markdown = getMarkdown ( )
161
+ const extractHeaders = [ 'h1' , 'h2' ]
162
+ const page = await setupPage ( { filePath, relative, extractHeaders } , { markdown } )
130
163
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 )
164
+ expect ( page . headers ) . toMatchSnapshot ( )
165
+ } )
166
+ } )
137
167
138
- return mixedEnhancers . map ( enhancer => expect ( enhancer . value ) . toHaveBeenCalled ( ) )
139
- } )
168
+ describe ( 'title' , ( ) => {
169
+ test ( 'should be able to set title' , async ( ) => {
170
+ const title = 'VuePress'
171
+ const page = await setupPage ( { path : '/' , title } )
172
+ expect ( page . title ) . toBe ( title )
173
+ } )
140
174
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'
175
+ test ( 'should set title from frontmatter' , async ( ) => {
176
+ const title = 'VuePress Alpha' // from fixture
177
+ const { relative, filePath } = getDocument ( 'alpha.md' )
178
+ const markdown = getMarkdown ( )
179
+ const page = await setupPage ( { filePath, relative } , { markdown } )
180
+ expect ( page . title ) . toBe ( title )
181
+ } )
144
182
145
- await expect ( page . enhance ( [ {
146
- name : pluginName ,
147
- value : jest . fn ( ) . mockRejectedValue ( error )
148
- } ] ) ) . rejects . toThrowError ( `[${ pluginName } ] execute extendPageData failed.` )
183
+ test ( 'should use first header in markdown to set title ' , async ( ) => {
184
+ const title = 'Home' // from fixture
185
+ const { relative, filePath } = getDocument ( 'README.md' )
186
+ const markdown = getMarkdown ( )
187
+ const page = await setupPage ( { filePath, relative } , { markdown } )
188
+ expect ( page . title ) . toBe ( title )
189
+ } )
190
+ } )
149
191
150
- expect ( console . log ) . toHaveBeenCalledWith ( error )
151
- } )
192
+ describe ( 'enhancer' , ( ) => {
193
+ test ( 'should loop over sync enhancers' , async ( ) => {
194
+ const page = await setupPage ( { path : '/' } )
195
+ const enhancers = [
196
+ {
197
+ name : 'foo' ,
198
+ value : jest . fn ( )
199
+ } ,
200
+ {
201
+ name : 'foo' ,
202
+ value : jest . fn ( )
203
+ }
204
+ ]
205
+ await page . enhance ( enhancers )
206
+
207
+ return enhancers . map ( enhancer => expect ( enhancer . value ) . toHaveBeenCalled ( ) )
208
+ } )
209
+
210
+ test ( 'should loop over sync and async enhancers' , async ( ) => {
211
+ const page = await setupPage ( { path : '/' } )
212
+ const enhancers = [
213
+ {
214
+ name : 'foo' ,
215
+ value : jest . fn ( )
216
+ } ,
217
+ {
218
+ name : 'foo' ,
219
+ value : jest . fn ( )
220
+ }
221
+ ]
222
+ const mixedEnhancers = [ ...enhancers , {
223
+ name : 'blog' ,
224
+ value : jest . fn ( ) . mockResolvedValue ( { } )
225
+ } ]
226
+ await page . enhance ( mixedEnhancers )
227
+
228
+ return mixedEnhancers . map ( enhancer => expect ( enhancer . value ) . toHaveBeenCalled ( ) )
229
+ } )
230
+
231
+ test ( 'should log and throw an error when enhancing fails' , async ( ) => {
232
+ global . console . log = jest . fn ( )
233
+ const pluginName = 'error-plugin'
234
+
235
+ const page = await setupPage ( { path : '/' } )
236
+ const error = { errorMessage : 'this is an error message' }
237
+
238
+ await expect ( page . enhance ( [ {
239
+ name : pluginName ,
240
+ value : jest . fn ( ) . mockRejectedValue ( error )
241
+ } ] ) ) . rejects . toThrowError ( `[${ pluginName } ] execute extendPageData failed.` )
242
+
243
+ expect ( console . log ) . toHaveBeenCalledWith ( error )
152
244
} )
153
245
} )
246
+
247
+ describe ( 'public api' , ( ) => {
248
+ test ( 'dirname' , async ( ) => {
249
+ const dirname = 'docs'
250
+ const { relative, filePath } = getDocument ( 'README.md' )
251
+ const markdown = getMarkdown ( )
252
+ const page = await setupPage ( { filePath, relative } , { markdown } )
253
+ expect ( page . dirname ) . toBe ( dirname )
254
+ } )
255
+
256
+ test ( 'filename' , async ( ) => {
257
+ const filename = 'README'
258
+ const { relative, filePath } = getDocument ( `${ filename } .md` )
259
+ const markdown = getMarkdown ( )
260
+ const page = await setupPage ( { filePath, relative } , { markdown } )
261
+ expect ( page . filename ) . toBe ( filename )
262
+ } )
263
+
264
+ test ( 'slug' , async ( ) => {
265
+ const markdown = getMarkdown ( )
266
+ const dirname = 'docs'
267
+ const indexPageFixture = getDocument ( 'README.md' )
268
+ const indexPage = await setupPage (
269
+ { filePath : indexPageFixture . filePath , relative : indexPageFixture . relative } , { markdown }
270
+ )
271
+ expect ( indexPage . slug ) . toBe ( dirname )
272
+
273
+ const filename = 'alpha'
274
+ const pageFixture = getDocument ( `${ filename } .md` )
275
+ const page = await setupPage (
276
+ { filePath : pageFixture . filePath , relative : pageFixture . relative } , { markdown }
277
+ )
278
+ expect ( page . slug ) . toBe ( filename )
279
+ } )
280
+
281
+ test ( 'strippedFilename' , async ( ) => {
282
+ const { relative, filePath } = getDocument ( '2020-01-01-date.md' )
283
+ const markdown = getMarkdown ( )
284
+ const page = await setupPage ( { filePath, relative } , { markdown } )
285
+ expect ( page . strippedFilename ) . toBe ( 'date' )
286
+ } )
287
+
288
+ test ( 'date' , async ( ) => {
289
+ const frontmatter = { date : '2020-01-01' }
290
+ const dateInFrontmatterPage = await setupPage ( { path : '/' , frontmatter } )
291
+ expect ( dateInFrontmatterPage . date ) . toBe ( '2020-01-01' )
292
+
293
+ const { relative, filePath } = getDocument ( '2020-01-01-date.md' )
294
+ const markdown = getMarkdown ( )
295
+ const dateInPathPage = await setupPage ( { filePath, relative } , { markdown } )
296
+ expect ( dateInPathPage . date ) . toBe ( '2020-01-01' )
297
+ } )
298
+ } )
299
+
0 commit comments