Skip to content

Commit 01ea441

Browse files
committed
fix xhr usage, fix init URL setup
provide full test setup including fixtures
1 parent bc4d06b commit 01ea441

File tree

5 files changed

+71
-59
lines changed

5 files changed

+71
-59
lines changed

package-lock.json

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

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -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",

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+
// TODO: use callback instead of polling, but usually it works after 10ms
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
})

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+
-->

test/render.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ 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)