Skip to content

Commit 96a6c5b

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

File tree

4 files changed

+198
-3
lines changed

4 files changed

+198
-3
lines changed

package-lock.json

Lines changed: 142 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: 3 additions & 1 deletion
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",

test/_loader.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
require = require('esm')(module/*, options*/)
22
const {JSDOM} = require('jsdom')
3-
const dom = new JSDOM('<!DOCTYPE html><body></body>')
3+
const dom = new JSDOM('<!DOCTYPE html><html><head></head><body></body></html>') // minimal DOM
44

55
global.window = dom.window
66
global.document = dom.window.document
77
global.navigator = dom.window.navigator
88
global.location = dom.window.location
99

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

test/render.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {expect} = require('chai')
2+
3+
const loader = require('./_loader')
4+
5+
describe('render', function() {
6+
it('important content (tips)', async function() {
7+
docsify = await loader()
8+
const output = docsify.compiler.compile('!> **Time** is money, my friend!')
9+
expect(output).equal('<p class="tip"><strong>Time</strong> is money, my friend!</p>')
10+
})
11+
})

0 commit comments

Comments
 (0)