|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { Window } from "happy-dom"; |
| 4 | + |
| 5 | +function setupGlobals(window) { |
| 6 | + window.SyntaxError = SyntaxError; |
| 7 | + window.TypeError = TypeError; |
| 8 | + window.Error = Error; |
| 9 | + const g = globalThis; |
| 10 | + g.window = window; |
| 11 | + g.document = window.document; |
| 12 | + g.Node = window.Node; |
| 13 | + g.Element = window.Element; |
| 14 | + g.HTMLElement = window.HTMLElement; |
| 15 | + g.DocumentFragment = window.DocumentFragment; |
| 16 | + g.Text = window.Text; |
| 17 | + g.Comment = window.Comment; |
| 18 | + g.Document = window.Document; |
| 19 | + g.DOMParser = window.DOMParser; |
| 20 | + g.MutationObserver = window.MutationObserver; |
| 21 | + g.CustomEvent = window.CustomEvent; |
| 22 | + g.Event = window.Event; |
| 23 | + g.MouseEvent = window.MouseEvent; |
| 24 | + g.KeyboardEvent = window.KeyboardEvent; |
| 25 | + g.NodeFilter = window.NodeFilter; |
| 26 | + g.SVGElement = window.SVGElement; |
| 27 | + g.customElements = window.customElements; |
| 28 | + g.requestAnimationFrame = window.requestAnimationFrame.bind(window); |
| 29 | + g.cancelAnimationFrame = window.cancelAnimationFrame.bind(window); |
| 30 | + g.navigator = window.navigator; |
| 31 | + g.getComputedStyle = window.getComputedStyle.bind(window); |
| 32 | + const styleProto = Object.getPrototypeOf( |
| 33 | + window.document.createElement("div").style, |
| 34 | + ); |
| 35 | + if (styleProto && !styleProto[Symbol.iterator]) { |
| 36 | + Object.defineProperty(styleProto, Symbol.iterator, { |
| 37 | + configurable: true, |
| 38 | + value: function* iter() { |
| 39 | + for (const key of Object.keys(this)) { |
| 40 | + if (/^[a-zA-Z-]+$/.test(key)) { |
| 41 | + yield key; |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +describe("ui out-replace DOM shape", () => { |
| 50 | + test("adjacent out-replace siblings preserve the expected direct child shape", async () => { |
| 51 | + const window = new Window({ url: "http://localhost:8000/repro" }); |
| 52 | + setupGlobals(window); |
| 53 | + const { ui } = await import("../src/js/select/ui.js"); |
| 54 | + |
| 55 | + document.body.innerHTML = ` |
| 56 | + <div id="app"></div> |
| 57 | + <template id="AdjacentOutReplaceRepro"> |
| 58 | + <div class="horizontal"> |
| 59 | + <div out-replace=".|One"></div> |
| 60 | + <div out-replace=".|Two"></div> |
| 61 | + <div out-replace=".|Three"></div> |
| 62 | + </div> |
| 63 | + </template> |
| 64 | + <template name="One"><div class="one">One</div></template> |
| 65 | + <template name="Two"><div class="two">Two</div></template> |
| 66 | + <template name="Three"><div class="three">Three</div></template> |
| 67 | + `; |
| 68 | + |
| 69 | + const Repro = ui("AdjacentOutReplaceRepro"); |
| 70 | + ui("One"); |
| 71 | + ui("Two"); |
| 72 | + ui("Three"); |
| 73 | + |
| 74 | + const instance = Repro.new().mount("#app"); |
| 75 | + const horizontal = document.querySelector("#app .horizontal"); |
| 76 | + const classes = Array.from(horizontal.children).map((_) => _.className); |
| 77 | + |
| 78 | + expect(horizontal.children.length).toBe(3); |
| 79 | + expect(classes).toEqual(["One one", "Two two", "Three three"]); |
| 80 | + |
| 81 | + instance.unmount(); |
| 82 | + document.body.innerHTML = ""; |
| 83 | + window.close?.(); |
| 84 | + }); |
| 85 | + |
| 86 | + test("wrapped out siblings preserve the expected direct child shape", async () => { |
| 87 | + const window = new Window({ url: "http://localhost:8000/repro" }); |
| 88 | + setupGlobals(window); |
| 89 | + const { ui } = await import("../src/js/select/ui.js"); |
| 90 | + |
| 91 | + document.body.innerHTML = ` |
| 92 | + <div id="app"></div> |
| 93 | + <template id="WrappedOutRepro"> |
| 94 | + <div class="horizontal"> |
| 95 | + <div class="slot" out=".|One"></div> |
| 96 | + <div class="slot" out=".|Two"></div> |
| 97 | + <div class="slot" out=".|Three"></div> |
| 98 | + </div> |
| 99 | + </template> |
| 100 | + <template name="One"><div class="one">One</div></template> |
| 101 | + <template name="Two"><div class="two">Two</div></template> |
| 102 | + <template name="Three"><div class="three">Three</div></template> |
| 103 | + `; |
| 104 | + |
| 105 | + const Repro = ui("WrappedOutRepro"); |
| 106 | + ui("One"); |
| 107 | + ui("Two"); |
| 108 | + ui("Three"); |
| 109 | + |
| 110 | + const instance = Repro.new().mount("#app"); |
| 111 | + const horizontal = document.querySelector("#app .horizontal"); |
| 112 | + const classes = Array.from(horizontal.children).map((_) => _.className); |
| 113 | + const nested = Array.from(horizontal.children).map( |
| 114 | + (_) => _.firstElementChild?.className ?? null, |
| 115 | + ); |
| 116 | + |
| 117 | + expect(horizontal.children.length).toBe(3); |
| 118 | + expect(classes).toEqual(["slot", "slot", "slot"]); |
| 119 | + expect(nested).toEqual(["One one", "Two two", "Three three"]); |
| 120 | + |
| 121 | + instance.unmount(); |
| 122 | + document.body.innerHTML = ""; |
| 123 | + window.close?.(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments