forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_helper.js
109 lines (89 loc) · 2.76 KB
/
_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disable no-global-assign */
// Load ES6 modules in Node.js on the fly
require = require('esm')(
module /* , options */
); /* eslint-disable-line no-global-assign */
const path = require('path');
const { expect } = require('chai');
const { JSDOM } = require('jsdom');
function ready(callback) {
const state = document.readyState;
if (state === 'complete' || state === 'interactive') {
return setTimeout(callback, 0);
}
document.addEventListener('DOMContentLoaded', callback);
}
module.exports.initJSDOM = initJSDOM;
/** @param {string} markup - The HTML document to initialize JSDOM with. */
function initJSDOM(markup, options = {}) {
const dom = new JSDOM(markup, options);
global.window = dom.window;
global.document = dom.window.document;
global.navigator = dom.window.navigator;
global.location = dom.window.location;
global.XMLHttpRequest = dom.window.XMLHttpRequest;
return dom;
}
module.exports.init = function(
fixture = 'default',
config = {},
markup = null
) {
if (markup === null || markup === undefined) {
markup = `<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="app"></div>
<script>
window.$docsify = ${JSON.stringify(config, null, 2)}
</script>
</body>
</html>`;
}
const rootPath = path.join(__dirname, 'fixtures', fixture);
const dom = initJSDOM(markup);
dom.reconfigure({ url: 'file:///' + rootPath });
// Mimic src/core/index.js but for Node.js
function Docsify() {
this._init();
}
const proto = Docsify.prototype;
const { initMixin } = require('../src/core/init');
const { routerMixin } = require('../src/core//router');
const { renderMixin } = require('../src/core//render');
const { fetchMixin } = require('../src/core/fetch');
const { eventMixin } = require('../src/core//event');
initMixin(proto);
routerMixin(proto);
renderMixin(proto);
fetchMixin(proto);
eventMixin(proto);
const NOT_INIT_PATTERN = '<!--main-->';
return new Promise(resolve => {
ready(() => {
const docsify = new Docsify();
// NOTE: I was not able to get it working with a callback, but polling works usually at the first time
const id = setInterval(() => {
if (
dom.window.document.body.innerHTML.indexOf(NOT_INIT_PATTERN) === -1
) {
clearInterval(id);
return resolve({
docsify: docsify,
dom: dom,
});
}
}, 10);
});
});
};
module.exports.expectSameDom = function(actual, expected) {
const WHITESPACES_BETWEEN_TAGS = />(\s\s+)</g;
function replacer(match, group1) {
return match.replace(group1, '');
}
expect(actual.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim()).equal(
expected.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim()
);
};