|
|
|
@ -1,45 +1,10 @@ |
|
|
|
|
// load ES6 modules in Node.js on the fly
|
|
|
|
|
require = require('esm')(module/*, options*/) |
|
|
|
|
|
|
|
|
|
const path = require('path') |
|
|
|
|
const {expect} = require('chai') |
|
|
|
|
|
|
|
|
|
const {JSDOM} = require('jsdom') |
|
|
|
|
const XMLHttpRequest = require('xhr2') // JSDOM doesn't support XMLHttpRequest
|
|
|
|
|
// TODO: try to fix tests when using `<div id="app"></div>` in body
|
|
|
|
|
const markup = `<!DOCTYPE html>
|
|
|
|
|
<html> |
|
|
|
|
<head></head> |
|
|
|
|
<body></body> |
|
|
|
|
</html>` |
|
|
|
|
// TODO: this may not work if tests are mutate the DOM, instead a new instance needs to be created
|
|
|
|
|
// for every test case but that will slow down the tests
|
|
|
|
|
const dom = new JSDOM(markup)
|
|
|
|
|
|
|
|
|
|
global.window = dom.window |
|
|
|
|
global.document = dom.window.document |
|
|
|
|
global.navigator = dom.window.navigator |
|
|
|
|
global.location = dom.window.location |
|
|
|
|
global.XMLHttpRequest = XMLHttpRequest |
|
|
|
|
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
|
// mimic src/core/index.js but for Node.js
|
|
|
|
|
|
|
|
|
|
function Docsify() { |
|
|
|
|
this._init() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const proto = Docsify.prototype |
|
|
|
|
|
|
|
|
|
initMixin(proto) |
|
|
|
|
routerMixin(proto) |
|
|
|
|
renderMixin(proto) |
|
|
|
|
fetchMixin(proto) |
|
|
|
|
eventMixin(proto) |
|
|
|
|
|
|
|
|
|
function ready(callback) { |
|
|
|
|
const state = document.readyState |
|
|
|
@ -50,16 +15,64 @@ function ready(callback) { |
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', callback) |
|
|
|
|
} |
|
|
|
|
let docsify = null |
|
|
|
|
module.exports.init = function(callback) { |
|
|
|
|
module.exports.init = function(fixture = 'default', config = {}, markup) { |
|
|
|
|
if (markup == null) { |
|
|
|
|
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 = new JSDOM(markup)
|
|
|
|
|
dom.reconfigure({ url: 'file:///' + rootPath }) |
|
|
|
|
|
|
|
|
|
global.window = dom.window |
|
|
|
|
global.document = dom.window.document |
|
|
|
|
global.navigator = dom.window.navigator |
|
|
|
|
global.location = dom.window.location |
|
|
|
|
global.XMLHttpRequest = dom.window.XMLHttpRequest |
|
|
|
|
|
|
|
|
|
// 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, reject) => { |
|
|
|
|
// return cached version / TODO: see above: this might not scale, new instance of JSDOM is reqiured
|
|
|
|
|
if (docsify != null) { |
|
|
|
|
return resolve(docsify) |
|
|
|
|
} |
|
|
|
|
ready(_ => { |
|
|
|
|
docsify = new Docsify() |
|
|
|
|
return resolve(docsify) |
|
|
|
|
ready(() => { |
|
|
|
|
const docsify = new Docsify() |
|
|
|
|
// TODO: use callback instead of polling, but usually it works after 10ms
|
|
|
|
|
const id = setInterval(() => { |
|
|
|
|
if (dom.window.document.body.innerHTML.indexOf(NOT_INIT_PATTERN) == -1) { |
|
|
|
|
clearInterval(id) |
|
|
|
|
return resolve({ |
|
|
|
|
docsify: docsify, |
|
|
|
|
dom: dom |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, 10) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
}) |
|
|
|
|