forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
navigation_tests.ts
383 lines (322 loc) · 13.2 KB
/
navigation_tests.ts
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { test } from "@playwright/test"
import { assert } from "chai"
import {
clickWithoutScrolling,
hash,
hasSelector,
isScrolledToSelector,
nextAttributeMutationNamed,
nextBeat,
nextBody,
nextEventNamed,
noNextEventNamed,
pathname,
readEventLogs,
search,
selectorHasFocus,
visitAction,
waitUntilSelector,
waitUntilNoSelector,
willChangeBody,
} from "../helpers/page"
test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/navigation.html")
await readEventLogs(page)
})
test("test navigating renders a progress bar", async ({ page }) => {
assert.equal(
await page.locator("style").evaluate((style) => style.nonce),
"123",
"renders progress bar stylesheet inline with nonce"
)
await page.evaluate(() => window.Turbo.setProgressBarDelay(0))
await page.click("#delayed-link")
await waitUntilSelector(page, ".turbo-progress-bar")
assert.ok(await hasSelector(page, ".turbo-progress-bar"), "displays progress bar")
await nextEventNamed(page, "turbo:load")
await waitUntilNoSelector(page, ".turbo-progress-bar")
assert.notOk(await hasSelector(page, ".turbo-progress-bar"), "hides progress bar")
})
test("test navigating does not render a progress bar before expiring the delay", async ({ page }) => {
await page.evaluate(() => window.Turbo.setProgressBarDelay(1000))
await page.click("#same-origin-unannotated-link")
assert.notOk(await hasSelector(page, ".turbo-progress-bar"), "does not show progress bar before delay")
})
test("test after loading the page", async ({ page }) => {
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
assert.equal(await visitAction(page), "load")
})
test("test following a same-origin unannotated link", async ({ page }) => {
page.click("#same-origin-unannotated-link")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "advance")
assert.equal(
await nextAttributeMutationNamed(page, "html", "aria-busy"),
"true",
"sets [aria-busy] on the document element"
)
assert.equal(
await nextAttributeMutationNamed(page, "html", "aria-busy"),
null,
"removes [aria-busy] from the document element"
)
})
test("test following a same-origin unannotated custom element link", async ({ page }) => {
await nextBeat()
await page.evaluate(() => {
const shadowRoot = document.querySelector("#custom-link-element")?.shadowRoot
const link = shadowRoot?.querySelector("a")
link?.click()
})
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(search(page.url()), "")
assert.equal(await visitAction(page), "advance")
})
test("test following a same-origin unannotated link with search params", async ({ page }) => {
page.click("#same-origin-unannotated-link-search-params")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(search(page.url()), "?key=value")
assert.equal(await visitAction(page), "advance")
})
test("test following a same-origin unannotated form[method=GET]", async ({ page }) => {
page.click("#same-origin-unannotated-form button")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "advance")
})
test("test following a same-origin data-turbo-action=replace link", async ({ page }) => {
page.click("#same-origin-replace-link")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test following a same-origin GET form[data-turbo-action=replace]", async ({ page }) => {
page.click("#same-origin-replace-form-get button")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test following a same-origin GET form button[data-turbo-action=replace]", async ({ page }) => {
page.click("#same-origin-replace-form-submitter-get button")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test following a same-origin POST form[data-turbo-action=replace]", async ({ page }) => {
page.click("#same-origin-replace-form-post button")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test following a same-origin POST form button[data-turbo-action=replace]", async ({ page }) => {
await page.click("#same-origin-replace-form-submitter-post button")
await nextEventNamed(page, "turbo:load")
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test following a POST form clears cache", async ({ page }) => {
await page.evaluate(() => {
const cachedElement = document.createElement("some-cached-element")
document.body.appendChild(cachedElement)
})
await page.click("#form-post-submit")
await nextBeat() // 301 redirect response
await nextBeat() // 200 response
await page.goBack()
assert.notOk(await hasSelector(page, "some-cached-element"))
})
test("test following a same-origin data-turbo=false link", async ({ page }) => {
page.click("#same-origin-false-link")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "load")
})
test("test following a same-origin unannotated link inside a data-turbo=false container", async ({ page }) => {
page.click("#same-origin-unannotated-link-inside-false-container")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "load")
})
test("test following a same-origin data-turbo=true link inside a data-turbo=false container", async ({ page }) => {
page.click("#same-origin-true-link-inside-false-container")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "advance")
})
test("test following a same-origin anchored link", async ({ page }) => {
await page.click("#same-origin-anchored-link")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(hash(page.url()), "#element-id")
assert.equal(await visitAction(page), "advance")
assert(await isScrolledToSelector(page, "#element-id"))
})
test("test following a same-origin link to a named anchor", async ({ page }) => {
await page.click("#same-origin-anchored-link-named")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(hash(page.url()), "#named-anchor")
assert.equal(await visitAction(page), "advance")
assert(await isScrolledToSelector(page, "[name=named-anchor]"))
})
test("test following a cross-origin unannotated link", async ({ page }) => {
await page.click("#cross-origin-unannotated-link")
await nextBody(page)
assert.equal(page.url(), "about:blank")
assert.equal(await visitAction(page), "load")
})
test("test following a same-origin [target] link", async ({ page }) => {
const [popup] = await Promise.all([page.waitForEvent("popup"), page.click("#same-origin-targeted-link")])
assert.equal(pathname(popup.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(popup), "load")
})
test("test following a same-origin [download] link", async ({ page }) => {
assert.notOk<boolean>(
await willChangeBody(page, async () => {
await page.click("#same-origin-download-link")
await nextBeat()
})
)
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
assert.equal(await visitAction(page), "load")
})
test("test following a same-origin link inside an SVG element", async ({ page }) => {
await page.click("#same-origin-link-inside-svg-element", { force: true })
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "advance")
})
test("test following a cross-origin link inside an SVG element", async ({ page }) => {
await page.click("#cross-origin-link-inside-svg-element", { force: true })
await nextBody(page)
assert.equal(page.url(), "about:blank")
assert.equal(await visitAction(page), "load")
})
test("test clicking the back button", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextBody(page)
await page.goBack()
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
assert.equal(await visitAction(page), "restore")
})
test("test clicking the forward button", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextBody(page)
await page.goBack()
await page.goForward()
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "restore")
})
test("test link targeting a disabled turbo-frame navigates the page", async ({ page }) => {
await page.click("#link-to-disabled-frame")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/frames/hello.html")
})
test("test skip link with hash-only path scrolls to the anchor without a visit", async ({ page }) => {
assert.notOk<boolean>(
await willChangeBody(page, async () => {
await page.click('a[href="#main"]')
await nextBeat()
})
)
assert.ok(await isScrolledToSelector(page, "#main"), "scrolled to #main")
})
test("test skip link with hash-only path moves focus and changes tab order", async ({ page }) => {
await page.click('a[href="#main"]')
await nextBeat()
await page.press("#main", "Tab")
assert.notOk(await selectorHasFocus(page, "#ignored-link"), "skips interactive elements before #main")
assert.ok(
await selectorHasFocus(page, "#same-origin-unannotated-link"),
"skips to first interactive element after #main"
)
})
test("test same-page anchored replace link assumes the intention was a refresh", async ({ page }) => {
await page.click("#refresh-link")
await nextBody(page)
assert.ok(await isScrolledToSelector(page, "#main"), "scrolled to #main")
})
test("test navigating back to anchored URL", async ({ page }) => {
await clickWithoutScrolling(page, 'a[href="#main"]', { hasText: "Skip Link" })
await nextBeat()
await clickWithoutScrolling(page, "#same-origin-unannotated-link")
await nextBody(page)
await nextBeat()
await page.goBack()
await nextBody(page)
assert.ok(await isScrolledToSelector(page, "#main"), "scrolled to #main")
})
test("test following a redirection", async ({ page }) => {
await page.click("#redirection-link")
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html")
assert.equal(await visitAction(page), "replace")
})
test("test clicking the back button after redirection", async ({ page }) => {
await page.click("#redirection-link")
await nextBody(page)
await page.goBack()
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
assert.equal(await visitAction(page), "restore")
})
test("test same-page anchor visits do not trigger visit events", async ({ page }) => {
const events = [
"turbo:before-visit",
"turbo:visit",
"turbo:before-cache",
"turbo:before-render",
"turbo:render",
"turbo:load",
]
for (const eventName in events) {
await page.goto("/src/tests/fixtures/navigation.html")
await page.click('a[href="#main"]')
assert.ok(await noNextEventNamed(page, eventName), `same-page links do not trigger ${eventName} events`)
}
})
test("test correct referrer header", async ({ page }) => {
page.click("#headers-link")
await nextBody(page)
const pre = await page.textContent("pre")
const headers = await JSON.parse(pre || "")
assert.equal(
headers.referer,
"http://localhost:9000/src/tests/fixtures/navigation.html",
`referer header is correctly set`
)
})
test("test double-clicking on a link", async ({ page }) => {
await page.click("#delayed-link", { clickCount: 2 })
await nextBeat()
await nextEventNamed(page, "turbo:load")
assert.equal(pathname(page.url()), "/__turbo/delayed_response")
assert.equal(await visitAction(page), "advance")
})
test("test does not fire turbo:load twice after following a redirect", async ({ page }) => {
page.click("#redirection-link")
await nextBeat() // 301 redirect response
await noNextEventNamed(page, "turbo:load")
await nextBeat() // 200 response
await nextBody(page)
await nextEventNamed(page, "turbo:load")
})
test("test navigating back whilst a visit is in-flight", async ({ page }) => {
page.click("#delayed-link")
await nextEventNamed(page, "turbo:before-render")
await page.goBack()
assert.ok(
await nextEventNamed(page, "turbo:visit"),
"navigating back whilst a visit is in-flight starts a non-silent Visit"
)
await nextBody(page)
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
assert.equal(await visitAction(page), "restore")
})
test("test ignores links that target an iframe", async ({ page }) => {
await page.click("#targets-iframe")
await nextBeat()
assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
})