1
1
// load ES6 modules in Node.js on the fly
2
2
require = require ( 'esm' ) ( module /*, options*/ )
3
3
4
+ const path = require ( 'path' )
4
5
const { expect} = require ( 'chai' )
5
6
6
7
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
8
44
9
function ready ( callback ) {
45
10
const state = document . readyState
@@ -50,16 +15,64 @@ function ready(callback) {
50
15
51
16
document . addEventListener ( 'DOMContentLoaded' , callback )
52
17
}
53
- let docsify = null
54
- module . exports . init = function ( callback ) {
18
+ module . exports . init = function ( fixture = 'default' , config = { } , markup ) {
19
+ if ( markup == null ) {
20
+ markup = `<!DOCTYPE html>
21
+ <html>
22
+ <head></head>
23
+ <body>
24
+ <div id="app"></div>
25
+ <script>
26
+ window.$docsify = ${ JSON . stringify ( config , null , 2 ) }
27
+ </script>
28
+ </body>
29
+ </html>`
30
+ }
31
+ const rootPath = path . join ( __dirname , 'fixtures' , fixture )
32
+
33
+ const dom = new JSDOM ( markup )
34
+ dom . reconfigure ( { url : 'file:///' + rootPath } )
35
+
36
+ global . window = dom . window
37
+ global . document = dom . window . document
38
+ global . navigator = dom . window . navigator
39
+ global . location = dom . window . location
40
+ global . XMLHttpRequest = dom . window . XMLHttpRequest
41
+
42
+ // mimic src/core/index.js but for Node.js
43
+ function Docsify ( ) {
44
+ this . _init ( )
45
+ }
46
+
47
+ const proto = Docsify . prototype
48
+
49
+ const { initMixin} = require ( '../src/core/init' )
50
+ const { routerMixin} = require ( '../src/core//router' )
51
+ const { renderMixin} = require ( '../src/core//render' )
52
+ const { fetchMixin} = require ( '../src/core/fetch' )
53
+ const { eventMixin} = require ( '../src/core//event' )
54
+
55
+ initMixin ( proto )
56
+ routerMixin ( proto )
57
+ renderMixin ( proto )
58
+ fetchMixin ( proto )
59
+ eventMixin ( proto )
60
+
61
+ const NOT_INIT_PATTERN = '<!--main-->'
62
+
55
63
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 )
64
+ ready ( ( ) => {
65
+ const docsify = new Docsify ( )
66
+ // TODO: use callback instead of polling, but usually it works after 10ms
67
+ const id = setInterval ( ( ) => {
68
+ if ( dom . window . document . body . innerHTML . indexOf ( NOT_INIT_PATTERN ) == - 1 ) {
69
+ clearInterval ( id )
70
+ return resolve ( {
71
+ docsify : docsify ,
72
+ dom : dom
73
+ } )
74
+ }
75
+ } , 10 )
63
76
} )
64
77
65
78
} )
0 commit comments