|
| 1 | +import { createContext } from '@bomb.sh/tree'; |
| 2 | +import { describe, expect, test, vi } from 'vitest'; |
| 3 | +import { createHost, HostApi } from './host.ts'; |
| 4 | +import { getElement } from './elements.ts'; |
| 5 | + |
| 6 | +describe('host', () => { |
| 7 | + test('a prebuilt subtree inherits host context when attached', () => { |
| 8 | + const host = createHost(); |
| 9 | + const { getHost } = HostApi.methods; |
| 10 | + const marker = createContext<string>('@test/host/attachment'); |
| 11 | + marker.set(host.element.node!, 'from host'); |
| 12 | + |
| 13 | + const box = host.createElement('box'); |
| 14 | + const text = host.createElement('text'); |
| 15 | + const literal = host.createLiteral('hello'); |
| 16 | + |
| 17 | + host.insertBefore(text, literal); |
| 18 | + host.insertBefore(box, text); |
| 19 | + |
| 20 | + expect(text.node).toBeNull(); |
| 21 | + |
| 22 | + host.insertBefore(host.element, box); |
| 23 | + |
| 24 | + expect(marker.expect(text.node!)).toBe('from host'); |
| 25 | + expect(getHost(text.node!)).toBe(host); |
| 26 | + }); |
| 27 | + |
| 28 | + test('each attached node resolves its own element', () => { |
| 29 | + const host = createHost(); |
| 30 | + const parent = host.createElement('box'); |
| 31 | + const child = host.createElement('text'); |
| 32 | + |
| 33 | + host.insertBefore(parent, child); |
| 34 | + host.insertBefore(host.element, parent); |
| 35 | + |
| 36 | + expect(getElement(host.root)).toBe(host.element); |
| 37 | + expect(getElement(parent.node!)).toBe(parent); |
| 38 | + expect(getElement(child.node!)).toBe(child); |
| 39 | + }); |
| 40 | + |
| 41 | + test('detaching a child does not detach its parent', () => { |
| 42 | + const host = createHost(); |
| 43 | + const parent = host.createElement('box'); |
| 44 | + const child = host.createElement('text'); |
| 45 | + const parentDetach = vi.fn(); |
| 46 | + const childDetach = vi.fn(); |
| 47 | + parent.detach = parentDetach; |
| 48 | + child.detach = childDetach; |
| 49 | + |
| 50 | + host.insertBefore(parent, child); |
| 51 | + host.insertBefore(host.element, parent); |
| 52 | + const parentNode = parent.node; |
| 53 | + const childNode = child.node; |
| 54 | + |
| 55 | + host.removeChild(parent, child); |
| 56 | + |
| 57 | + expect(parent.node).toBe(parentNode); |
| 58 | + expect(parentDetach).not.toHaveBeenCalled(); |
| 59 | + expect(child.node).toBeNull(); |
| 60 | + expect(childDetach).toHaveBeenCalledOnce(); |
| 61 | + expect(childDetach).toHaveBeenCalledWith(childNode); |
| 62 | + }); |
| 63 | + |
| 64 | + test('moving an attached element to a detached parent detaches it', () => { |
| 65 | + const host = createHost(); |
| 66 | + const attached = host.createElement('box'); |
| 67 | + const detached = host.createElement('box'); |
| 68 | + const child = host.createElement('text'); |
| 69 | + |
| 70 | + host.insertBefore(host.element, attached); |
| 71 | + host.insertBefore(attached, child); |
| 72 | + |
| 73 | + expect(child.node).not.toBeNull(); |
| 74 | + expect(detached.node).toBeNull(); |
| 75 | + |
| 76 | + host.insertBefore(detached, child); |
| 77 | + |
| 78 | + expect(attached.children).toEqual([]); |
| 79 | + expect(detached.children).toEqual([child]); |
| 80 | + expect(child.node).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('moving an element within the same parent changes its position', () => { |
| 84 | + const host = createHost(); |
| 85 | + const parent = host.createElement('box'); |
| 86 | + const first = host.createElement('text'); |
| 87 | + const second = host.createElement('text'); |
| 88 | + const third = host.createElement('text'); |
| 89 | + |
| 90 | + host.insertBefore(host.element, parent); |
| 91 | + host.insertBefore(parent, third); |
| 92 | + host.insertBefore(parent, second, third); |
| 93 | + host.insertBefore(parent, first, second); |
| 94 | + |
| 95 | + const node = third.node; |
| 96 | + host.insertBefore(parent, third, first); |
| 97 | + |
| 98 | + expect(parent.children).toEqual([third, first, second]); |
| 99 | + expect(third.parent).toBe(parent); |
| 100 | + expect(third.node).toBe(node); |
| 101 | + }); |
| 102 | + |
| 103 | + test('moving an attached element to another attached parent reattaches it', () => { |
| 104 | + const host = createHost(); |
| 105 | + const marker = createContext<string>('@test/host/move'); |
| 106 | + const first = host.createElement('box'); |
| 107 | + const second = host.createElement('box'); |
| 108 | + const child = host.createElement('text'); |
| 109 | + |
| 110 | + host.insertBefore(host.element, first); |
| 111 | + host.insertBefore(host.element, second); |
| 112 | + marker.set(first.node!, 'first'); |
| 113 | + marker.set(second.node!, 'second'); |
| 114 | + host.insertBefore(first, child); |
| 115 | + |
| 116 | + const firstNode = child.node; |
| 117 | + expect(marker.expect(firstNode!)).toBe('first'); |
| 118 | + |
| 119 | + host.insertBefore(second, child); |
| 120 | + |
| 121 | + expect(first.children).toEqual([]); |
| 122 | + expect(second.children).toEqual([child]); |
| 123 | + expect(child.node).not.toBe(firstNode); |
| 124 | + expect(marker.expect(child.node!)).toBe('second'); |
| 125 | + }); |
| 126 | + |
| 127 | + test('setProperty adds and replaces a property', () => { |
| 128 | + const host = createHost(); |
| 129 | + const element = host.createElement('box'); |
| 130 | + |
| 131 | + host.setProperty(element, 'color', 'cyan'); |
| 132 | + host.setProperty(element, 'gap', 1); |
| 133 | + expect(element.properties).toEqual({ color: 'cyan', gap: 1 }); |
| 134 | + |
| 135 | + host.setProperty(element, 'color', 'green'); |
| 136 | + expect(element.properties).toEqual({ color: 'green', gap: 1 }); |
| 137 | + }); |
| 138 | + |
| 139 | + test('setProperty removes a property when its value is undefined', () => { |
| 140 | + const host = createHost(); |
| 141 | + const element = host.createElement('box'); |
| 142 | + |
| 143 | + host.setProperty(element, 'color', 'cyan'); |
| 144 | + host.setProperty(element, 'gap', 1); |
| 145 | + host.setProperty(element, 'color', undefined); |
| 146 | + |
| 147 | + expect(element.properties).toEqual({ gap: 1 }); |
| 148 | + expect(element.properties).not.toHaveProperty('color'); |
| 149 | + }); |
| 150 | + |
| 151 | + test('setProperty preserves null as an explicit value', () => { |
| 152 | + const host = createHost(); |
| 153 | + const element = host.createElement('box'); |
| 154 | + |
| 155 | + host.setProperty(element, 'value', null); |
| 156 | + |
| 157 | + expect(element.properties).toHaveProperty('value', null); |
| 158 | + }); |
| 159 | + |
| 160 | + test('setText updates a text literal without replacing it', () => { |
| 161 | + const host = createHost(); |
| 162 | + const parent = host.createElement('text'); |
| 163 | + const literal = host.createLiteral('hello'); |
| 164 | + |
| 165 | + host.insertBefore(parent, literal); |
| 166 | + const instance = parent.children[0]; |
| 167 | + |
| 168 | + host.setText(literal, 'world'); |
| 169 | + |
| 170 | + expect(literal.content).toBe('world'); |
| 171 | + expect(parent.children).toEqual([literal]); |
| 172 | + expect(parent.children[0]).toBe(instance); |
| 173 | + expect(literal.parent).toBe(parent); |
| 174 | + }); |
| 175 | + |
| 176 | + test('setText preserves an empty text literal', () => { |
| 177 | + const host = createHost(); |
| 178 | + const parent = host.createElement('text'); |
| 179 | + const literal = host.createLiteral('hello'); |
| 180 | + |
| 181 | + host.insertBefore(parent, literal); |
| 182 | + host.setText(literal, ''); |
| 183 | + |
| 184 | + expect(literal.content).toBe(''); |
| 185 | + expect(parent.children).toEqual([literal]); |
| 186 | + }); |
| 187 | + |
| 188 | + test('removeChild removes a text literal', () => { |
| 189 | + const host = createHost(); |
| 190 | + const parent = host.createElement('text'); |
| 191 | + const literal = host.createLiteral('hello'); |
| 192 | + |
| 193 | + host.insertBefore(parent, literal); |
| 194 | + host.removeChild(parent, literal); |
| 195 | + |
| 196 | + expect(parent.children).toEqual([]); |
| 197 | + expect(literal.parent).toBeNull(); |
| 198 | + }); |
| 199 | + |
| 200 | + test('removeChild detaches a subtree', () => { |
| 201 | + const host = createHost(); |
| 202 | + const parent = host.createElement('box'); |
| 203 | + const child = host.createElement('box'); |
| 204 | + const grandchild = host.createElement('text'); |
| 205 | + |
| 206 | + host.insertBefore(child, grandchild); |
| 207 | + host.insertBefore(parent, child); |
| 208 | + host.insertBefore(host.element, parent); |
| 209 | + |
| 210 | + expect(child.node).not.toBeNull(); |
| 211 | + expect(grandchild.node).not.toBeNull(); |
| 212 | + |
| 213 | + host.removeChild(parent, child); |
| 214 | + |
| 215 | + expect(parent.children).toEqual([]); |
| 216 | + expect(child.parent).toBeNull(); |
| 217 | + expect(child.children).toEqual([grandchild]); |
| 218 | + expect(grandchild.parent).toBe(child); |
| 219 | + expect(child.node).toBeNull(); |
| 220 | + expect(grandchild.node).toBeNull(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments