|
| 1 | +import { test, expect } from "../../test-fixtures"; |
| 2 | +import { syncLV, evalLV } from "../../utils"; |
| 3 | + |
| 4 | +// https://github.com/phoenixframework/phoenix_live_view/pull/4323 |
| 5 | +// |
| 6 | +// Form-Associated Custom Elements (FACEs) store their value in |
| 7 | +// ElementInternals (via setFormValue), not in DOM children. When a FACE |
| 8 | +// host has focus, morphdom should still descend into its light-DOM children |
| 9 | +// to patch them normally. These tests cover the fix and verify no |
| 10 | +// regressions for related scenarios. |
| 11 | + |
| 12 | +const incrementCounter = async (page) => { |
| 13 | + await evalLV( |
| 14 | + page, |
| 15 | + "{:noreply, Phoenix.Component.assign(socket, :counter, socket.assigns.counter + 1)}", |
| 16 | + ); |
| 17 | + await syncLV(page); |
| 18 | +}; |
| 19 | + |
| 20 | +test.describe("FACE children patching", () => { |
| 21 | + test.beforeEach(async ({ page }) => { |
| 22 | + await page.goto("/issues/4323"); |
| 23 | + await syncLV(page); |
| 24 | + }); |
| 25 | + |
| 26 | + test("FACE host focused: light DOM children are patched", async ({ |
| 27 | + page, |
| 28 | + }) => { |
| 29 | + await expect(page.locator("#case1-child")).toHaveText("count:0"); |
| 30 | + |
| 31 | + await page.locator("#case1").focus(); |
| 32 | + await expect(page.locator("#case1")).toBeFocused(); |
| 33 | + |
| 34 | + await incrementCounter(page); |
| 35 | + |
| 36 | + await expect(page.locator("#case1-child")).toHaveText("count:1"); |
| 37 | + }); |
| 38 | + |
| 39 | + test("FACE host focused with input child: children are patched", async ({ |
| 40 | + page, |
| 41 | + }) => { |
| 42 | + await expect(page.locator("#case2-child")).toHaveText("count:0"); |
| 43 | + |
| 44 | + // Focus the FACE host, NOT the input inside it |
| 45 | + await page.locator("#case2").focus(); |
| 46 | + await expect(page.locator("#case2")).toBeFocused(); |
| 47 | + |
| 48 | + await incrementCounter(page); |
| 49 | + |
| 50 | + await expect(page.locator("#case2-child")).toHaveText("count:1"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("FACE with slotted input focused: input is protected, siblings patched", async ({ |
| 54 | + page, |
| 55 | + }) => { |
| 56 | + await expect(page.locator("#case3-child")).toHaveText("count:0"); |
| 57 | + |
| 58 | + // Focus the slotted input (not the FACE host) |
| 59 | + await page.locator("#case3-input").focus(); |
| 60 | + await expect(page.locator("#case3-input")).toBeFocused(); |
| 61 | + await page.locator("#case3-input").fill("user-typed"); |
| 62 | + |
| 63 | + await incrementCounter(page); |
| 64 | + |
| 65 | + // Input value is preserved by normal focus-skip |
| 66 | + await expect(page.locator("#case3-input")).toHaveValue("user-typed"); |
| 67 | + // Sibling children are still patched (FACE host is not focused) |
| 68 | + await expect(page.locator("#case3-child")).toHaveText("count:1"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("FACE with delegatesFocus: light DOM children are patched", async ({ |
| 72 | + page, |
| 73 | + }) => { |
| 74 | + await expect(page.locator("#case4-child")).toHaveText("count:0"); |
| 75 | + |
| 76 | + // Click the FACE host; delegatesFocus sends focus to the shadow input, |
| 77 | + // but document.activeElement still returns the shadow host |
| 78 | + await page.locator("#case4").click(); |
| 79 | + const activeId = await page.evaluate(() => document.activeElement?.id); |
| 80 | + expect(activeId).toBe("case4"); |
| 81 | + |
| 82 | + await incrementCounter(page); |
| 83 | + |
| 84 | + await expect(page.locator("#case4-child")).toHaveText("count:1"); |
| 85 | + }); |
| 86 | + |
| 87 | + test("non-FACE custom element: children are patched normally", async ({ |
| 88 | + page, |
| 89 | + }) => { |
| 90 | + await expect(page.locator("#case5-child")).toHaveText("count:0"); |
| 91 | + |
| 92 | + await page.locator("#case5").focus(); |
| 93 | + await expect(page.locator("#case5")).toBeFocused(); |
| 94 | + |
| 95 | + await incrementCounter(page); |
| 96 | + |
| 97 | + // Non-FACE elements are not treated as editable inputs, |
| 98 | + // so morphdom patches them normally regardless of focus |
| 99 | + await expect(page.locator("#case5-child")).toHaveText("count:1"); |
| 100 | + }); |
| 101 | +}); |
0 commit comments