Skip to content

Commit c8fa4b8

Browse files
devtonhereQingWei-Li
authored andcommitted
Setup test environment and provide first tests 🌟
1 parent 016799b commit c8fa4b8

File tree

5 files changed

+316
-12
lines changed

5 files changed

+316
-12
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/_helper.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// load ES6 modules in Node.js on the fly
2+
require = require('esm')(module/*, options*/)
3+
4+
const {expect} = require('chai')
5+
6+
const {JSDOM} = require('jsdom')
7+
const XMLHttpRequest = require('xhr2') // JSDOM doesn't support XMLHttpRequest
8+
// TODO: try to fix tests when using `<div id="app"></div>` in body
9+
const markup = `<!DOCTYPE html>
10+
<html>
11+
<head></head>
12+
<body></body>
13+
</html>`
14+
// TODO: this may not work if tests are mutate the DOM, instead a new instance needs to be created
15+
// for every test case but that will slow down the tests
16+
const dom = new JSDOM(markup)
17+
18+
global.window = dom.window
19+
global.document = dom.window.document
20+
global.navigator = dom.window.navigator
21+
global.location = dom.window.location
22+
global.XMLHttpRequest = XMLHttpRequest
23+
24+
const {initMixin} = require('../src/core/init')
25+
const {routerMixin} = require('../src/core//router')
26+
const {renderMixin} = require('../src/core//render')
27+
const {fetchMixin} = require('../src/core/fetch')
28+
const {eventMixin} = require('../src/core//event')
29+
30+
// mimic src/core/index.js but for Node.js
31+
32+
function Docsify() {
33+
this._init()
34+
}
35+
36+
const proto = Docsify.prototype
37+
38+
initMixin(proto)
39+
routerMixin(proto)
40+
renderMixin(proto)
41+
fetchMixin(proto)
42+
eventMixin(proto)
43+
44+
function ready(callback) {
45+
const state = document.readyState
46+
47+
if (state === 'complete' || state === 'interactive') {
48+
return setTimeout(callback, 0)
49+
}
50+
51+
document.addEventListener('DOMContentLoaded', callback)
52+
}
53+
let docsify = null
54+
module.exports.init = function(callback) {
55+
return new Promise((resolve, reject) => {
56+
// return cached version / TODO: see above: this might not scale, new instance of JSDOM is reqiured
57+
if (docsify != null) {
58+
return resolve(docsify)
59+
}
60+
ready(_ => {
61+
docsify = new Docsify()
62+
return resolve(docsify)
63+
})
64+
65+
})
66+
}
67+
module.exports.expectSameDom = function(actual, expected) {
68+
const WHITESPACES_BETWEEN_TAGS = />(\s\s+)</g
69+
function replacer(match, group1) {
70+
return match.replace(group1, '')
71+
}
72+
expect(actual.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim())
73+
.equal(expected.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim())
74+
}

test/_loader.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)