Skip to content

Commit 97399e4

Browse files
committed
Replace do-mock-ajax with xhr-mock
- Allows mocking all XMLHttpRequests instead of just those initiated via /src/core/fetch/ajax.js - Allows JSDOM tests to use /lib/docsify.js instead of /src/core/index.js (same as Playwright tests) - Allows JSDOM tests to use /docs site as test content
1 parent da36452 commit 97399e4

File tree

6 files changed

+135
-102
lines changed

6 files changed

+135
-102
lines changed

package-lock.json

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"rollup-plugin-replace": "^2.2.0",
104104
"rollup-plugin-uglify": "^6.0.4",
105105
"serve-handler": "^6.1.2",
106-
"stylus": "^0.54.5"
106+
"stylus": "^0.54.5",
107+
"xhr-mock": "^2.5.1"
107108
},
108109
"keywords": [
109110
"doc",

test/helpers/do-mock-ajax.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/helpers/docsify-init.js

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* global jestPlaywright page */
2+
import mock, { proxy } from 'xhr-mock';
23
import { waitForSelector } from './wait-for-selector';
3-
import doMockAjax from './do-mock-ajax.js';
44

55
const axios = require('axios');
66
const prettier = require('prettier');
77
const stripIndent = require('common-tags/lib/stripIndent');
88

9-
const docsifyPATH = `${SRC_PATH}/core/index.js`; // JSDOM
9+
const docsifyPATH = `${LIB_PATH}/docsify.js`; // JSDOM
1010
const docsifyURL = `${LIB_URL}/docsify.js`; // Playwright
1111
const isJSDOM = 'window' in global;
1212
const isPlaywright = 'page' in global;
@@ -37,7 +37,7 @@ const isPlaywright = 'page' in global;
3737
async function docsifyInit(options = {}) {
3838
const defaults = {
3939
config: {
40-
basePath: '/',
40+
basePath: TEST_URL,
4141
el: '#app',
4242
},
4343
html: `
@@ -136,39 +136,52 @@ async function docsifyInit(options = {}) {
136136
};
137137

138138
// Routes
139+
const contentTypes = {
140+
css: 'text/css',
141+
html: 'text/html',
142+
js: 'application/javascript',
143+
json: 'application/json',
144+
md: 'text/markdown',
145+
};
146+
const reFileExtentionFromURL = new RegExp(
147+
'(?:.)(' + Object.keys(contentTypes).join('|') + ')(?:[?#].*)?$',
148+
'i'
149+
);
150+
139151
if (isJSDOM) {
140-
doMockAjax(settings.routes);
141-
} else if (isPlaywright) {
142-
const contentTypes = {
143-
css: 'text/css',
144-
html: 'text/html',
145-
js: 'application/javascript',
146-
json: 'application/json',
147-
md: 'text/markdown',
148-
};
149-
const reFileExtentionFromURL = new RegExp(
150-
'(?:.)(' + Object.keys(contentTypes).join('|') + ')(?:[?#].*)?$',
151-
'i'
152-
);
153-
154-
Object.entries(settings.routes).forEach(async ([urlGlob, response]) => {
155-
if (typeof response === 'string') {
156-
const urlFileExtension = (urlGlob.match(reFileExtentionFromURL) ||
157-
[])[1];
158-
const contentType = contentTypes[urlFileExtension];
159-
160-
response = {
161-
status: 200,
162-
body: response,
163-
};
152+
// Replace the global XMLHttpRequest object
153+
mock.setup();
154+
}
164155

165-
// Specifying contentType required for Webkit
166-
if (contentType) {
167-
response.contentType = contentType;
168-
}
169-
}
156+
for (let [urlGlob, response] of Object.entries(settings.routes)) {
157+
const fileExtension = (urlGlob.match(reFileExtentionFromURL) || [])[1];
158+
const contentType = contentTypes[fileExtension];
159+
160+
if (typeof response === 'string') {
161+
response = {
162+
status: 200,
163+
body: response,
164+
};
165+
}
166+
167+
// Specifying contentType required for Webkit
168+
response.contentType = response.contentType || contentType || '';
169+
170+
if (isJSDOM) {
171+
mock.get(urlGlob, (req, res) => {
172+
return res
173+
.status(response.status)
174+
.body(settings.routes[urlGlob])
175+
.header('Content-Type', contentType);
176+
});
177+
} else {
170178
await page.route(urlGlob, route => route.fulfill(response));
171-
});
179+
}
180+
}
181+
182+
if (isJSDOM) {
183+
// Proxy unhandled requests to real server(s)
184+
mock.use(proxy);
172185
}
173186

174187
// Set test URL / HTML
@@ -294,6 +307,11 @@ async function docsifyInit(options = {}) {
294307
}
295308
}
296309

310+
if (isJSDOM) {
311+
// Restore the global XMLHttpRequest object
312+
mock.teardown();
313+
}
314+
297315
return Promise.resolve();
298316
}
299317

test/integration/__snapshots__/docs.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Docs Site coverpage renders and is unchanged 1`] = `
4-
"<section class=\\"cover show\\" style=\\"background: linear-gradient(to left bottom, hsl(127, 100%, 85%) 0%,hsl(127, 100%, 85%) 100%)\\"><div class=\\"mask\\"></div><div class=\\"cover-main\\"><p><img src=\\"/_media/icon.svg\\" data-origin=\\"_media/icon.svg\\" alt=\\"logo\\"></p><h1 id=\\"docsify-4114\\"><a href=\\"#/?id=docsify-4114\\" data-id=\\"docsify-4114\\" class=\\"anchor\\"><span>docsify <small>4.11.4</small></span></a></h1><blockquote>
4+
"<section class=\\"cover show\\" style=\\"background: linear-gradient(to left bottom, hsl(127, 100%, 85%) 0%,hsl(127, 100%, 85%) 100%)\\"><div class=\\"mask\\"></div><div class=\\"cover-main\\"><p><img src=\\"http://127.0.0.1:3001/_media/icon.svg\\" data-origin=\\"_media/icon.svg\\" alt=\\"logo\\"></p><h1 id=\\"docsify-4114\\"><a href=\\"#/?id=docsify-4114\\" data-id=\\"docsify-4114\\" class=\\"anchor\\"><span>docsify <small>4.11.4</small></span></a></h1><blockquote>
55
<p>A magical documentation site generator.</p></blockquote>
66
<ul><li>Simple and lightweight</li><li>No statically built html files</li><li>Multiple themes</li></ul><p><a href=\\"https://github.com/docsifyjs/docsify/\\" target=\\"_blank\\" rel=\\"noopener\\">GitHub</a>
77
<a href=\\"#/?id=docsify\\">Getting Started</a></p></div></section>"

test/integration/example.test.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ const docsifyInit = require('../helpers/docsify-init');
77
describe('Example Tests', function() {
88
// Tests
99
// ---------------------------------------------------------------------------
10-
// test('Docsify /docs/ site using docsifyInit()', async () => {
11-
// // Load custom docsify
12-
// // (See ./helpers/docsifyInit.js for details)
13-
// await docsifyInit({
14-
// config: {
15-
// basePath: `/docs`,
16-
// },
17-
// // _logHTML: true,
18-
// });
19-
20-
// // Verify config options
21-
// expect(typeof window.$docsify).toEqual('object');
22-
23-
// // Verify options.markdown content was rendered
24-
// expect(document.querySelector('#main').textContent).toContain(
25-
// 'A magical documentation site generator'
26-
// );
27-
// });
10+
test('Docsify /docs/ site using docsifyInit()', async () => {
11+
await docsifyInit({
12+
config: {
13+
basePath: `${TEST_URL}/docs`,
14+
},
15+
// _logHTML: true,
16+
});
17+
18+
// Verify config options
19+
expect(typeof window.$docsify).toEqual('object');
20+
21+
// Verify options.markdown content was rendered
22+
expect(document.querySelector('#main').textContent).toContain(
23+
'A magical documentation site generator'
24+
);
25+
});
2826

2927
test('kitchen sink docsify site using docsifyInit()', async () => {
3028
const docsifyInitConfig = {

0 commit comments

Comments
 (0)