-
Notifications
You must be signed in to change notification settings - Fork 478
Expand file tree
/
Copy pathnavigation_tests.js
More file actions
582 lines (457 loc) · 20.9 KB
/
navigation_tests.js
File metadata and controls
582 lines (457 loc) · 20.9 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import { expect, test } from "@playwright/test"
import {
clickWithoutScrolling,
isScrolledToSelector,
nextAttributeMutationNamed,
nextBeat,
nextBody,
nextEventNamed,
noNextEventNamed,
pathname,
pathnameForIFrame,
readEventLogs,
visitAction,
willChangeBody,
withHash,
withPathname,
withSearch,
withSearchParam
} from "../helpers/page"
test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/navigation.html")
await readEventLogs(page)
})
test("navigating renders a progress bar until the next turbo:load", async ({ page }) => {
await page.evaluate(() => window.Turbo.setProgressBarDelay(0))
await page.click("#delayed-link")
await expect(page.locator(".turbo-progress-bar"), "displays progress bar").toBeAttached()
await nextEventNamed(page, "turbo:render")
await expect(page.locator(".turbo-progress-bar"), "displays progress bar").toBeAttached()
await nextEventNamed(page, "turbo:load")
await expect(page.locator(".turbo-progress-bar"), "hides progress bar").not.toBeAttached()
})
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")
await expect(page.locator(".turbo-progress-bar"), "does not show progress bar before delay").not.toBeAttached()
})
test("navigating hides the progress bar on failure", async ({ page }) => {
await page.evaluate(() => window.Turbo.setProgressBarDelay(0))
await page.click("#delayed-failure-link")
await expect(page.locator(".turbo-progress-bar")).toBeAttached()
await expect(page.locator(".turbo-progress-bar")).not.toBeAttached()
})
test("after loading the page", async ({ page }) => {
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin unannotated link", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
expect(
await nextAttributeMutationNamed(page, "html", "aria-busy"),
"sets [aria-busy] on the document element"
).toEqual(
"true"
)
expect(
await nextAttributeMutationNamed(page, "html", "aria-busy"),
"removes [aria-busy] from the document element"
).toEqual(
null
)
})
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 expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
await expect(page).toHaveURL(withSearch(""))
expect(await visitAction(page)).toEqual("advance")
})
test("drive enabled; click an element in the shadow DOM wrapped by a link in the light DOM", async ({ page }) => {
await page.click("#shadow-dom-drive-enabled span")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("drive disabled; click an element in the shadow DOM within data-turbo='false'", async ({ page }) => {
await page.click("#shadow-dom-drive-disabled span")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("drive enabled; click an element in the slot", async ({ page }) => {
await page.click("#element-in-slot")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("drive disabled; click an element in the slot within data-turbo='false'", async ({ page }) => {
await page.click("#element-in-slot-disabled")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("drive disabled; click an element in the nested slot within data-turbo='false'", async ({ page }) => {
await page.click("#element-in-nested-slot-disabled")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin unannotated link with search params", async ({ page }) => {
await page.click("#same-origin-unannotated-link-search-params")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
await expect(page).toHaveURL(withSearch("?key=value"))
expect(await visitAction(page)).toEqual("advance")
})
test("following a same-origin unannotated form[method=GET]", async ({ page }) => {
await page.click("#same-origin-unannotated-form button")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("following a same-origin data-turbo-method=get link", async ({ page }) => {
await page.click("#same-origin-get-link-form")
await nextEventNamed(page, "turbo:submit-start")
await nextEventNamed(page, "turbo:submit-end")
await nextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
await expect(page).toHaveURL(withSearchParam("a", "one"))
await expect(page).toHaveURL(withSearchParam("b", "two"))
})
test("following a same-origin data-turbo-action=replace link", async ({ page }) => {
await page.click("#same-origin-replace-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
test("following a same-origin GET form[data-turbo-action=replace]", async ({ page }) => {
await page.click("#same-origin-replace-form-get button")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
test("following a same-origin GET form button[data-turbo-action=replace]", async ({ page }) => {
await page.click("#same-origin-replace-form-submitter-get button")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
test("following a same-origin POST form[data-turbo-action=replace]", async ({ page }) => {
await page.click("#same-origin-replace-form-post button")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
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")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
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()
await expect(page.locator("some-cached-element")).not.toBeAttached()
})
test("following a same-origin POST link with data-turbo-action=replace", async ({ page }) => {
await page.click("#same-origin-replace-post-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
})
test("following a same-origin data-turbo=false link", async ({ page }) => {
await page.click("#same-origin-false-link")
await page.waitForEvent("load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin unannotated link inside a data-turbo=false container", async ({ page }) => {
await page.click("#same-origin-unannotated-link-inside-false-container")
await page.waitForEvent("load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin data-turbo=true link inside a data-turbo=false container", async ({ page }) => {
await page.click("#same-origin-true-link-inside-false-container")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("following a same-origin anchored link", async ({ page }) => {
await page.click("#same-origin-anchored-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
await expect(page).toHaveURL(withHash("#element-id"))
expect(await visitAction(page)).toEqual("advance")
expect(await isScrolledToSelector(page, "#element-id")).toEqual(true)
})
test("following a same-origin link to a named anchor", async ({ page }) => {
await page.click("#same-origin-anchored-link-named")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
await expect(page).toHaveURL(withHash("#named-anchor"))
expect(await visitAction(page)).toEqual("advance")
expect(await isScrolledToSelector(page, "[name=named-anchor]")).toEqual(true)
})
test("following a cross-origin unannotated link", async ({ page }) => {
await page.click("#cross-origin-unannotated-link")
await expect(page).toHaveURL("about:blank")
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin [target] link", async ({ page }) => {
const [popup] = await Promise.all([page.waitForEvent("popup"), page.click("#same-origin-targeted-link")])
expect(pathname(popup.url())).toEqual("/src/tests/fixtures/one.html")
expect(await visitAction(page)).toEqual("load")
})
test("following a _self [target] link", async ({ page }) => {
await page.click("#self-targeted-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("following an empty [target] link", async ({ page }) => {
await page.click("#empty-target-link")
await nextBody(page)
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("following a bare [target] link", async ({ page }) => {
await page.click("#bare-target-link")
await nextBody(page)
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("advance")
})
test("following a same-origin [download] link", async ({ page }) => {
expect(
await willChangeBody(page, async () => {
await page.click("#same-origin-download-link")
await nextBeat()
})
).toEqual(false)
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a same-origin link inside an SVG element", async ({ page }) => {
const link = page.locator("#same-origin-link-inside-svg-element")
await link.focus()
await page.keyboard.press("Enter")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("load")
})
test("following a cross-origin link inside an SVG element", async ({ page }) => {
const link = page.locator("#cross-origin-link-inside-svg-element")
await link.focus()
await page.keyboard.press("Enter")
await expect(page).toHaveURL("about:blank")
expect(await visitAction(page)).toEqual("load")
})
test("clicking the back button", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextEventNamed(page, "turbo:load")
await page.goBack()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await visitAction(page)).toEqual("restore")
})
test("clicking the forward button", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextEventNamed(page, "turbo:load")
await page.goBack()
await page.goForward()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("restore")
})
test("form submissions that redirect to a different location have a default advance action", async ({ page }) => {
await page.click("#redirect-submit")
await nextEventNamed(page, "turbo:load")
expect(await visitAction(page)).toEqual("advance")
})
test("form submissions that redirect to the current location have a default replace action", async ({ page }) => {
await page.click("#refresh-submit")
await nextEventNamed(page, "turbo:load")
expect(await visitAction(page)).toEqual("replace")
})
test("link targeting a disabled turbo-frame navigates the page", async ({ page }) => {
await page.click("#link-to-disabled-frame")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/frames/hello.html"))
})
test("skip link with hash-only path scrolls to the anchor without a visit", async ({ page }) => {
expect(
await willChangeBody(page, async () => {
await page.click('a[href="#main"]')
})
).not.toBeTruthy()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
await expect(page).toHaveURL(withHash("#main"))
expect(await isScrolledToSelector(page, "#main"), "scrolled to #main").toEqual(true)
})
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")
await expect(page.locator("#ignored-link"), "skips interactive elements before #main").not.toBeFocused()
await expect(page.locator("#main *:focus"), "moves focus inside #main").toBeFocused()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
await expect(page).toHaveURL(withHash("#main"))
})
test("same-page anchored replace link assumes the intention was a refresh", async ({ page }) => {
await page.click("#refresh-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
await expect(page).toHaveURL(withHash("#main"))
expect(await isScrolledToSelector(page, "#main"), "scrolled to #main").toEqual(true)
})
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)
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
await expect(page).toHaveURL(withHash("#main"))
expect(await isScrolledToSelector(page, "#main"), "scrolled to #main").toEqual(true)
})
test("following a redirection", async ({ page }) => {
await page.click("#redirection-link")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
expect(await visitAction(page)).toEqual("replace")
await expect(page.locator(".turbo-progress-bar")).not.toBeAttached()
})
test("clicking the back button after redirection", async ({ page }) => {
await page.click("#redirection-link")
await nextBody(page)
await page.goBack()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await visitAction(page)).toEqual("restore")
})
test("hash-only navigation does not trigger a visit via popstate", async ({ page }) => {
await page.click('a[href="#main"]')
await nextBeat()
expect(
await noNextEventNamed(page, "turbo:before-visit"),
"hash-only click does not trigger turbo:before-visit"
).toEqual(true)
expect(
await noNextEventNamed(page, "turbo:load"),
"hash-only click does not trigger turbo:load"
).toEqual(true)
await expect(page).toHaveURL(withHash("#main"))
})
test("navigating back after hash-only change does not replace the body", async ({ page }) => {
await page.click('a[href="#main"]')
await nextBeat()
await page.click('a[href="#ignored-link"]')
await nextBeat()
await expect(page).toHaveURL(withHash("#ignored-link"))
expect(
await willChangeBody(page, async () => {
await page.goBack()
await nextBeat()
})
).not.toBeTruthy()
await expect(page).toHaveURL(withHash("#main"))
})
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"]')
expect(await noNextEventNamed(page, eventName), `same-page links do not trigger ${eventName} events`).toEqual(true)
}
})
test("correct referrer header", async ({ page }) => {
page.click("#headers-link")
await nextEventNamed(page, "turbo:load")
const pre = await page.textContent("pre")
const headers = await JSON.parse(pre || "")
expect(
headers.referer,
`referer header is correctly set`
).toEqual(
"http://localhost:9000/src/tests/fixtures/navigation.html"
)
})
test("double-clicking on a link", async ({ page }) => {
await page.click("#delayed-link", { clickCount: 2 })
await nextBeat()
await nextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/__turbo/delayed_response"))
expect(await visitAction(page)).toEqual("advance")
})
test("does not fire turbo:load twice after following a redirect", async ({ page }) => {
await page.evaluate(() => {
window.turboLoadCount = 0
addEventListener("turbo:load", () => window.turboLoadCount++)
})
page.click("#redirection-link")
await nextBeat() // 301 redirect response
await nextBeat() // 200 response
await nextBody(page)
await nextEventNamed(page, "turbo:load")
await nextBeat()
expect(await page.evaluate(() => window.turboLoadCount)).toEqual(1)
})
test("navigating back whilst a visit is in-flight", async ({ page }) => {
page.click("#delayed-link")
await nextEventNamed(page, "turbo:before-render")
await page.goBack()
expect(
await nextEventNamed(page, "turbo:visit"),
"navigating back whilst a visit is in-flight starts a non-silent Visit"
).toBeTruthy()
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await visitAction(page)).toEqual("restore")
})
test("ignores links with a [target] attribute that target an iframe with a matching [name]", async ({ page }) => {
await page.click("#link-target-iframe")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await pathnameForIFrame(page, "iframe")).toEqual("/src/tests/fixtures/one.html")
})
test("ignores links with a [target] attribute that targets an iframe with [name='']", async ({ page }) => {
await page.click("#link-target-empty-name-iframe")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
})
test("ignores forms with a [target=_blank] attribute", async ({ page }) => {
const [popup] = await Promise.all([page.waitForEvent("popup"), page.click("#form-target-blank button")])
await expect(popup).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
})
test("ignores forms with a [target] attribute that targets an iframe with a matching [name]", async ({ page }) => {
await page.click("#form-target-iframe button")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await pathnameForIFrame(page, "iframe")).toEqual("/src/tests/fixtures/one.html")
})
test("ignores forms with a button[formtarget=_blank] attribute", async ({ page }) => {
const [popup] = await Promise.all([page.waitForEvent("popup"), page.click("#button-formtarget-blank")])
await expect(popup).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
})
test("ignores forms with a button[formtarget] attribute that targets an iframe with [name='']", async ({ page }) => {
await page.click("#form-target-empty-name-iframe button")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
})
test("ignores forms with a button[formtarget] attribute that targets an iframe with a matching [name]", async ({
page
}) => {
await page.click("#button-formtarget-iframe")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/navigation.html"))
expect(await pathnameForIFrame(page, "iframe")).toEqual("/src/tests/fixtures/one.html")
})
test("ignores forms with a [target] attribute that target an iframe with [name='']", async ({ page }) => {
await page.click("#button-formtarget-empty-name-iframe")
await nextBeat()
await noNextEventNamed(page, "turbo:load")
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/one.html"))
})