Skip to content

Commit a4b5662

Browse files
committed
Setup test environment and provide first test 🌟
1 parent fd86c72 commit a4b5662

File tree

4 files changed

+260
-4
lines changed

4 files changed

+260
-4
lines changed

package-lock.json

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"dev": "run-p serve watch:*",
2828
"dev:ssr": "run-p serve:ssr watch:*",
2929
"lint": "eslint {src,packages} --fix",
30-
"test": "run-p lint",
30+
"test": "mocha",
3131
"css": "stylus src/themes/*.styl -u autoprefixer-stylus",
3232
"watch:css": "run-p 'css -- -o themes -w'",
3333
"watch:js": "node build/build.js",
@@ -51,6 +51,7 @@
5151
},
5252
"devDependencies": {
5353
"autoprefixer-stylus": "^0.14.0",
54+
"chai": "^4.2.0",
5455
"chokidar": "^2.0.2",
5556
"conventional-changelog-cli": "^1.3.5",
5657
"cross-env": "^5.1.3",
@@ -61,6 +62,7 @@
6162
"jsdom": "^13.2.0",
6263
"lerna": "^2.5.1",
6364
"live-server": "^1.2.1",
65+
"mocha": "^5.2.0",
6466
"npm-run-all": "^4.1.5",
6567
"rimraf": "^2.6.2",
6668
"rollup": "^0.53.3",
@@ -70,7 +72,8 @@
7072
"rollup-plugin-node-resolve": "^3.0.0",
7173
"rollup-plugin-replace": "^2.0.0",
7274
"rollup-plugin-uglify": "^2.0.1",
73-
"stylus": "^0.54.5"
75+
"stylus": "^0.54.5",
76+
"xhr2": "^0.1.4"
7477
},
7578
"keywords": [
7679
"doc",

test/_loader.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,55 @@
11
require = require('esm')(module/*, options*/)
22
const {JSDOM} = require('jsdom')
3-
const dom = new JSDOM('<!DOCTYPE html><body></body>')
3+
const XMLHttpRequest = require('xhr2')
4+
// TODO: try to fix tests when using `<div id="app"></div>` in body
5+
const dom = new JSDOM('<!DOCTYPE html><html><head></head><body></body></html>')
46

57
global.window = dom.window
68
global.document = dom.window.document
79
global.navigator = dom.window.navigator
810
global.location = dom.window.location
11+
global.XMLHttpRequest = XMLHttpRequest
912

10-
require('../src/core')
13+
const {initMixin} = require('../src/core/init')
14+
const {routerMixin} = require('../src/core//router')
15+
const {renderMixin} = require('../src/core//render')
16+
const {fetchMixin} = require('../src/core/fetch')
17+
const {eventMixin} = require('../src/core//event')
18+
19+
// mimic src/core/index.js but for Node.js
20+
21+
function Docsify() {
22+
this._init()
23+
}
24+
25+
const proto = Docsify.prototype
26+
27+
initMixin(proto)
28+
routerMixin(proto)
29+
renderMixin(proto)
30+
fetchMixin(proto)
31+
eventMixin(proto)
32+
33+
function ready(callback) {
34+
const state = document.readyState
35+
36+
if (state === 'complete' || state === 'interactive') {
37+
return setTimeout(callback, 0)
38+
}
39+
40+
document.addEventListener('DOMContentLoaded', callback)
41+
}
42+
let docsify = null
43+
module.exports = function(callback) {
44+
return new Promise((resolve, reject) => {
45+
// return cached version
46+
if (docsify != null) {
47+
return resolve(docsify)
48+
}
49+
ready(_ => {
50+
docsify = new Docsify()
51+
return resolve(docsify)
52+
})
53+
54+
})
55+
}

test/render.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const path = require('path')
2+
3+
const {expect} = require('chai')
4+
5+
const loader = require('./_loader')
6+
7+
function expectSameDom(actual, expected) {
8+
const WHITESPACES_BETWEEN_TAGS = />(\s\s+)</g
9+
function replacer(match, group1) {
10+
return match.replace(group1, '')
11+
}
12+
expect(actual.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim())
13+
.equal(expected.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim())
14+
}
15+
16+
describe('render', function() {
17+
it('important content (tips)', async function() {
18+
docsify = await loader()
19+
const output = docsify.compiler.compile('!> **Time** is money, my friend!')
20+
expect(output).equal('<p class="tip"><strong>Time</strong> is money, my friend!</p>')
21+
})
22+
23+
describe('lists', function() {
24+
it('as unordered task list', async function() {
25+
docsify = await loader()
26+
const output = docsify.compiler.compile(`
27+
- [x] Task 1
28+
- [ ] Task 2
29+
- [ ] Task 3`)
30+
expect(output, `<ul class="task-list">
31+
<li class="task-list-item"><label><input checked="" disabled="" type="checkbox"> Task 1</label></li>
32+
<li class="task-list-item"><label><input disabled="" type="checkbox"> Task 2</label></li>
33+
<li class="task-list-item"><label><input disabled="" type="checkbox"> Task 3</label></li>
34+
</ul>`)
35+
})
36+
37+
it('as ordered task list', async function() {
38+
docsify = await loader()
39+
const output = docsify.compiler.compile(`
40+
1. [ ] Task 1
41+
2. [x] Task 2`)
42+
expectSameDom(output, `<ol class="task-list">
43+
<li class="task-list-item"><label><input disabled="" type="checkbox"> Task 1</label></li>
44+
<li class="task-list-item"><label><input checked="" disabled="" type="checkbox"> Task 2</label></li>
45+
</ol>`)
46+
})
47+
48+
it('normal', async function() {
49+
docsify = await loader()
50+
const output = docsify.compiler.compile(`
51+
- [linktext](link)
52+
- just text`)
53+
expectSameDom(output, `<ul >
54+
<li><a href="#/link">linktext</a></li>
55+
<li>just text</li>
56+
</ul>`)
57+
})
58+
})
59+
60+
})

0 commit comments

Comments
 (0)