Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test setup #776

Merged
merged 3 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
103 changes: 58 additions & 45 deletions test/_helper.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
})

})
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!--

create your own fixture directory
if you need a custom README.md or sidebar

-->
18 changes: 18 additions & 0 deletions test/fixtures/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Heading

[another page](other.md)

## II 1

### III 1

#### IV 1

##### V 1


## II 2

### III 2

#### IV 2
16 changes: 16 additions & 0 deletions test/fixtures/simple/other-page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Other

## two 1

### three 1

#### four 1

##### five 1


## two 2

### three 2

#### four 2
13 changes: 13 additions & 0 deletions test/integration/render.js
Original file line number Diff line number Diff line change
@@ -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)
})

})
13 changes: 13 additions & 0 deletions test/integration/router.js
Original file line number Diff line number Diff line change
@@ -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'
})

})
14 changes: 7 additions & 7 deletions test/render.js → test/unit/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ 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() {
docsify = await init()
const {docsify, dom} = await init()
const output = docsify.compiler.compile('!> **Time** is money, my friend!')
expect(output).equal('<p class="tip"><strong>Time</strong> is money, my friend!</p>')
})

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
Expand All @@ -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`)
Expand All @@ -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`)
Expand All @@ -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
Expand All @@ -67,7 +67,7 @@ text
})

it('nested', async function() {
docsify = await init()
const {docsify, dom} = await init()
const output = docsify.compiler.compile(`
- 1
- 2
Expand Down