|
1 | 1 | import docsifyInit from '../helpers/docsify-init.js';
|
| 2 | +import { waitForFunction } from '../helpers/wait-for.js'; |
2 | 3 | import { test, expect } from './fixtures/docsify-init-fixture.js';
|
3 | 4 |
|
4 | 5 | test.describe('Plugins', () => {
|
@@ -72,84 +73,155 @@ test.describe('Plugins', () => {
|
72 | 73 | expect(consoleMsgs).toEqual(expectedMsgs);
|
73 | 74 | });
|
74 | 75 |
|
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'); |
87 | 92 | });
|
88 | 93 |
|
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 | + }); |
90 | 115 | });
|
91 | 116 |
|
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'); |
109 | 136 | });
|
110 | 137 |
|
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 | + }); |
112 | 159 | });
|
113 | 160 |
|
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 | + }); |
129 | 173 | });
|
130 | 174 |
|
131 |
| - await expect(page.locator('#main')).toContainText('afterEach'); |
132 |
| - }); |
| 175 | + test.afterEach(async ({ page }) => { |
| 176 | + routeData = null; |
| 177 | + }); |
133 | 178 |
|
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'); |
151 | 196 | });
|
152 | 197 |
|
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 | + }); |
154 | 226 | });
|
155 | 227 | });
|
0 commit comments