Skip to content

Commit c1d4961

Browse files
authored
Merge pull request #776 from docsifyjs/improve-test-setup
Improve test setup
2 parents d5910bd + 2039e0d commit c1d4961

File tree

9 files changed

+135
-61
lines changed

9 files changed

+135
-61
lines changed

Diff for: package-lock.json

-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-3
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": "mocha",
30+
"test": "mocha test/*/**",
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",
@@ -72,8 +72,7 @@
7272
"rollup-plugin-node-resolve": "^3.0.0",
7373
"rollup-plugin-replace": "^2.0.0",
7474
"rollup-plugin-uglify": "^2.0.1",
75-
"stylus": "^0.54.5",
76-
"xhr2": "^0.1.4"
75+
"stylus": "^0.54.5"
7776
},
7877
"keywords": [
7978
"doc",

Diff for: test/_helper.js

+58-45
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
// load ES6 modules in Node.js on the fly
22
require = require('esm')(module/*, options*/)
33

4+
const path = require('path')
45
const {expect} = require('chai')
56

67
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)
438

449
function ready(callback) {
4510
const state = document.readyState
@@ -50,16 +15,64 @@ function ready(callback) {
5015

5116
document.addEventListener('DOMContentLoaded', callback)
5217
}
53-
let docsify = null
54-
module.exports.init = function(callback) {
18+
module.exports.init = function(fixture = 'default', config = {}, markup) {
19+
if (markup == null) {
20+
markup = `<!DOCTYPE html>
21+
<html>
22+
<head></head>
23+
<body>
24+
<div id="app"></div>
25+
<script>
26+
window.$docsify = ${JSON.stringify(config, null, 2)}
27+
</script>
28+
</body>
29+
</html>`
30+
}
31+
const rootPath = path.join(__dirname, 'fixtures', fixture)
32+
33+
const dom = new JSDOM(markup)
34+
dom.reconfigure({ url: 'file:///' + rootPath })
35+
36+
global.window = dom.window
37+
global.document = dom.window.document
38+
global.navigator = dom.window.navigator
39+
global.location = dom.window.location
40+
global.XMLHttpRequest = dom.window.XMLHttpRequest
41+
42+
// mimic src/core/index.js but for Node.js
43+
function Docsify() {
44+
this._init()
45+
}
46+
47+
const proto = Docsify.prototype
48+
49+
const {initMixin} = require('../src/core/init')
50+
const {routerMixin} = require('../src/core//router')
51+
const {renderMixin} = require('../src/core//render')
52+
const {fetchMixin} = require('../src/core/fetch')
53+
const {eventMixin} = require('../src/core//event')
54+
55+
initMixin(proto)
56+
routerMixin(proto)
57+
renderMixin(proto)
58+
fetchMixin(proto)
59+
eventMixin(proto)
60+
61+
const NOT_INIT_PATTERN = '<!--main-->'
62+
5563
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)
64+
ready(() => {
65+
const docsify = new Docsify()
66+
// NOTE: I was not able to get it working with a callback, but polling works usually at the first time
67+
const id = setInterval(() => {
68+
if (dom.window.document.body.innerHTML.indexOf(NOT_INIT_PATTERN) == -1) {
69+
clearInterval(id)
70+
return resolve({
71+
docsify: docsify,
72+
dom: dom
73+
})
74+
}
75+
}, 10)
6376
})
6477

6578
})

Diff for: test/fixtures/default/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--
2+
3+
create your own fixture directory
4+
if you need a custom README.md or sidebar
5+
6+
-->

Diff for: test/fixtures/simple/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Heading
2+
3+
[another page](other.md)
4+
5+
## II 1
6+
7+
### III 1
8+
9+
#### IV 1
10+
11+
##### V 1
12+
13+
14+
## II 2
15+
16+
### III 2
17+
18+
#### IV 2

Diff for: test/fixtures/simple/other-page.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Other
2+
3+
## two 1
4+
5+
### three 1
6+
7+
#### four 1
8+
9+
##### five 1
10+
11+
12+
## two 2
13+
14+
### three 2
15+
16+
#### four 2

Diff for: test/integration/render.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path')
2+
3+
const {expect} = require('chai')
4+
5+
const {init, expectSameDom} = require('../_helper')
6+
7+
describe('full docsify initialization', function() {
8+
it('TODO: check generated markup', async function() {
9+
const {docsify, dom} = await init('simple', {loadSidebar: true})
10+
console.log(dom.window.document.body.innerHTML)
11+
// TODO: add some expectations
12+
})
13+
14+
})

Diff for: test/integration/router.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path')
2+
3+
const {expect} = require('chai')
4+
5+
const {init, expectSameDom} = require('../_helper')
6+
7+
describe('router', function() {
8+
it('TODO: trigger to load another page', async function() {
9+
const {docsify} = await init()
10+
window.location = '/?foo=bar'
11+
// TODO: add some expectations
12+
})
13+
14+
})

Diff for: test/render.js renamed to test/unit/render.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ const path = require('path')
22

33
const {expect} = require('chai')
44

5-
const {init, expectSameDom} = require('./_helper')
5+
const {init, expectSameDom} = require('../_helper')
66

77
describe('render', function() {
88
it('important content (tips)', async function() {
9-
docsify = await init()
9+
const {docsify, dom} = await init()
1010
const output = docsify.compiler.compile('!> **Time** is money, my friend!')
1111
expect(output).equal('<p class="tip"><strong>Time</strong> is money, my friend!</p>')
1212
})
1313

1414
describe('lists', function() {
1515
it('as unordered task list', async function() {
16-
docsify = await init()
16+
const {docsify, dom} = await init()
1717
const output = docsify.compiler.compile(`
1818
- [x] Task 1
1919
- [ ] Task 2
@@ -26,7 +26,7 @@ describe('render', function() {
2626
})
2727

2828
it('as ordered task list', async function() {
29-
docsify = await init()
29+
const {docsify, dom} = await init()
3030
const output = docsify.compiler.compile(`
3131
1. [ ] Task 1
3232
2. [x] Task 2`)
@@ -37,7 +37,7 @@ describe('render', function() {
3737
})
3838

3939
it('normal unordered', async function() {
40-
docsify = await init()
40+
const {docsify, dom} = await init()
4141
const output = docsify.compiler.compile(`
4242
- [linktext](link)
4343
- just text`)
@@ -48,7 +48,7 @@ describe('render', function() {
4848
})
4949

5050
it('unordered with custom start', async function() {
51-
docsify = await init()
51+
const {docsify, dom} = await init()
5252
const output = docsify.compiler.compile(`
5353
1. first
5454
2. second
@@ -67,7 +67,7 @@ text
6767
})
6868

6969
it('nested', async function() {
70-
docsify = await init()
70+
const {docsify, dom} = await init()
7171
const output = docsify.compiler.compile(`
7272
- 1
7373
- 2

0 commit comments

Comments
 (0)