Skip to content

Commit a2539a6

Browse files
committed
Relocate route data tests
1 parent 3e40714 commit a2539a6

File tree

2 files changed

+138
-112
lines changed

2 files changed

+138
-112
lines changed

test/e2e/plugins.test.js

Lines changed: 138 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import docsifyInit from '../helpers/docsify-init.js';
2+
import { waitForFunction } from '../helpers/wait-for.js';
23
import { test, expect } from './fixtures/docsify-init-fixture.js';
34

45
test.describe('Plugins', () => {
@@ -72,84 +73,155 @@ test.describe('Plugins', () => {
7273
expect(consoleMsgs).toEqual(expectedMsgs);
7374
});
7475

75-
test('beforeEach() return value', async ({ page }) => {
76-
await docsifyInit({
77-
config: {
78-
plugins: [
79-
function (hook, vm) {
80-
hook.beforeEach(markdown => {
81-
return 'beforeEach';
82-
});
83-
},
84-
],
85-
},
86-
// _logHTML: true,
76+
test.describe('beforeEach()', () => {
77+
test('return value', async ({ page }) => {
78+
await docsifyInit({
79+
config: {
80+
plugins: [
81+
function (hook, vm) {
82+
hook.beforeEach(markdown => {
83+
return 'beforeEach';
84+
});
85+
},
86+
],
87+
},
88+
// _logHTML: true,
89+
});
90+
91+
await expect(page.locator('#main')).toContainText('beforeEach');
8792
});
8893

89-
await expect(page.locator('#main')).toContainText('beforeEach');
94+
test('async return value', async ({ page }) => {
95+
await docsifyInit({
96+
config: {
97+
plugins: [
98+
function (hook, vm) {
99+
hook.beforeEach((markdown, next) => {
100+
setTimeout(() => {
101+
next('beforeEach');
102+
}, 100);
103+
});
104+
},
105+
],
106+
},
107+
markdown: {
108+
homepage: '# Hello World',
109+
},
110+
// _logHTML: true,
111+
});
112+
113+
await expect(page.locator('#main')).toContainText('beforeEach');
114+
});
90115
});
91116

92-
test('beforeEach() async return value', async ({ page }) => {
93-
await docsifyInit({
94-
config: {
95-
plugins: [
96-
function (hook, vm) {
97-
hook.beforeEach((markdown, next) => {
98-
setTimeout(() => {
99-
next('beforeEach');
100-
}, 100);
101-
});
102-
},
103-
],
104-
},
105-
markdown: {
106-
homepage: '# Hello World',
107-
},
108-
// _logHTML: true,
117+
test.describe('afterEach()', () => {
118+
test('return value', async ({ page }) => {
119+
await docsifyInit({
120+
config: {
121+
plugins: [
122+
function (hook, vm) {
123+
hook.afterEach(html => {
124+
return '<p>afterEach</p>';
125+
});
126+
},
127+
],
128+
},
129+
markdown: {
130+
homepage: '# Hello World',
131+
},
132+
// _logHTML: true,
133+
});
134+
135+
await expect(page.locator('#main')).toContainText('afterEach');
109136
});
110137

111-
await expect(page.locator('#main')).toContainText('beforeEach');
138+
test('async return value', async ({ page }) => {
139+
await docsifyInit({
140+
config: {
141+
plugins: [
142+
function (hook, vm) {
143+
hook.afterEach((html, next) => {
144+
setTimeout(() => {
145+
next('<p>afterEach</p>');
146+
}, 100);
147+
});
148+
},
149+
],
150+
},
151+
markdown: {
152+
homepage: '# Hello World',
153+
},
154+
// _logHTML: true,
155+
});
156+
157+
await expect(page.locator('#main')).toContainText('afterEach');
158+
});
112159
});
113160

114-
test('afterEach() return value', async ({ page }) => {
115-
await docsifyInit({
116-
config: {
117-
plugins: [
118-
function (hook, vm) {
119-
hook.afterEach(html => {
120-
return '<p>afterEach</p>';
121-
});
122-
},
123-
],
124-
},
125-
markdown: {
126-
homepage: '# Hello World',
127-
},
128-
// _logHTML: true,
161+
test.describe('route data accessible to plugins', () => {
162+
let routeData = null;
163+
164+
test.beforeEach(async ({ page }) => {
165+
// Store route data set via plugin hook (below)
166+
page.on('console', async msg => {
167+
for (const arg of msg.args()) {
168+
const val = await arg.jsonValue();
169+
const obj = JSON.parse(val);
170+
obj.response && (routeData = obj);
171+
}
172+
});
129173
});
130174

131-
await expect(page.locator('#main')).toContainText('afterEach');
132-
});
175+
test.afterEach(async ({ page }) => {
176+
routeData = null;
177+
});
133178

134-
test('afterEach() async return value', async ({ page }) => {
135-
await docsifyInit({
136-
config: {
137-
plugins: [
138-
function (hook, vm) {
139-
hook.afterEach((html, next) => {
140-
setTimeout(() => {
141-
next('<p>afterEach</p>');
142-
}, 100);
143-
});
144-
},
145-
],
146-
},
147-
markdown: {
148-
homepage: '# Hello World',
149-
},
150-
// _logHTML: true,
179+
test('success (200)', async ({ page }) => {
180+
await docsifyInit({
181+
config: {
182+
plugins: [
183+
function (hook, vm) {
184+
hook.doneEach(html => {
185+
console.log(JSON.stringify(vm.route));
186+
});
187+
},
188+
],
189+
},
190+
});
191+
192+
expect(routeData).toHaveProperty('response');
193+
expect(routeData.response).toHaveProperty('ok', true);
194+
expect(routeData.response).toHaveProperty('status', 200);
195+
expect(routeData.response).toHaveProperty('statusText', 'OK');
151196
});
152197

153-
await expect(page.locator('#main')).toContainText('afterEach');
198+
test('fail (404)', async ({ page }) => {
199+
const link404Elm = page.locator('a[href*="404"]');
200+
201+
await docsifyInit({
202+
markdown: {
203+
sidebar: `
204+
- [404](404.md)
205+
`,
206+
},
207+
config: {
208+
plugins: [
209+
function (hook, vm) {
210+
hook.doneEach(html => {
211+
console.log(JSON.stringify(vm.route));
212+
});
213+
},
214+
],
215+
},
216+
});
217+
218+
await link404Elm.click();
219+
await waitForFunction(() => routeData?.response?.status === 404);
220+
221+
expect(routeData).toHaveProperty('response');
222+
expect(routeData.response).toHaveProperty('ok', false);
223+
expect(routeData.response).toHaveProperty('status', 404);
224+
expect(routeData.response).toHaveProperty('statusText', 'Not Found');
225+
});
154226
});
155227
});

test/e2e/virtual-routes.test.js

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import docsifyInit from '../helpers/docsify-init.js';
22
import { test, expect } from './fixtures/docsify-init-fixture.js';
3-
import { waitForFunction } from '../helpers/wait-for.js';
43

54
/**
65
* Navigate to a specific route in the site
@@ -291,49 +290,4 @@ test.describe('Virtual Routes - Generate Dynamic Content via Config', () => {
291290
await expect(titleElm).toContainText('Last Match');
292291
});
293292
});
294-
295-
test.describe('Route Data', () => {
296-
test('route data accessible to plugins', async ({ page }) => {
297-
const failLink = page.locator('a[href*="fail"]');
298-
299-
let routeData = null;
300-
301-
// Store route data set via plugin hook (below)
302-
page.on('console', async msg => {
303-
for (const arg of msg.args()) {
304-
const val = await arg.jsonValue();
305-
const obj = JSON.parse(val);
306-
obj.response && (routeData = obj);
307-
}
308-
});
309-
310-
await docsifyInit({
311-
markdown: {
312-
homepage: '[Fail](fail.md)',
313-
},
314-
config: {
315-
plugins: [
316-
function (hook, vm) {
317-
hook.doneEach(html => {
318-
console.log(JSON.stringify(vm.route));
319-
});
320-
},
321-
],
322-
},
323-
});
324-
325-
expect(routeData).toHaveProperty('response');
326-
expect(routeData.response).toHaveProperty('ok', true);
327-
expect(routeData.response).toHaveProperty('status', 200);
328-
expect(routeData.response).toHaveProperty('statusText', 'OK');
329-
330-
await failLink.click();
331-
await waitForFunction(() => routeData?.response?.status !== 200);
332-
333-
expect(routeData).toHaveProperty('response');
334-
expect(routeData.response).toHaveProperty('ok', false);
335-
expect(routeData.response).toHaveProperty('status', 404);
336-
expect(routeData.response).toHaveProperty('statusText', 'Not Found');
337-
});
338-
});
339293
});

0 commit comments

Comments
 (0)