|
| 1 | +// load ES6 modules in Node.js on the fly |
| 2 | +require = require('esm')(module/*, options*/) |
| 3 | + |
| 4 | +const {expect} = require('chai') |
| 5 | + |
| 6 | +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) |
| 43 | + |
| 44 | +function ready(callback) { |
| 45 | + const state = document.readyState |
| 46 | + |
| 47 | + if (state === 'complete' || state === 'interactive') { |
| 48 | + return setTimeout(callback, 0) |
| 49 | + } |
| 50 | + |
| 51 | + document.addEventListener('DOMContentLoaded', callback) |
| 52 | +} |
| 53 | +let docsify = null |
| 54 | +module.exports.init = function(callback) { |
| 55 | + 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) |
| 63 | + }) |
| 64 | + |
| 65 | + }) |
| 66 | +} |
| 67 | +module.exports.expectSameDom = function(actual, expected) { |
| 68 | + const WHITESPACES_BETWEEN_TAGS = />(\s\s+)</g |
| 69 | + function replacer(match, group1) { |
| 70 | + return match.replace(group1, '') |
| 71 | + } |
| 72 | + expect(actual.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim()) |
| 73 | + .equal(expected.replace(WHITESPACES_BETWEEN_TAGS, replacer).trim()) |
| 74 | +} |
0 commit comments