Skip to content

Commit f3beba6

Browse files
authored
test: set up test for core (#480)
1 parent d53807e commit f3beba6

37 files changed

+1582
-111
lines changed

.babelrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"env": {
3+
"test": {
4+
"presets": [
5+
["@babel/preset-env", { "targets": { "node": 8 }}]
6+
]
7+
}
8+
}
9+
}

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = {
22
root: true,
33
extends: [
4-
'plugin:vue-libs/recommended'
4+
'plugin:vue-libs/recommended',
5+
'plugin:jest/recommended'
56
],
67
rules: {
78
indent: ['error', 2, { MemberExpression: 'off' }]

lib/app/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (module.hot) {
2626
Vue.config.productionTip = false
2727
Vue.use(Router)
2828
// mixin for exposing $site and $page
29-
Vue.mixin(dataMixin)
29+
Vue.mixin(dataMixin(siteData))
3030
// component for rendering markdown content and setting title etc.
3131
Vue.component('Content', Content)
3232
Vue.component('OutboundLink', OutboundLink)

lib/app/dataMixin.js

+69-68
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,75 @@
11
import Vue from 'vue'
2-
import { siteData } from './.temp/siteData'
32
import { findPageForPath } from './util'
43

4+
export default function dataMixin (siteData) {
5+
prepare(siteData)
6+
const store = new Vue({
7+
data: { siteData }
8+
})
9+
10+
if (module.hot) {
11+
module.hot.accept('./.temp/siteData', () => {
12+
prepare(siteData)
13+
store.siteData = siteData
14+
})
15+
}
16+
17+
return {
18+
computed: {
19+
$site () {
20+
return store.siteData
21+
},
22+
$localeConfig () {
23+
const { locales = {}} = this.$site
24+
let targetLang
25+
let defaultLang
26+
for (const path in locales) {
27+
if (path === '/') {
28+
defaultLang = locales[path]
29+
} else if (this.$page.path.indexOf(path) === 0) {
30+
targetLang = locales[path]
31+
}
32+
}
33+
return targetLang || defaultLang || {}
34+
},
35+
$siteTitle () {
36+
return this.$localeConfig.title || this.$site.title || ''
37+
},
38+
$title () {
39+
const page = this.$page
40+
const siteTitle = this.$siteTitle
41+
const selfTitle = page.frontmatter.home ? null : (
42+
page.frontmatter.title || // explicit title
43+
page.title // inferred title
44+
)
45+
return siteTitle
46+
? selfTitle
47+
? (siteTitle + ' | ' + selfTitle)
48+
: siteTitle
49+
: selfTitle || 'VuePress'
50+
},
51+
$description () {
52+
return this.$page.frontmatter.description || this.$localeConfig.description || this.$site.description || ''
53+
},
54+
$lang () {
55+
return this.$page.frontmatter.lang || this.$localeConfig.lang || 'en-US'
56+
},
57+
$localePath () {
58+
return this.$localeConfig.path || '/'
59+
},
60+
$themeLocaleConfig () {
61+
return (this.$site.themeConfig.locales || {})[this.$localePath] || {}
62+
},
63+
$page () {
64+
return findPageForPath(
65+
this.$site.pages,
66+
this.$route.path
67+
)
68+
}
69+
}
70+
}
71+
}
72+
573
function prepare (siteData) {
674
siteData.pages.forEach(page => {
775
if (!page.frontmatter) {
@@ -15,70 +83,3 @@ function prepare (siteData) {
1583
}
1684
Object.freeze(siteData)
1785
}
18-
19-
prepare(siteData)
20-
const store = new Vue({
21-
data: { siteData }
22-
})
23-
24-
if (module.hot) {
25-
module.hot.accept('./.temp/siteData', () => {
26-
prepare(siteData)
27-
store.siteData = siteData
28-
})
29-
}
30-
31-
export default {
32-
computed: {
33-
$site () {
34-
return store.siteData
35-
},
36-
$localeConfig () {
37-
const { locales = {}} = this.$site
38-
let targetLang
39-
let defaultLang
40-
for (const path in locales) {
41-
if (path === '/') {
42-
defaultLang = locales[path]
43-
} else if (this.$page.path.indexOf(path) === 0) {
44-
targetLang = locales[path]
45-
}
46-
}
47-
return targetLang || defaultLang || {}
48-
},
49-
$siteTitle () {
50-
return this.$localeConfig.title || this.$site.title || ''
51-
},
52-
$title () {
53-
const page = this.$page
54-
const siteTitle = this.$siteTitle
55-
const selfTitle = page.frontmatter.home ? null : (
56-
page.frontmatter.title || // explicit title
57-
page.title // inferred title
58-
)
59-
return siteTitle
60-
? selfTitle
61-
? (siteTitle + ' | ' + selfTitle)
62-
: siteTitle
63-
: selfTitle || 'VuePress'
64-
},
65-
$description () {
66-
return this.$page.frontmatter.description || this.$localeConfig.description || this.$site.description || ''
67-
},
68-
$lang () {
69-
return this.$page.frontmatter.lang || this.$localeConfig.lang || 'en-US'
70-
},
71-
$localePath () {
72-
return this.$localeConfig.path || '/'
73-
},
74-
$themeLocaleConfig () {
75-
return (this.$site.themeConfig.locales || {})[this.$localePath] || {}
76-
},
77-
$page () {
78-
return findPageForPath(
79-
this.$site.pages,
80-
this.$route.path
81-
)
82-
}
83-
}
84-
}

lib/markdown/index.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const emoji = require('markdown-it-emoji')
1010
const anchor = require('markdown-it-anchor')
1111
const toc = require('markdown-it-table-of-contents')
1212
const _slugify = require('./slugify')
13-
const { parseHeaders } = require('../util')
13+
const parseHeaders = require('../util/parseHeaders')
1414

1515
module.exports = ({ markdown = {}} = {}) => {
1616
// allow user config slugify
@@ -54,6 +54,15 @@ module.exports = ({ markdown = {}} = {}) => {
5454
md.use(lineNumbers)
5555
}
5656

57+
module.exports.dataReturnable(md)
58+
59+
// expose slugify
60+
md.slugify = slugify
61+
62+
return md
63+
}
64+
65+
module.exports.dataReturnable = function dataReturnable (md) {
5766
// override render to allow custom plugins return data
5867
const render = md.render
5968
md.render = (...args) => {
@@ -64,9 +73,4 @@ module.exports = ({ markdown = {}} = {}) => {
6473
data: md.__data
6574
}
6675
}
67-
68-
// expose slugify
69-
md.slugify = slugify
70-
71-
return md
7276
}

lib/util/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ exports.encodePath = path => {
55
return path.split('/').map(item => encodeURIComponent(item)).join('/')
66
}
77

8-
exports.parseHeaders = parseHeaders
9-
108
exports.normalizeHeadTag = tag => {
119
if (typeof tag === 'string') {
1210
tag = [tag]

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"dev": "node bin/vuepress dev docs",
1111
"build": "node bin/vuepress build docs",
1212
"lint": "eslint --fix --ext .js,.vue bin/ lib/ test/",
13-
"prepublishOnly": "conventional-changelog -p angular -r 2 -i CHANGELOG.md -s"
13+
"prepublishOnly": "conventional-changelog -p angular -r 2 -i CHANGELOG.md -s",
14+
"test": "node test/prepare.js && jest --config test/jest.config.js"
1415
},
1516
"repository": {
1617
"type": "git",
@@ -37,10 +38,10 @@
3738
]
3839
},
3940
"dependencies": {
40-
"autoprefixer": "^8.2.0",
4141
"@babel/core": "7.0.0-beta.47",
42-
"babel-loader": "8.0.0-beta.3",
4342
"@vue/babel-preset-app": "3.0.0-beta.11",
43+
"autoprefixer": "^8.2.0",
44+
"babel-loader": "8.0.0-beta.3",
4445
"cache-loader": "^1.2.2",
4546
"chalk": "^2.3.2",
4647
"chokidar": "^2.0.3",
@@ -94,10 +95,17 @@
9495
"workbox-build": "^3.1.0"
9596
},
9697
"devDependencies": {
98+
"@vue/test-utils": "^1.0.0-beta.16",
99+
"babel-core": "^7.0.0-0",
100+
"babel-jest": "^23.0.0",
97101
"conventional-changelog-cli": "^1.3.22",
98102
"eslint": "^4.19.1",
103+
"eslint-plugin-jest": "^21.15.1",
99104
"eslint-plugin-vue-libs": "^3.0.0",
105+
"jest": "^23.0.0",
106+
"jest-serializer-vue": "^1.0.0",
100107
"lint-staged": "^7.0.4",
108+
"vue-jest": "^2.6.0",
101109
"vuepress-theme-vue": "^1.0.2",
102110
"yorkie": "^1.0.3"
103111
},

test/app/Content.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Content from '@/app/Content.js'
2+
import { mount } from '@vue/test-utils'
3+
import { getRouter, modeTestRunner } from '../util'
4+
5+
function test (mode, localVue) {
6+
it(`${mode} - add custom class by default.`, () => {
7+
const wrapper = mount(Content, {
8+
localVue,
9+
router: getRouter()
10+
})
11+
expect(wrapper.contains('.custom')).toBe(true)
12+
})
13+
14+
it(`${mode} - remove custom when custom set to false.`, () => {
15+
const wrapper = mount(Content, {
16+
// https://vue-test-utils.vuejs.org/api/options.html#context
17+
context: {
18+
props: {
19+
custom: false
20+
}
21+
},
22+
localVue,
23+
router: getRouter()
24+
})
25+
expect(wrapper.contains('.custom')).toBe(false)
26+
})
27+
}
28+
29+
modeTestRunner('Content', test)
30+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { mount, RouterLinkStub } from '@vue/test-utils'
2+
import DropdownLink from '@/default-theme/DropdownLink.vue'
3+
import { modeTestRunner } from '../util'
4+
5+
function test (mode, localVue) {
6+
it(`$${mode} - renders dropdown link.`, () => {
7+
const item = {
8+
text: 'VuePress',
9+
items: [
10+
{
11+
text: 'Guide',
12+
link: '/guide/'
13+
},
14+
{
15+
text: 'Config Reference',
16+
link: '/config/'
17+
}
18+
]
19+
}
20+
const wrapper = mount(DropdownLink, {
21+
localVue,
22+
stubs: {
23+
'router-link': RouterLinkStub
24+
},
25+
propsData: { item }
26+
})
27+
expect(wrapper.html()).toMatchSnapshot()
28+
})
29+
}
30+
31+
modeTestRunner('DropdownLink', test)

test/default-theme/NavLink.spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { mount, RouterLinkStub } from '@vue/test-utils'
2+
import { modeTestRunner } from '../util'
3+
import NavLink from '@/default-theme/NavLink.vue'
4+
5+
function test (mode, localVue) {
6+
it(`$${mode} - renders nav link with internal link`, () => {
7+
const item = {
8+
link: '/',
9+
text: 'VuePress'
10+
}
11+
const wrapper = mount(NavLink, {
12+
localVue,
13+
stubs: {
14+
'router-link': RouterLinkStub
15+
},
16+
propsData: { item }
17+
})
18+
expect(wrapper.html()).toMatchSnapshot()
19+
})
20+
21+
it(`$${mode} - renders nav link with external link`, () => {
22+
const item = {
23+
link: 'http://vuejs.org/',
24+
text: 'Vue'
25+
}
26+
const wrapper = mount(NavLink, {
27+
localVue,
28+
propsData: { item }
29+
})
30+
expect(wrapper.html()).toMatchSnapshot()
31+
})
32+
}
33+
34+
modeTestRunner('NavLink', test)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`DropdownLink $i18n - renders dropdown link. 1`] = `
4+
<div class="dropdown-wrapper">
5+
<a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
6+
<ul class="nav-dropdown" style="display: none;" name="dropdown">
7+
<li class="dropdown-item">
8+
<!---->
9+
<a class="nav-link">Guide</a>
10+
</li>
11+
<li class="dropdown-item">
12+
<!---->
13+
<a class="nav-link">Config Reference</a>
14+
</li>
15+
</ul>
16+
</div>
17+
`;
18+
19+
exports[`DropdownLink $simple - renders dropdown link. 1`] = `
20+
<div class="dropdown-wrapper">
21+
<a class="dropdown-title"><span class="title">VuePress</span> <span class="arrow right"></span></a>
22+
<ul class="nav-dropdown" style="display: none;" name="dropdown">
23+
<li class="dropdown-item">
24+
<!---->
25+
<a class="nav-link">Guide</a>
26+
</li>
27+
<li class="dropdown-item">
28+
<!---->
29+
<a class="nav-link">Config Reference</a>
30+
</li>
31+
</ul>
32+
</div>
33+
`;

0 commit comments

Comments
 (0)