|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | +import { describe, expect, test } from "vitest"; |
| 6 | +import { createSignal, flush, Show, For, children } from "solid-js"; |
| 7 | +import { render, insert } from "@solidjs/web"; |
| 8 | + |
| 9 | +describe("slot ownership in a markerless hole", () => { |
| 10 | + test("resolved children moved from a sibling hole to a sole-child hole are still removed", () => { |
| 11 | + const [items, setItems] = createSignal(["a", "b", "c"]); |
| 12 | + const [wide, setWide] = createSignal(true); |
| 13 | + const Layout = (props: { children: any }) => { |
| 14 | + const c = children(() => props.children); |
| 15 | + return ( |
| 16 | + <Show when={wide()} fallback={<div class="stack">{c()}</div>}> |
| 17 | + <div class="row"> |
| 18 | + {c()} |
| 19 | + <span>end</span> |
| 20 | + </div> |
| 21 | + </Show> |
| 22 | + ); |
| 23 | + }; |
| 24 | + const el = document.createElement("div"); |
| 25 | + const dispose = render( |
| 26 | + () => ( |
| 27 | + <Layout> |
| 28 | + <For each={items()}>{x => <i>{x}</i>}</For> |
| 29 | + </Layout> |
| 30 | + ), |
| 31 | + el |
| 32 | + ); |
| 33 | + expect(el.innerHTML).toBe('<div class="row"><i>a</i><i>b</i><i>c</i><span>end</span></div>'); |
| 34 | + setWide(false); |
| 35 | + flush(); |
| 36 | + expect(el.innerHTML).toBe('<div class="stack"><i>a</i><i>b</i><i>c</i></div>'); |
| 37 | + setItems(["a", "c"]); |
| 38 | + flush(); |
| 39 | + expect(el.innerHTML).toBe('<div class="stack"><i>a</i><i>c</i></div>'); |
| 40 | + dispose(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("minimal: node from a marker hole reused in a sole-child hole", () => { |
| 44 | + const n1 = document.createElement("i"); |
| 45 | + n1.textContent = "1"; |
| 46 | + const n2 = document.createElement("i"); |
| 47 | + n2.textContent = "2"; |
| 48 | + const [a, setA] = createSignal<any>([n1, n2]); |
| 49 | + const [b, setB] = createSignal<any>([]); |
| 50 | + const el = document.createElement("div"); |
| 51 | + const dispose = render( |
| 52 | + () => ( |
| 53 | + <> |
| 54 | + <div> |
| 55 | + {a()} |
| 56 | + <hr /> |
| 57 | + </div> |
| 58 | + <p>{b()}</p> |
| 59 | + </> |
| 60 | + ), |
| 61 | + el |
| 62 | + ); |
| 63 | + setA([]); |
| 64 | + setB([n1, n2]); |
| 65 | + flush(); |
| 66 | + expect(el.innerHTML).toBe("<div><hr></div><p><i>1</i><i>2</i></p>"); |
| 67 | + setB([n2]); |
| 68 | + flush(); |
| 69 | + expect(el.innerHTML).toBe("<div><hr></div><p><i>2</i></p>"); |
| 70 | + dispose(); |
| 71 | + }); |
| 72 | + |
| 73 | + test("a slot that clears does not remove a node a markerless sibling slot claimed", () => { |
| 74 | + const node = document.createElement("div"); |
| 75 | + const parent = document.createElement("div"); |
| 76 | + const markerA = parent.appendChild(document.createTextNode("")); |
| 77 | + const [aVal, setAVal] = createSignal<any>(node); |
| 78 | + const [bVal, setBVal] = createSignal<any>(null); |
| 79 | + const dispose = render(() => { |
| 80 | + insert(parent, aVal, markerA); |
| 81 | + insert(parent, bVal, null); |
| 82 | + return parent; |
| 83 | + }, document.createElement("div")); |
| 84 | + setBVal(node); |
| 85 | + setAVal(null); |
| 86 | + flush(); |
| 87 | + expect(node.parentNode).toBe(parent); |
| 88 | + dispose(); |
| 89 | + }); |
| 90 | + |
| 91 | + test("a slot that reconciles does not remove nodes a markerless sibling slot claimed", () => { |
| 92 | + const [n1, n2, n3] = ["1", "2", "3"].map(t => |
| 93 | + Object.assign(document.createElement("i"), { textContent: t }) |
| 94 | + ); |
| 95 | + const parent = document.createElement("div"); |
| 96 | + const markerA = parent.appendChild(document.createTextNode("")); |
| 97 | + const [aVal, setAVal] = createSignal<any>([n1, n2]); |
| 98 | + const [bVal, setBVal] = createSignal<any>([]); |
| 99 | + const dispose = render(() => { |
| 100 | + insert(parent, aVal, markerA); |
| 101 | + insert(parent, bVal, null); |
| 102 | + return parent; |
| 103 | + }, document.createElement("div")); |
| 104 | + setBVal([n2]); |
| 105 | + setAVal([n3]); |
| 106 | + flush(); |
| 107 | + expect([...parent.children].map(n => n.textContent)).toEqual(["3", "2"]); |
| 108 | + expect(n1.parentNode).toBe(null); |
| 109 | + dispose(); |
| 110 | + }); |
| 111 | + |
| 112 | + test("a markerless swap retags a node a sibling slot claimed and dropped", () => { |
| 113 | + const [n1, n2, n3] = ["1", "2", "3"].map(t => |
| 114 | + Object.assign(document.createElement("i"), { textContent: t }) |
| 115 | + ); |
| 116 | + const parent = document.createElement("div"); |
| 117 | + const markerA = parent.appendChild(document.createTextNode("")); |
| 118 | + const [aVal, setAVal] = createSignal<any>([]); |
| 119 | + const [bVal, setBVal] = createSignal<any>([n1, n2, n3]); |
| 120 | + const dispose = render(() => { |
| 121 | + insert(parent, aVal, markerA); |
| 122 | + insert(parent, bVal, null); |
| 123 | + return parent; |
| 124 | + }, document.createElement("div")); |
| 125 | + setAVal([n3]); |
| 126 | + flush(); |
| 127 | + // The marker slot drops n3 while the markerless slot reverses, which moves |
| 128 | + // n3 back in through the swap loop. |
| 129 | + setAVal([]); |
| 130 | + setBVal([n3, n2, n1]); |
| 131 | + flush(); |
| 132 | + expect([...parent.children].map(n => n.textContent)).toEqual(["3", "2", "1"]); |
| 133 | + setBVal([n2, n1]); |
| 134 | + flush(); |
| 135 | + expect([...parent.children].map(n => n.textContent)).toEqual(["2", "1"]); |
| 136 | + expect(n3.parentNode).toBe(null); |
| 137 | + dispose(); |
| 138 | + }); |
| 139 | +}); |
0 commit comments