|
1 | 1 | import { expect, test } from "@playwright/test"
|
| 2 | +import { nextBeat, nextEventNamed } from "../helpers/page" |
2 | 3 |
|
3 | 4 | test.beforeEach(async ({ page }) => {
|
4 | 5 | await page.goto("/src/tests/fixtures/autofocus.html")
|
@@ -74,3 +75,48 @@ test("receiving a Turbo Stream message with an [autofocus] element when an eleme
|
74 | 75 | })
|
75 | 76 | await expect(page.locator("#first-autofocus-element")).toBeFocused()
|
76 | 77 | })
|
| 78 | + |
| 79 | +test("focus on [autofocus] elements on page refreshes with morphing when no other element in the page has the focus", async ({ page }) => { |
| 80 | + await page.goto("/src/tests/fixtures/page_refresh.html") |
| 81 | + |
| 82 | + const input = await page.locator("input[autofocus]") |
| 83 | + |
| 84 | + await page.evaluate(() => { |
| 85 | + document.activeElement.blur() |
| 86 | + }) |
| 87 | + |
| 88 | + await nextBeat() |
| 89 | + expect(input).not.toBeFocused() |
| 90 | + |
| 91 | + await page.evaluate(() => { |
| 92 | + document.querySelector("form#with-autofocus").requestSubmit() |
| 93 | + }) |
| 94 | + |
| 95 | + await nextEventNamed(page, "turbo:render", { renderMethod: "morph" }) |
| 96 | + |
| 97 | + await expect(input).toBeFocused() |
| 98 | +}) |
| 99 | + |
| 100 | +test("don't focus on [autofocus] elements on page refreshes with morphing when other element in the page has the focus", async ({ page }) => { |
| 101 | + await page.goto("/src/tests/fixtures/page_refresh.html") |
| 102 | + |
| 103 | + const input = await page.locator("input[autofocus]") |
| 104 | + const link = await page.locator("#refresh-after-navigation-link") |
| 105 | + |
| 106 | + await page.evaluate(() => { |
| 107 | + document.getElementById("refresh-after-navigation-link").focus() |
| 108 | + }) |
| 109 | + |
| 110 | + await nextBeat() |
| 111 | + expect(link).toBeFocused() |
| 112 | + expect(input).not.toBeFocused() |
| 113 | + |
| 114 | + await page.evaluate(() => { |
| 115 | + document.querySelector("form#with-autofocus").requestSubmit() |
| 116 | + }) |
| 117 | + |
| 118 | + await nextEventNamed(page, "turbo:render", { renderMethod: "morph" }) |
| 119 | + |
| 120 | + expect(link).toBeFocused() |
| 121 | + expect(input).not.toBeFocused() |
| 122 | +}) |
0 commit comments