Skip to content

Commit 037080d

Browse files
committed
test: Page class
1 parent 239867f commit 037080d

File tree

10 files changed

+161
-10
lines changed

10 files changed

+161
-10
lines changed

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
"workspaces": [
44
"packages/@vuepress/*",
55
"packages/vuepress",
6-
"packages/docs"
6+
"packages/docs",
7+
"packages/blog-example"
78
],
89
"description": "Minimalistic doc generator with Vue component based layout system",
910
"scripts": {
1011
"boot": "node scripts/bootstrap.js",
11-
"dev": "lerna exec --scope docs -- yarn dev",
12-
"build": "lerna exec --scope docs -- yarn build",
12+
"dev": "yarn workspace docs dev",
13+
"build": "yarn workspace docs build",
14+
"dev:blog-example": "yarn workspace blog-example dev",
15+
"build:blog-example": "yarn workspace blog-example build",
1316
"lint": "eslint --fix packages/**/*.js packages/**/*.vue packages/**/bin/*",
1417
"release": "yarn --pure-lockfile && node scripts/release.js",
1518
"changelog": "node scripts/genChangelog.js run",
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const Page = require('../../lib/prepare/Page')
2+
const {
3+
getComputed,
4+
getMarkdown,
5+
getDocument,
6+
readFile
7+
} = require('./util')
8+
9+
describe('Page', () => {
10+
test('pure route', async () => {
11+
const page = new Page({ path: '/' })
12+
13+
expect(page.path).toBe('/')
14+
expect(page.regularPath).toBe('/')
15+
16+
const computed = getComputed()
17+
await page.process({ computed })
18+
19+
expect(page.path).toBe('/')
20+
expect(page.regularPath).toBe('/')
21+
})
22+
23+
test('pure route - encodeURI', async () => {
24+
const path = '/尤/'
25+
const page = new Page({ path })
26+
27+
expect(page.path).toBe(encodeURI(path))
28+
expect(page.regularPath).toBe(encodeURI(path))
29+
})
30+
31+
test('pure route - custom frontmatter', async () => {
32+
const frontmatter = { title: 'alpha' }
33+
const page = new Page({
34+
path: '/',
35+
frontmatter
36+
})
37+
expect(page.frontmatter).toBe(frontmatter)
38+
})
39+
40+
test('pure route - enhancers', async () => {
41+
const frontmatter = { title: 'alpha' }
42+
const page = new Page({
43+
path: '/',
44+
frontmatter
45+
})
46+
47+
expect(page.frontmatter.title).toBe('alpha')
48+
49+
const computed = getComputed()
50+
const enhancers = [
51+
{
52+
name: 'plugin-a',
53+
value: page => { page.frontmatter.title = 'beta' }
54+
}
55+
]
56+
await page.process({ computed, enhancers })
57+
58+
expect(page.frontmatter.title).toBe('beta')
59+
})
60+
61+
test('markdown page - pointing to a markdown file', async () => {
62+
const { relative, filePath } = getDocument('README.md')
63+
const page = new Page({ filePath, relative })
64+
65+
expect(page._filePath).toBe(filePath)
66+
expect(page.regularPath).toBe('/')
67+
expect(page.path).toBe('/')
68+
expect(page.frontmatter).toEqual({})
69+
70+
const computed = getComputed()
71+
const markdown = getMarkdown()
72+
await page.process({ computed, markdown })
73+
74+
expect(page.title).toBe('Home')
75+
const content = await readFile(filePath)
76+
expect(page._content).toBe(content)
77+
expect(page._strippedContent).toBe(content)
78+
})
79+
80+
test('markdown page - pointing to a markdown file with frontmatter', async () => {
81+
const { relative, filePath } = getDocument('alpha.md')
82+
const page = new Page({ filePath, relative })
83+
84+
expect(page._filePath).toBe(filePath)
85+
expect(page.regularPath).toBe('/alpha.html')
86+
expect(page.path).toBe('/alpha.html')
87+
expect(page.frontmatter).toEqual({})
88+
89+
const computed = getComputed()
90+
const markdown = getMarkdown()
91+
await page.process({ computed, markdown })
92+
93+
expect(page.title).toBe(page.frontmatter.title)
94+
expect(page._content.startsWith('---')).toBe(true)
95+
expect(page._strippedContent.startsWith('---')).toBe(false)
96+
})
97+
})
98+

packages/@vuepress/core/__test__/prepare/fixtures/docs-simple/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Home
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: VuePress Alpha
3+
---
4+
5+
# Alpha
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const path = require('path')
2+
const { fs } = require('@vuepress/shared-utils')
3+
const AppContext = require('../../lib/prepare/AppContext')
4+
const createMarkdown = require('../../../markdown/lib/index')
5+
6+
function getAppContext () {
7+
return new AppContext('.')
8+
}
9+
10+
function getComputed () {
11+
const context = getAppContext()
12+
return new context.ClientComputedMixinConstructor()
13+
}
14+
15+
const docsBaseDir = path.resolve(__dirname, 'fixtures/docs')
16+
17+
function getDocument (relative) {
18+
return {
19+
filePath: path.join(docsBaseDir, relative),
20+
relative
21+
}
22+
}
23+
24+
const getMarkdown = createMarkdown
25+
26+
const readFile = async filePath => await fs.readFile(filePath, 'utf-8')
27+
28+
module.exports = {
29+
getAppContext,
30+
getComputed,
31+
getMarkdown,
32+
getDocument,
33+
readFile
34+
}

packages/docs/docs/plugin/README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,3 @@ See: [base](../config/README.md#base).
644644
- Type: `Function`
645645

646646
A utility for writing temporary files to tempPath.
647-
648-
649-
650-
651-
652-

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6109,6 +6109,12 @@ markdown-it-anchor@^5.0.2:
61096109
version "5.0.2"
61106110
resolved "https://registry.yarnpkg.com/markdown-it-anchor/-/markdown-it-anchor-5.0.2.tgz#cdd917a05b7bf92fb736a6dae3385c6d0d0fa552"
61116111

6112+
markdown-it-chain@^1.2.0:
6113+
version "1.2.0"
6114+
resolved "https://registry.yarnpkg.com/markdown-it-chain/-/markdown-it-chain-1.2.0.tgz#b11db6755a3d2ab3da89e9566fc3ac384f75fdac"
6115+
dependencies:
6116+
webpack-chain "^4.9.0"
6117+
61126118
markdown-it-container@^2.0.0:
61136119
version "2.0.0"
61146120
resolved "https://registry.yarnpkg.com/markdown-it-container/-/markdown-it-container-2.0.0.tgz#0019b43fd02eefece2f1960a2895fba81a404695"
@@ -8015,6 +8021,10 @@ schema-utils@^0.4.0, schema-utils@^0.4.2, schema-utils@^0.4.3, schema-utils@^0.4
80158021
ajv "^6.1.0"
80168022
ajv-keywords "^3.1.0"
80178023

8024+
scifi@^0.1.4:
8025+
version "0.1.4"
8026+
resolved "https://registry.yarnpkg.com/scifi/-/scifi-0.1.4.tgz#4d514ea11e0b2a6a350a33c6c56addcc9c4e676e"
8027+
80188028
section-matter@^1.0.0:
80198029
version "1.0.0"
80208030
resolved "https://registry.yarnpkg.com/section-matter/-/section-matter-1.0.0.tgz#e9041953506780ec01d59f292a19c7b850b84167"
@@ -9158,6 +9168,13 @@ webpack-chain@^4.6.0:
91589168
deepmerge "^1.5.2"
91599169
javascript-stringify "^1.6.0"
91609170

9171+
webpack-chain@^4.9.0:
9172+
version "4.11.0"
9173+
resolved "https://registry.yarnpkg.com/webpack-chain/-/webpack-chain-4.11.0.tgz#41b57773d2dcdcbfd43c9df28a05b40705ae421c"
9174+
dependencies:
9175+
deepmerge "^1.5.2"
9176+
javascript-stringify "^1.6.0"
9177+
91619178
webpack-dev-middleware@^3.0.0:
91629179
version "3.1.3"
91639180
resolved "https://registry.yarnpkg.com/webpack-dev-middleware/-/webpack-dev-middleware-3.1.3.tgz#8b32aa43da9ae79368c1bf1183f2b6cf5e1f39ed"

0 commit comments

Comments
 (0)