-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathvirtual-routes.test.js
291 lines (239 loc) Β· 7.16 KB
/
virtual-routes.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import docsifyInit from '../helpers/docsify-init.js';
import { test, expect } from './fixtures/docsify-init-fixture.js';
/**
* Navigate to a specific route in the site
* @param {import('playwright-core').Page} page the playwright page instance from the test
* @param {string} route the route you want to navigate to
*/
async function navigateToRoute(page, route) {
await page.evaluate(r => (window.location.hash = r), route);
await page.waitForLoadState('load');
}
test.describe('Virtual Routes - Generate Dynamic Content via Config', () => {
test.describe('Different Types of Virtual Routes', () => {
test('rendering virtual routes specified as string', async ({ page }) => {
const routes = {
'/my-awesome-route': '# My Awesome Route',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/my-awesome-route');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('My Awesome Route');
});
test('rendering virtual routes specified as functions', async ({
page,
}) => {
const routes = {
'/my-awesome-function-route': function () {
return '# My Awesome Function Route';
},
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/my-awesome-function-route');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('My Awesome Function Route');
});
test('rendering virtual routes specified functions that use the "next" callback', async ({
page,
}) => {
const routes = {
'/my-awesome-async-function-route': async function (
route,
matched,
next,
) {
setTimeout(() => next('# My Awesome Function Route'), 100);
},
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/my-awesome-async-function-route');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('My Awesome Function Route');
});
});
test.describe('Routes with Regex Matches', () => {
test('rendering virtual routes with regex matches', async ({ page }) => {
const routes = {
'/items/(.*)': '# Item Page',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/items/banana');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Item Page');
});
test('virtual route functions should get the route as first parameter', async ({
page,
}) => {
const routes = {
'/pets/(.*)': route => '# Route: /pets/dog',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/pets/dog');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Route: /pets/dog');
});
test('virtual route functions should get the matched array as second parameter', async ({
page,
}) => {
const routes = {
'/pets/(.*)'(_, matched) {
return `# Pets Page (${matched[1]})`;
},
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/pets/cat');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Pets Page (cat)');
});
});
test.describe('Route Matching Specifics', () => {
test('routes should be exact match if no regex was passed', async ({
page,
}) => {
const routes = {
'/my': '# Incorrect Route - only prefix',
'/route': '# Incorrect Route - only postfix',
'/my/route': '# Correct Route',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/my/route');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Correct Route');
});
test('if there are two routes that match, the first one should be taken', async ({
page,
}) => {
const routes = {
'/multiple/(.+)': '# First Match',
'/multiple/(.*)': '# Second Match',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/multiple/matches');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('First Match');
});
test('prefer virtual route over a real file, if a virtual route exists', async ({
page,
}) => {
const routes = {
'/': '# Virtual Homepage',
};
await docsifyInit({
markdown: {
homepage: '# Real File Homepage',
},
config: {
routes,
},
});
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Virtual Homepage');
});
test('fallback to default routing if no route was matched', async ({
page,
}) => {
const routes = {
'/a': '# A',
'/b': '# B',
'/c': '# C',
};
await docsifyInit({
markdown: {
homepage: '# Real File Homepage',
},
config: {
routes,
},
});
await navigateToRoute(page, '/d');
const mainElm = page.locator('#main');
await expect(mainElm).toContainText('404 - Not Found');
});
test('skip routes that returned a falsy value that is not a boolean', async ({
page,
}) => {
const routes = {
'/multiple/(.+)': () => null,
'/multiple/(.*)': () => undefined,
'/multiple/.+': () => 0,
'/multiple/.*': () => '# Last Match',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/multiple/matches');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Last Match');
});
test('abort virtual routes and not try the next one, if any matched route returned an explicit "false" boolean', async ({
page,
}) => {
const routes = {
'/multiple/(.+)': () => false,
'/multiple/(.*)': () => "# You Shouldn't See Me",
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/multiple/matches');
const mainElm = page.locator('#main');
await expect(mainElm).toContainText('404 - Not Found');
});
test('skip routes that are not a valid string or function', async ({
page,
}) => {
const routes = {
'/multiple/(.+)': 123,
'/multiple/(.*)': false,
'/multiple/.+': null,
'/multiple/..+': [],
'/multiple/..*': {},
'/multiple/.*': '# Last Match',
};
await docsifyInit({
config: {
routes,
},
});
await navigateToRoute(page, '/multiple/matches');
const titleElm = page.locator('#main h1');
await expect(titleElm).toContainText('Last Match');
});
});
});