|
1 | 1 | /* global jestPlaywright page */ |
| 2 | +import mock, { proxy } from 'xhr-mock'; |
2 | 3 | import { waitForSelector } from './wait-for-selector'; |
3 | | -import doMockAjax from './do-mock-ajax.js'; |
4 | 4 |
|
5 | 5 | const axios = require('axios'); |
6 | 6 | const prettier = require('prettier'); |
7 | 7 | const stripIndent = require('common-tags/lib/stripIndent'); |
8 | 8 |
|
9 | | -const docsifyPATH = `${SRC_PATH}/core/index.js`; // JSDOM |
| 9 | +const docsifyPATH = `${LIB_PATH}/docsify.js`; // JSDOM |
10 | 10 | const docsifyURL = `${LIB_URL}/docsify.js`; // Playwright |
11 | 11 | const isJSDOM = 'window' in global; |
12 | 12 | const isPlaywright = 'page' in global; |
@@ -37,7 +37,7 @@ const isPlaywright = 'page' in global; |
37 | 37 | async function docsifyInit(options = {}) { |
38 | 38 | const defaults = { |
39 | 39 | config: { |
40 | | - basePath: '/', |
| 40 | + basePath: TEST_URL, |
41 | 41 | el: '#app', |
42 | 42 | }, |
43 | 43 | html: ` |
@@ -136,39 +136,52 @@ async function docsifyInit(options = {}) { |
136 | 136 | }; |
137 | 137 |
|
138 | 138 | // 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 | + |
139 | 151 | 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 | + } |
164 | 155 |
|
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 { |
170 | 178 | 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); |
172 | 185 | } |
173 | 186 |
|
174 | 187 | // Set test URL / HTML |
@@ -294,6 +307,11 @@ async function docsifyInit(options = {}) { |
294 | 307 | } |
295 | 308 | } |
296 | 309 |
|
| 310 | + if (isJSDOM) { |
| 311 | + // Restore the global XMLHttpRequest object |
| 312 | + mock.teardown(); |
| 313 | + } |
| 314 | + |
297 | 315 | return Promise.resolve(); |
298 | 316 | } |
299 | 317 |
|
|
0 commit comments