From 01ea44106b13f93aba6c3dbfd81068d9b1931c98 Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Wed, 20 Feb 2019 21:36:47 +0100 Subject: [PATCH 1/3] fix xhr usage, fix init URL setup provide full test setup including fixtures --- package-lock.json | 6 -- package.json | 3 +- test/_helper.js | 103 ++++++++++++++++++-------------- test/fixtures/default/README.md | 6 ++ test/render.js | 12 ++-- 5 files changed, 71 insertions(+), 59 deletions(-) create mode 100644 test/fixtures/default/README.md diff --git a/package-lock.json b/package-lock.json index 9a205ac90..a0582ad6b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8791,12 +8791,6 @@ "async-limiter": "~1.0.0" } }, - "xhr2": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/xhr2/-/xhr2-0.1.4.tgz", - "integrity": "sha1-f4dliEdxbbUCYyOBL4GMras4el8=", - "dev": true - }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index 5a7b8bc47..33e61461d 100644 --- a/package.json +++ b/package.json @@ -72,8 +72,7 @@ "rollup-plugin-node-resolve": "^3.0.0", "rollup-plugin-replace": "^2.0.0", "rollup-plugin-uglify": "^2.0.1", - "stylus": "^0.54.5", - "xhr2": "^0.1.4" + "stylus": "^0.54.5" }, "keywords": [ "doc", diff --git a/test/_helper.js b/test/_helper.js index bac140761..47e757863 100644 --- a/test/_helper.js +++ b/test/_helper.js @@ -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 `
` in body -const markup = ` - - - -` -// 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 = ` + + + +
+ + + ` + } + 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 = '' + 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) }) }) diff --git a/test/fixtures/default/README.md b/test/fixtures/default/README.md new file mode 100644 index 000000000..28df398a9 --- /dev/null +++ b/test/fixtures/default/README.md @@ -0,0 +1,6 @@ + diff --git a/test/render.js b/test/render.js index 9cb502c1b..a00c1a45a 100644 --- a/test/render.js +++ b/test/render.js @@ -6,14 +6,14 @@ const {init, expectSameDom} = require('./_helper') describe('render', function() { it('important content (tips)', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile('!> **Time** is money, my friend!') expect(output).equal('

Time is money, my friend!

') }) describe('lists', function() { it('as unordered task list', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile(` - [x] Task 1 - [ ] Task 2 @@ -26,7 +26,7 @@ describe('render', function() { }) it('as ordered task list', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile(` 1. [ ] Task 1 2. [x] Task 2`) @@ -37,7 +37,7 @@ describe('render', function() { }) it('normal unordered', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile(` - [linktext](link) - just text`) @@ -48,7 +48,7 @@ describe('render', function() { }) it('unordered with custom start', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile(` 1. first 2. second @@ -67,7 +67,7 @@ text }) it('nested', async function() { - docsify = await init() + const {docsify, dom} = await init() const output = docsify.compiler.compile(` - 1 - 2 From dac0bfec6c442d0b9eefbf60f22cf3ae50eb751e Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Wed, 20 Feb 2019 21:37:02 +0100 Subject: [PATCH 2/3] split test into unit and integration provide some dummy tests cases --- package.json | 2 +- test/fixtures/simple/README.md | 18 ++++++++++++++++++ test/fixtures/simple/other-page.md | 16 ++++++++++++++++ test/integration/render.js | 13 +++++++++++++ test/integration/router.js | 13 +++++++++++++ test/{ => unit}/render.js | 2 +- 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/simple/README.md create mode 100644 test/fixtures/simple/other-page.md create mode 100644 test/integration/render.js create mode 100644 test/integration/router.js rename test/{ => unit}/render.js (97%) diff --git a/package.json b/package.json index 33e61461d..c61835b52 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "dev": "run-p serve watch:*", "dev:ssr": "run-p serve:ssr watch:*", "lint": "eslint {src,packages} --fix", - "test": "mocha", + "test": "mocha test/*/**", "css": "stylus src/themes/*.styl -u autoprefixer-stylus", "watch:css": "run-p 'css -- -o themes -w'", "watch:js": "node build/build.js", diff --git a/test/fixtures/simple/README.md b/test/fixtures/simple/README.md new file mode 100644 index 000000000..0f3fd4699 --- /dev/null +++ b/test/fixtures/simple/README.md @@ -0,0 +1,18 @@ +# Heading + +[another page](other.md) + +## II 1 + +### III 1 + +#### IV 1 + +##### V 1 + + +## II 2 + +### III 2 + +#### IV 2 diff --git a/test/fixtures/simple/other-page.md b/test/fixtures/simple/other-page.md new file mode 100644 index 000000000..4bc6e9877 --- /dev/null +++ b/test/fixtures/simple/other-page.md @@ -0,0 +1,16 @@ +# Other + +## two 1 + +### three 1 + +#### four 1 + +##### five 1 + + +## two 2 + +### three 2 + +#### four 2 diff --git a/test/integration/render.js b/test/integration/render.js new file mode 100644 index 000000000..adddfc454 --- /dev/null +++ b/test/integration/render.js @@ -0,0 +1,13 @@ +const path = require('path') + +const {expect} = require('chai') + +const {init, expectSameDom} = require('../_helper') + +describe('full docsify initialization', function() { + it('TODO: check generated markup', async function() { + const {docsify, dom} = await init('simple', {loadSidebar: true}) + console.log(dom.window.document.body.innerHTML) + }) + +}) diff --git a/test/integration/router.js b/test/integration/router.js new file mode 100644 index 000000000..5408683c1 --- /dev/null +++ b/test/integration/router.js @@ -0,0 +1,13 @@ +const path = require('path') + +const {expect} = require('chai') + +const {init, expectSameDom} = require('../_helper') + +describe('router', function() { + it('TODO: trigger to load another page', async function() { + const {docsify} = await init() + window.location = '/?foo=bar' + }) + +}) diff --git a/test/render.js b/test/unit/render.js similarity index 97% rename from test/render.js rename to test/unit/render.js index a00c1a45a..25043cf0d 100644 --- a/test/render.js +++ b/test/unit/render.js @@ -2,7 +2,7 @@ const path = require('path') const {expect} = require('chai') -const {init, expectSameDom} = require('./_helper') +const {init, expectSameDom} = require('../_helper') describe('render', function() { it('important content (tips)', async function() { From 2039e0dd1c61083324279479fff4b291408f1e7c Mon Sep 17 00:00:00 2001 From: Anton Wilhelm Date: Wed, 6 Mar 2019 21:53:09 +0100 Subject: [PATCH 3/3] rework TODOs --- test/_helper.js | 2 +- test/integration/render.js | 1 + test/integration/router.js | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/_helper.js b/test/_helper.js index 47e757863..4b4abce89 100644 --- a/test/_helper.js +++ b/test/_helper.js @@ -63,7 +63,7 @@ module.exports.init = function(fixture = 'default', config = {}, markup) { return new Promise((resolve, reject) => { ready(() => { const docsify = new Docsify() - // TODO: use callback instead of polling, but usually it works after 10ms + // 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) diff --git a/test/integration/render.js b/test/integration/render.js index adddfc454..7b2b253c1 100644 --- a/test/integration/render.js +++ b/test/integration/render.js @@ -8,6 +8,7 @@ describe('full docsify initialization', function() { it('TODO: check generated markup', async function() { const {docsify, dom} = await init('simple', {loadSidebar: true}) console.log(dom.window.document.body.innerHTML) + // TODO: add some expectations }) }) diff --git a/test/integration/router.js b/test/integration/router.js index 5408683c1..b87e8022d 100644 --- a/test/integration/router.js +++ b/test/integration/router.js @@ -8,6 +8,7 @@ describe('router', function() { it('TODO: trigger to load another page', async function() { const {docsify} = await init() window.location = '/?foo=bar' + // TODO: add some expectations }) })