|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 3 | +test.describe("AttributeMap", () => { |
| 4 | + test.beforeEach(async ({ page }) => { |
| 5 | + await page.goto("/fixtures/attribute-map/"); |
| 6 | + await page.waitForSelector("attribute-map-test-element"); |
| 7 | + }); |
| 8 | + |
| 9 | + test("should define @attr for a simple leaf property", async ({ page }) => { |
| 10 | + const element = page.locator("attribute-map-test-element"); |
| 11 | + |
| 12 | + const hasFooAccessor = await element.evaluate(node => { |
| 13 | + const desc = Object.getOwnPropertyDescriptor( |
| 14 | + Object.getPrototypeOf(node), |
| 15 | + "foo", |
| 16 | + ); |
| 17 | + return typeof desc?.get === "function"; |
| 18 | + }); |
| 19 | + |
| 20 | + expect(hasFooAccessor).toBeTruthy(); |
| 21 | + }); |
| 22 | + |
| 23 | + test("should define @attr for a dash-case property", async ({ page }) => { |
| 24 | + const element = page.locator("attribute-map-test-element"); |
| 25 | + |
| 26 | + const hasFooBarAccessor = await element.evaluate(node => { |
| 27 | + const desc = Object.getOwnPropertyDescriptor( |
| 28 | + Object.getPrototypeOf(node), |
| 29 | + "foo-bar", |
| 30 | + ); |
| 31 | + return typeof desc?.get === "function"; |
| 32 | + }); |
| 33 | + |
| 34 | + expect(hasFooBarAccessor).toBeTruthy(); |
| 35 | + }); |
| 36 | + |
| 37 | + test("should use binding key as-is for both attribute and property name", async ({ |
| 38 | + page, |
| 39 | + }) => { |
| 40 | + const element = page.locator("attribute-map-test-element"); |
| 41 | + |
| 42 | + // Setting the foo-bar attribute should update the foo-bar property (no conversion) |
| 43 | + await element.evaluate(node => node.setAttribute("foo-bar", "dash-case-test")); |
| 44 | + const propValue = await element.evaluate(node => (node as any)["foo-bar"]); |
| 45 | + |
| 46 | + expect(propValue).toBe("dash-case-test"); |
| 47 | + }); |
| 48 | + |
| 49 | + test("should not define @attr for event handler methods", async ({ page }) => { |
| 50 | + const element = page.locator("attribute-map-test-element"); |
| 51 | + |
| 52 | + // @click="{setFoo()}" etc. produce "event" type bindings — excluded from schema. |
| 53 | + // Regular methods have a value descriptor, not a getter/setter. |
| 54 | + const results = await element.evaluate(node => { |
| 55 | + const proto = Object.getPrototypeOf(node); |
| 56 | + const isAccessor = (name: string) => { |
| 57 | + const desc = Object.getOwnPropertyDescriptor(proto, name); |
| 58 | + return typeof desc?.get === "function"; |
| 59 | + }; |
| 60 | + return { |
| 61 | + setFoo: isAccessor("setFoo"), |
| 62 | + setFooBar: isAccessor("setFooBar"), |
| 63 | + setMultiple: isAccessor("setMultiple"), |
| 64 | + }; |
| 65 | + }); |
| 66 | + |
| 67 | + expect(results.setFoo).toBe(false); |
| 68 | + expect(results.setFooBar).toBe(false); |
| 69 | + expect(results.setMultiple).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + test("should update template when attribute is set via setAttribute", async ({ |
| 73 | + page, |
| 74 | + }) => { |
| 75 | + const element = page.locator("attribute-map-test-element"); |
| 76 | + |
| 77 | + await element.evaluate(node => node.setAttribute("foo", "attr-value")); |
| 78 | + |
| 79 | + await expect(page.locator(".foo-value")).toHaveText("attr-value"); |
| 80 | + }); |
| 81 | + |
| 82 | + test("should update template when dash-case attribute is set via setAttribute", async ({ |
| 83 | + page, |
| 84 | + }) => { |
| 85 | + const element = page.locator("attribute-map-test-element"); |
| 86 | + |
| 87 | + await element.evaluate(node => node.setAttribute("foo-bar", "bar-attr-value")); |
| 88 | + |
| 89 | + await expect(page.locator(".foo-bar-value")).toHaveText("bar-attr-value"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("should reflect property value back to attribute", async ({ page }) => { |
| 93 | + const element = page.locator("attribute-map-test-element"); |
| 94 | + |
| 95 | + await element.evaluate(node => { |
| 96 | + (node as any).foo = "reflected"; |
| 97 | + }); |
| 98 | + |
| 99 | + // FAST reflects attributes asynchronously via Updates.enqueue |
| 100 | + await page.evaluate(() => new Promise(r => requestAnimationFrame(r))); |
| 101 | + |
| 102 | + const attrValue = await element.evaluate(node => node.getAttribute("foo")); |
| 103 | + expect(attrValue).toBe("reflected"); |
| 104 | + }); |
| 105 | + |
| 106 | + test("should update definition attributeLookup for simple properties", async ({ |
| 107 | + page, |
| 108 | + }) => { |
| 109 | + const element = page.locator("attribute-map-test-element"); |
| 110 | + |
| 111 | + // setAttribute triggers attributeChangedCallback via attributeLookup |
| 112 | + await element.evaluate(node => node.setAttribute("foo", "lookup-test")); |
| 113 | + const propValue = await element.evaluate(node => (node as any).foo); |
| 114 | + |
| 115 | + expect(propValue).toBe("lookup-test"); |
| 116 | + }); |
| 117 | + |
| 118 | + test("should update definition attributeLookup for dash-case properties", async ({ |
| 119 | + page, |
| 120 | + }) => { |
| 121 | + const element = page.locator("attribute-map-test-element"); |
| 122 | + |
| 123 | + // setAttribute with foo-bar triggers attributeChangedCallback for the foo-bar property |
| 124 | + await element.evaluate(node => node.setAttribute("foo-bar", "lookup-bar-test")); |
| 125 | + const propValue = await element.evaluate(node => (node as any)["foo-bar"]); |
| 126 | + |
| 127 | + expect(propValue).toBe("lookup-bar-test"); |
| 128 | + }); |
| 129 | + |
| 130 | + test("should not overwrite an existing @attr accessor", async ({ page }) => { |
| 131 | + await page.waitForSelector("attribute-map-existing-attr-test-element"); |
| 132 | + const element = page.locator("attribute-map-existing-attr-test-element"); |
| 133 | + |
| 134 | + // The @attr default value must survive AttributeMap processing |
| 135 | + const defaultValue = await element.evaluate(node => (node as any).foo); |
| 136 | + expect(defaultValue).toBe("original"); |
| 137 | + |
| 138 | + // setAttribute must still work via the original @attr definition |
| 139 | + await element.evaluate(node => node.setAttribute("foo", "updated")); |
| 140 | + const updatedValue = await element.evaluate(node => (node as any).foo); |
| 141 | + expect(updatedValue).toBe("updated"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments