Skip to content

Commit ec48f93

Browse files
committed
feat(test): add dynamic and asynchronous content provision support for test instances
1 parent 837d04a commit ec48f93

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

test/helpers/docsify-init.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const docsifyURL = '/dist/docsify.js'; // Playwright
1717
* @param {Function|Object} [options.config] docsify configuration (merged with default)
1818
* @param {String} [options.html] HTML content to use for docsify `index.html` page
1919
* @param {Object} [options.markdown] Docsify markdown content
20-
* @param {String} [options.markdown.coverpage] coverpage markdown
21-
* @param {String} [options.markdown.homepage] homepage markdown
22-
* @param {String} [options.markdown.navbar] navbar markdown
23-
* @param {String} [options.markdown.sidebar] sidebar markdown
24-
* @param {Object} [options.routes] custom routes defined as `{ pathOrGlob: responseText }`
20+
* @param {String|(()=>Promise<String>|String)} [options.markdown.coverpage] coverpage markdown
21+
* @param {String|(()=>Promise<String>|String)} [options.markdown.homepage] homepage markdown
22+
* @param {String|(()=>Promise<String>|String)} [options.markdown.navbar] navbar markdown
23+
* @param {String|(()=>Promise<String>|String)} [options.markdown.sidebar] sidebar markdown
24+
* @param {Record<String,String|(()=>Promise<String>|String)>} [options.routes] custom routes defined as `{ pathOrGlob: response }`
2525
* @param {String} [options.script] JS to inject via <script> tag
2626
* @param {String|String[]} [options.scriptURLs] External JS to inject via <script src="..."> tag(s)
2727
* @param {String} [options.style] CSS to inject via <style> tag
@@ -114,7 +114,7 @@ async function docsifyInit(options = {}) {
114114
...options.markdown,
115115
})
116116
.filter(([key, markdown]) => key && markdown)
117-
.map(([key, markdown]) => [key, stripIndent`${markdown}`]),
117+
.map(([key, markdown]) => [key, markdown]),
118118
);
119119
},
120120
get routes() {
@@ -132,13 +132,12 @@ async function docsifyInit(options = {}) {
132132
...options.routes,
133133
})
134134
// Remove items with falsey responseText
135-
.filter(([url, responseText]) => url && responseText)
136-
.map(([url, responseText]) => [
135+
.filter(([url, response]) => url && response)
136+
.map(([url, response]) => [
137137
// Convert relative to absolute URL
138138
new URL(url, settings.config.basePath || process.env.TEST_HOST)
139139
.href,
140-
// Strip indentation from responseText
141-
stripIndent`${responseText}`,
140+
response,
142141
]),
143142
);
144143

@@ -173,29 +172,30 @@ async function docsifyInit(options = {}) {
173172
mock.setup();
174173
}
175174

176-
for (let [urlGlob, response] of Object.entries(settings.routes)) {
175+
for (const [urlGlob, response] of Object.entries(settings.routes)) {
177176
const fileExtension = (urlGlob.match(reFileExtentionFromURL) || [])[1];
178177
const contentType = contentTypes[fileExtension];
179-
180-
if (typeof response === 'string') {
181-
response = {
182-
status: 200,
183-
body: response,
184-
};
185-
}
186-
187-
// Specifying contentType required for Webkit
188-
response.contentType = response.contentType || contentType || '';
178+
const responseBody = async () => {
179+
if (typeof response === 'string') {
180+
return stripIndent`${response}`;
181+
} else {
182+
return stripIndent`${await response()}`;
183+
}
184+
};
189185

190186
if (isJSDOM) {
191-
mock.get(urlGlob, (req, res) => {
192-
return res
193-
.status(response.status)
194-
.body(settings.routes[urlGlob])
195-
.header('Content-Type', contentType);
187+
mock.get(urlGlob, async (req, res) => {
188+
const body = await responseBody();
189+
return res.status(200).body(body).header('Content-Type', contentType);
196190
});
197191
} else {
198-
await page.route(urlGlob, route => route.fulfill(response));
192+
await page.route(urlGlob, async route => {
193+
return route.fulfill({
194+
status: 200,
195+
body: await responseBody(),
196+
contentType: contentType || '',
197+
});
198+
});
199199
}
200200
}
201201

0 commit comments

Comments
 (0)