@pyreon/runtime-dom 0.11.5 → 0.11.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -22
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.js +4 -3
- package/lib/index.js.map +1 -1
- package/package.json +12 -12
- package/src/delegate.ts +25 -25
- package/src/devtools.ts +37 -37
- package/src/hydrate.ts +30 -30
- package/src/hydration-debug.ts +2 -2
- package/src/index.ts +20 -20
- package/src/keep-alive.ts +6 -6
- package/src/mount.ts +46 -46
- package/src/nodes.ts +31 -19
- package/src/props.ts +93 -93
- package/src/template.ts +6 -6
- package/src/tests/coverage-gaps.test.ts +669 -669
- package/src/tests/coverage.test.ts +299 -299
- package/src/tests/mount.test.ts +1183 -1183
- package/src/tests/props.test.ts +219 -219
- package/src/tests/setup.ts +1 -1
- package/src/tests/show-context.test.ts +43 -43
- package/src/tests/template.test.ts +71 -71
- package/src/tests/transition.test.ts +124 -124
- package/src/transition-group.ts +22 -22
- package/src/transition.ts +18 -18
package/src/tests/mount.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ComponentFn, VNodeChild } from
|
|
1
|
+
import type { ComponentFn, VNodeChild } from '@pyreon/core'
|
|
2
2
|
import {
|
|
3
3
|
ErrorBoundary as _ErrorBoundary,
|
|
4
4
|
createRef,
|
|
@@ -14,9 +14,9 @@ import {
|
|
|
14
14
|
Portal,
|
|
15
15
|
Show,
|
|
16
16
|
Switch,
|
|
17
|
-
} from
|
|
18
|
-
import { cell, signal } from
|
|
19
|
-
import { installDevTools, registerComponent, unregisterComponent } from
|
|
17
|
+
} from '@pyreon/core'
|
|
18
|
+
import { cell, signal } from '@pyreon/reactivity'
|
|
19
|
+
import { installDevTools, registerComponent, unregisterComponent } from '../devtools'
|
|
20
20
|
import {
|
|
21
21
|
KeepAlive as _KeepAlive,
|
|
22
22
|
Transition as _Transition,
|
|
@@ -28,8 +28,8 @@ import {
|
|
|
28
28
|
mount,
|
|
29
29
|
sanitizeHtml,
|
|
30
30
|
setSanitizer,
|
|
31
|
-
} from
|
|
32
|
-
import { mountChild } from
|
|
31
|
+
} from '../index'
|
|
32
|
+
import { mountChild } from '../mount'
|
|
33
33
|
|
|
34
34
|
// Cast components that return VNodeChild (not VNode | null) so h() accepts them
|
|
35
35
|
const Transition = _Transition as unknown as ComponentFn<Record<string, unknown>>
|
|
@@ -38,178 +38,178 @@ const ErrorBoundary = _ErrorBoundary as unknown as ComponentFn<Record<string, un
|
|
|
38
38
|
const KeepAlive = _KeepAlive as unknown as ComponentFn<Record<string, unknown>>
|
|
39
39
|
|
|
40
40
|
function container(): HTMLElement {
|
|
41
|
-
const el = document.createElement(
|
|
41
|
+
const el = document.createElement('div')
|
|
42
42
|
document.body.appendChild(el)
|
|
43
43
|
return el
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
// ─── Static mounting ─────────────────────────────────────────────────────────
|
|
47
47
|
|
|
48
|
-
describe(
|
|
49
|
-
test(
|
|
48
|
+
describe('mount — static', () => {
|
|
49
|
+
test('mounts a text node', () => {
|
|
50
50
|
const el = container()
|
|
51
|
-
mount(
|
|
52
|
-
expect(el.textContent).toBe(
|
|
51
|
+
mount('hello', el)
|
|
52
|
+
expect(el.textContent).toBe('hello')
|
|
53
53
|
})
|
|
54
54
|
|
|
55
|
-
test(
|
|
55
|
+
test('mounts a number as text', () => {
|
|
56
56
|
const el = container()
|
|
57
57
|
mount(42, el)
|
|
58
|
-
expect(el.textContent).toBe(
|
|
58
|
+
expect(el.textContent).toBe('42')
|
|
59
59
|
})
|
|
60
60
|
|
|
61
|
-
test(
|
|
61
|
+
test('mounts a simple element', () => {
|
|
62
62
|
const el = container()
|
|
63
|
-
mount(h(
|
|
64
|
-
expect(el.innerHTML).toBe(
|
|
63
|
+
mount(h('span', null, 'world'), el)
|
|
64
|
+
expect(el.innerHTML).toBe('<span>world</span>')
|
|
65
65
|
})
|
|
66
66
|
|
|
67
|
-
test(
|
|
67
|
+
test('mounts nested elements', () => {
|
|
68
68
|
const el = container()
|
|
69
|
-
mount(h(
|
|
70
|
-
expect(el.querySelector(
|
|
69
|
+
mount(h('div', null, h('p', null, 'nested')), el)
|
|
70
|
+
expect(el.querySelector('p')?.textContent).toBe('nested')
|
|
71
71
|
})
|
|
72
72
|
|
|
73
|
-
test(
|
|
73
|
+
test('mounts null / undefined / false as nothing', () => {
|
|
74
74
|
const el = container()
|
|
75
75
|
mount(null, el)
|
|
76
|
-
expect(el.innerHTML).toBe(
|
|
76
|
+
expect(el.innerHTML).toBe('')
|
|
77
77
|
mount(undefined, el)
|
|
78
|
-
expect(el.innerHTML).toBe(
|
|
78
|
+
expect(el.innerHTML).toBe('')
|
|
79
79
|
mount(false, el)
|
|
80
|
-
expect(el.innerHTML).toBe(
|
|
80
|
+
expect(el.innerHTML).toBe('')
|
|
81
81
|
})
|
|
82
82
|
|
|
83
|
-
test(
|
|
83
|
+
test('mounts a Fragment', () => {
|
|
84
84
|
const el = container()
|
|
85
|
-
mount(h(Fragment, null, h(
|
|
86
|
-
expect(el.querySelectorAll(
|
|
85
|
+
mount(h(Fragment, null, h('span', null, 'a'), h('span', null, 'b')), el)
|
|
86
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
87
87
|
})
|
|
88
88
|
})
|
|
89
89
|
|
|
90
90
|
// ─── Props ────────────────────────────────────────────────────────────────────
|
|
91
91
|
|
|
92
|
-
describe(
|
|
93
|
-
test(
|
|
92
|
+
describe('mount — props', () => {
|
|
93
|
+
test('sets class attribute', () => {
|
|
94
94
|
const el = container()
|
|
95
|
-
mount(h(
|
|
96
|
-
expect(el.querySelector(
|
|
95
|
+
mount(h('div', { class: 'foo bar' }), el)
|
|
96
|
+
expect(el.querySelector('div')?.className).toBe('foo bar')
|
|
97
97
|
})
|
|
98
98
|
|
|
99
|
-
test(
|
|
99
|
+
test('sets arbitrary attribute', () => {
|
|
100
100
|
const el = container()
|
|
101
|
-
mount(h(
|
|
102
|
-
expect(el.querySelector(
|
|
101
|
+
mount(h('div', { 'data-id': '123' }), el)
|
|
102
|
+
expect(el.querySelector('div')?.getAttribute('data-id')).toBe('123')
|
|
103
103
|
})
|
|
104
104
|
|
|
105
|
-
test(
|
|
105
|
+
test('removes attribute when value is null', () => {
|
|
106
106
|
const el = container()
|
|
107
|
-
mount(h(
|
|
108
|
-
expect(el.querySelector(
|
|
107
|
+
mount(h('div', { 'data-x': null }), el)
|
|
108
|
+
expect(el.querySelector('div')?.hasAttribute('data-x')).toBe(false)
|
|
109
109
|
})
|
|
110
110
|
|
|
111
|
-
test(
|
|
111
|
+
test('attaches event listener', () => {
|
|
112
112
|
const el = container()
|
|
113
113
|
let clicked = false
|
|
114
114
|
mount(
|
|
115
115
|
h(
|
|
116
|
-
|
|
116
|
+
'button',
|
|
117
117
|
{
|
|
118
118
|
onClick: () => {
|
|
119
119
|
clicked = true
|
|
120
120
|
},
|
|
121
121
|
},
|
|
122
|
-
|
|
122
|
+
'click me',
|
|
123
123
|
),
|
|
124
124
|
el,
|
|
125
125
|
)
|
|
126
|
-
el.querySelector(
|
|
126
|
+
el.querySelector('button')?.click()
|
|
127
127
|
expect(clicked).toBe(true)
|
|
128
128
|
})
|
|
129
129
|
})
|
|
130
130
|
|
|
131
131
|
// ─── Reactive props & children ────────────────────────────────────────────────
|
|
132
132
|
|
|
133
|
-
describe(
|
|
134
|
-
test(
|
|
133
|
+
describe('mount — reactive', () => {
|
|
134
|
+
test('reactive text child updates', () => {
|
|
135
135
|
const el = container()
|
|
136
|
-
const text = signal(
|
|
136
|
+
const text = signal('hello')
|
|
137
137
|
mount(
|
|
138
|
-
h(
|
|
138
|
+
h('div', null, () => text()),
|
|
139
139
|
el,
|
|
140
140
|
)
|
|
141
|
-
expect(el.querySelector(
|
|
142
|
-
text.set(
|
|
143
|
-
expect(el.querySelector(
|
|
141
|
+
expect(el.querySelector('div')?.textContent).toBe('hello')
|
|
142
|
+
text.set('world')
|
|
143
|
+
expect(el.querySelector('div')?.textContent).toBe('world')
|
|
144
144
|
})
|
|
145
145
|
|
|
146
|
-
test(
|
|
146
|
+
test('reactive class prop updates', () => {
|
|
147
147
|
const el = container()
|
|
148
|
-
const cls = signal(
|
|
149
|
-
mount(h(
|
|
150
|
-
expect(el.querySelector(
|
|
151
|
-
cls.set(
|
|
152
|
-
expect(el.querySelector(
|
|
148
|
+
const cls = signal('a')
|
|
149
|
+
mount(h('div', { class: () => cls() }), el)
|
|
150
|
+
expect(el.querySelector('div')?.className).toBe('a')
|
|
151
|
+
cls.set('b')
|
|
152
|
+
expect(el.querySelector('div')?.className).toBe('b')
|
|
153
153
|
})
|
|
154
154
|
})
|
|
155
155
|
|
|
156
156
|
// ─── Components ───────────────────────────────────────────────────────────────
|
|
157
157
|
|
|
158
|
-
describe(
|
|
159
|
-
test(
|
|
158
|
+
describe('mount — components', () => {
|
|
159
|
+
test('mounts a functional component', () => {
|
|
160
160
|
const Greeting = defineComponent(({ name }: { name: string }) =>
|
|
161
|
-
h(
|
|
161
|
+
h('p', null, `Hello, ${name}!`),
|
|
162
162
|
)
|
|
163
163
|
const el = container()
|
|
164
|
-
mount(h(Greeting, { name:
|
|
165
|
-
expect(el.querySelector(
|
|
164
|
+
mount(h(Greeting, { name: 'Pyreon' }), el)
|
|
165
|
+
expect(el.querySelector('p')?.textContent).toBe('Hello, Pyreon!')
|
|
166
166
|
})
|
|
167
167
|
|
|
168
|
-
test(
|
|
168
|
+
test('component with reactive state updates DOM', () => {
|
|
169
169
|
const Counter = defineComponent(() => {
|
|
170
170
|
const count = signal(0)
|
|
171
171
|
return h(
|
|
172
|
-
|
|
172
|
+
'div',
|
|
173
173
|
null,
|
|
174
|
-
h(
|
|
175
|
-
h(
|
|
174
|
+
h('span', null, () => String(count())),
|
|
175
|
+
h('button', { onClick: () => count.update((n) => n + 1) }, '+'),
|
|
176
176
|
)
|
|
177
177
|
})
|
|
178
178
|
const el = container()
|
|
179
179
|
mount(h(Counter, {}), el)
|
|
180
|
-
expect(el.querySelector(
|
|
181
|
-
el.querySelector(
|
|
182
|
-
expect(el.querySelector(
|
|
183
|
-
el.querySelector(
|
|
184
|
-
expect(el.querySelector(
|
|
180
|
+
expect(el.querySelector('span')?.textContent).toBe('0')
|
|
181
|
+
el.querySelector('button')?.click()
|
|
182
|
+
expect(el.querySelector('span')?.textContent).toBe('1')
|
|
183
|
+
el.querySelector('button')?.click()
|
|
184
|
+
expect(el.querySelector('span')?.textContent).toBe('2')
|
|
185
185
|
})
|
|
186
186
|
})
|
|
187
187
|
|
|
188
188
|
// ─── Unmount ──────────────────────────────────────────────────────────────────
|
|
189
189
|
|
|
190
|
-
describe(
|
|
191
|
-
test(
|
|
190
|
+
describe('mount — refs', () => {
|
|
191
|
+
test('ref.current is set after mount', () => {
|
|
192
192
|
const el = container()
|
|
193
193
|
const ref = createRef<HTMLButtonElement>()
|
|
194
194
|
expect(ref.current).toBeNull()
|
|
195
|
-
mount(h(
|
|
195
|
+
mount(h('button', { ref }), el)
|
|
196
196
|
expect(ref.current).toBeInstanceOf(HTMLButtonElement)
|
|
197
197
|
})
|
|
198
198
|
|
|
199
|
-
test(
|
|
199
|
+
test('ref.current is cleared after unmount', () => {
|
|
200
200
|
const el = container()
|
|
201
201
|
const ref = createRef<HTMLDivElement>()
|
|
202
|
-
const unmount = mount(h(
|
|
202
|
+
const unmount = mount(h('div', { ref }), el)
|
|
203
203
|
expect(ref.current).not.toBeNull()
|
|
204
204
|
unmount()
|
|
205
205
|
expect(ref.current).toBeNull()
|
|
206
206
|
})
|
|
207
207
|
|
|
208
|
-
test(
|
|
208
|
+
test('callback ref is called with element after mount', () => {
|
|
209
209
|
const el = container()
|
|
210
210
|
let refEl: Element | null = null
|
|
211
211
|
mount(
|
|
212
|
-
h(
|
|
212
|
+
h('div', {
|
|
213
213
|
ref: (e: Element) => {
|
|
214
214
|
refEl = e
|
|
215
215
|
},
|
|
@@ -219,11 +219,11 @@ describe("mount — refs", () => {
|
|
|
219
219
|
expect(refEl).toBeInstanceOf(HTMLDivElement)
|
|
220
220
|
})
|
|
221
221
|
|
|
222
|
-
test(
|
|
222
|
+
test('callback ref element is not nulled on unmount', () => {
|
|
223
223
|
const el = container()
|
|
224
224
|
let refEl: Element | null = null
|
|
225
225
|
const unmount = mount(
|
|
226
|
-
h(
|
|
226
|
+
h('div', {
|
|
227
227
|
ref: (e: Element) => {
|
|
228
228
|
refEl = e
|
|
229
229
|
},
|
|
@@ -236,185 +236,185 @@ describe("mount — refs", () => {
|
|
|
236
236
|
expect(refEl).toBeInstanceOf(HTMLDivElement)
|
|
237
237
|
})
|
|
238
238
|
|
|
239
|
-
test(
|
|
239
|
+
test('ref is not emitted as an HTML attribute', () => {
|
|
240
240
|
const el = container()
|
|
241
241
|
const ref = createRef<HTMLDivElement>()
|
|
242
|
-
mount(h(
|
|
243
|
-
expect(el.firstElementChild?.hasAttribute(
|
|
242
|
+
mount(h('div', { ref }), el)
|
|
243
|
+
expect(el.firstElementChild?.hasAttribute('ref')).toBe(false)
|
|
244
244
|
})
|
|
245
245
|
})
|
|
246
246
|
|
|
247
|
-
describe(
|
|
248
|
-
test(
|
|
247
|
+
describe('mount — unmount', () => {
|
|
248
|
+
test('unmount removes mounted nodes', () => {
|
|
249
249
|
const el = container()
|
|
250
|
-
const unmount = mount(h(
|
|
251
|
-
expect(el.innerHTML).not.toBe(
|
|
250
|
+
const unmount = mount(h('div', null, 'bye'), el)
|
|
251
|
+
expect(el.innerHTML).not.toBe('')
|
|
252
252
|
unmount()
|
|
253
|
-
expect(el.innerHTML).toBe(
|
|
253
|
+
expect(el.innerHTML).toBe('')
|
|
254
254
|
})
|
|
255
255
|
|
|
256
|
-
test(
|
|
256
|
+
test('unmount disposes reactive effects', () => {
|
|
257
257
|
const el = container()
|
|
258
|
-
const text = signal(
|
|
258
|
+
const text = signal('initial')
|
|
259
259
|
const unmount = mount(
|
|
260
|
-
h(
|
|
260
|
+
h('p', null, () => text()),
|
|
261
261
|
el,
|
|
262
262
|
)
|
|
263
263
|
unmount()
|
|
264
|
-
text.set(
|
|
264
|
+
text.set('updated')
|
|
265
265
|
// After unmount, node is gone — no error thrown, no stale update
|
|
266
|
-
expect(el.innerHTML).toBe(
|
|
266
|
+
expect(el.innerHTML).toBe('')
|
|
267
267
|
})
|
|
268
268
|
})
|
|
269
269
|
|
|
270
270
|
// ─── For ──────────────────────────────────────────────────────────────────────
|
|
271
271
|
|
|
272
|
-
describe(
|
|
272
|
+
describe('mount — For', () => {
|
|
273
273
|
type Item = { id: number; label: string }
|
|
274
274
|
|
|
275
|
-
test(
|
|
275
|
+
test('renders initial list', () => {
|
|
276
276
|
const el = container()
|
|
277
277
|
const items = signal<Item[]>([
|
|
278
|
-
{ id: 1, label:
|
|
279
|
-
{ id: 2, label:
|
|
280
|
-
{ id: 3, label:
|
|
278
|
+
{ id: 1, label: 'a' },
|
|
279
|
+
{ id: 2, label: 'b' },
|
|
280
|
+
{ id: 3, label: 'c' },
|
|
281
281
|
])
|
|
282
282
|
mount(
|
|
283
283
|
h(
|
|
284
|
-
|
|
284
|
+
'ul',
|
|
285
285
|
null,
|
|
286
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
286
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
287
287
|
),
|
|
288
288
|
el,
|
|
289
289
|
)
|
|
290
|
-
expect(el.querySelectorAll(
|
|
291
|
-
expect(el.querySelectorAll(
|
|
292
|
-
expect(el.querySelectorAll(
|
|
290
|
+
expect(el.querySelectorAll('li').length).toBe(3)
|
|
291
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('a')
|
|
292
|
+
expect(el.querySelectorAll('li')[2]?.textContent).toBe('c')
|
|
293
293
|
})
|
|
294
294
|
|
|
295
|
-
test(
|
|
295
|
+
test('appends new items', () => {
|
|
296
296
|
const el = container()
|
|
297
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
297
|
+
const items = signal<Item[]>([{ id: 1, label: 'a' }])
|
|
298
298
|
mount(
|
|
299
299
|
h(
|
|
300
|
-
|
|
300
|
+
'ul',
|
|
301
301
|
null,
|
|
302
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
302
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
303
303
|
),
|
|
304
304
|
el,
|
|
305
305
|
)
|
|
306
|
-
expect(el.querySelectorAll(
|
|
306
|
+
expect(el.querySelectorAll('li').length).toBe(1)
|
|
307
307
|
items.set([
|
|
308
|
-
{ id: 1, label:
|
|
309
|
-
{ id: 2, label:
|
|
308
|
+
{ id: 1, label: 'a' },
|
|
309
|
+
{ id: 2, label: 'b' },
|
|
310
310
|
])
|
|
311
|
-
expect(el.querySelectorAll(
|
|
312
|
-
expect(el.querySelectorAll(
|
|
311
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
312
|
+
expect(el.querySelectorAll('li')[1]?.textContent).toBe('b')
|
|
313
313
|
})
|
|
314
314
|
|
|
315
|
-
test(
|
|
315
|
+
test('removes items', () => {
|
|
316
316
|
const el = container()
|
|
317
317
|
const items = signal<Item[]>([
|
|
318
|
-
{ id: 1, label:
|
|
319
|
-
{ id: 2, label:
|
|
318
|
+
{ id: 1, label: 'a' },
|
|
319
|
+
{ id: 2, label: 'b' },
|
|
320
320
|
])
|
|
321
321
|
mount(
|
|
322
322
|
h(
|
|
323
|
-
|
|
323
|
+
'ul',
|
|
324
324
|
null,
|
|
325
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
325
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
326
326
|
),
|
|
327
327
|
el,
|
|
328
328
|
)
|
|
329
|
-
items.set([{ id: 1, label:
|
|
330
|
-
expect(el.querySelectorAll(
|
|
331
|
-
expect(el.querySelectorAll(
|
|
329
|
+
items.set([{ id: 1, label: 'a' }])
|
|
330
|
+
expect(el.querySelectorAll('li').length).toBe(1)
|
|
331
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('a')
|
|
332
332
|
})
|
|
333
333
|
|
|
334
|
-
test(
|
|
334
|
+
test('swaps two items (small-k fast path)', () => {
|
|
335
335
|
const el = container()
|
|
336
336
|
const items = signal<Item[]>([
|
|
337
|
-
{ id: 1, label:
|
|
338
|
-
{ id: 2, label:
|
|
339
|
-
{ id: 3, label:
|
|
337
|
+
{ id: 1, label: 'a' },
|
|
338
|
+
{ id: 2, label: 'b' },
|
|
339
|
+
{ id: 3, label: 'c' },
|
|
340
340
|
])
|
|
341
341
|
mount(
|
|
342
342
|
h(
|
|
343
|
-
|
|
343
|
+
'ul',
|
|
344
344
|
null,
|
|
345
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
345
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
346
346
|
),
|
|
347
347
|
el,
|
|
348
348
|
)
|
|
349
349
|
items.set([
|
|
350
|
-
{ id: 1, label:
|
|
351
|
-
{ id: 3, label:
|
|
352
|
-
{ id: 2, label:
|
|
350
|
+
{ id: 1, label: 'a' },
|
|
351
|
+
{ id: 3, label: 'c' },
|
|
352
|
+
{ id: 2, label: 'b' },
|
|
353
353
|
])
|
|
354
|
-
const lis = el.querySelectorAll(
|
|
355
|
-
expect(lis[0]?.textContent).toBe(
|
|
356
|
-
expect(lis[1]?.textContent).toBe(
|
|
357
|
-
expect(lis[2]?.textContent).toBe(
|
|
354
|
+
const lis = el.querySelectorAll('li')
|
|
355
|
+
expect(lis[0]?.textContent).toBe('a')
|
|
356
|
+
expect(lis[1]?.textContent).toBe('c')
|
|
357
|
+
expect(lis[2]?.textContent).toBe('b')
|
|
358
358
|
})
|
|
359
359
|
|
|
360
|
-
test(
|
|
360
|
+
test('replaces all items', () => {
|
|
361
361
|
const el = container()
|
|
362
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
362
|
+
const items = signal<Item[]>([{ id: 1, label: 'old' }])
|
|
363
363
|
mount(
|
|
364
364
|
h(
|
|
365
|
-
|
|
365
|
+
'ul',
|
|
366
366
|
null,
|
|
367
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
367
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
368
368
|
),
|
|
369
369
|
el,
|
|
370
370
|
)
|
|
371
|
-
items.set([{ id: 99, label:
|
|
372
|
-
const lis = el.querySelectorAll(
|
|
371
|
+
items.set([{ id: 99, label: 'new' }])
|
|
372
|
+
const lis = el.querySelectorAll('li')
|
|
373
373
|
expect(lis.length).toBe(1)
|
|
374
|
-
expect(lis[0]?.textContent).toBe(
|
|
374
|
+
expect(lis[0]?.textContent).toBe('new')
|
|
375
375
|
})
|
|
376
376
|
|
|
377
|
-
test(
|
|
377
|
+
test('clears list', () => {
|
|
378
378
|
const el = container()
|
|
379
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
379
|
+
const items = signal<Item[]>([{ id: 1, label: 'x' }])
|
|
380
380
|
mount(
|
|
381
381
|
h(
|
|
382
|
-
|
|
382
|
+
'ul',
|
|
383
383
|
null,
|
|
384
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
384
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
385
385
|
),
|
|
386
386
|
el,
|
|
387
387
|
)
|
|
388
388
|
items.set([])
|
|
389
|
-
expect(el.querySelectorAll(
|
|
389
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
390
390
|
})
|
|
391
391
|
|
|
392
|
-
test(
|
|
392
|
+
test('unmount cleans up', () => {
|
|
393
393
|
const el = container()
|
|
394
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
394
|
+
const items = signal<Item[]>([{ id: 1, label: 'x' }])
|
|
395
395
|
const unmount = mount(
|
|
396
396
|
h(
|
|
397
|
-
|
|
397
|
+
'ul',
|
|
398
398
|
null,
|
|
399
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
399
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
400
400
|
),
|
|
401
401
|
el,
|
|
402
402
|
)
|
|
403
403
|
unmount()
|
|
404
|
-
expect(el.innerHTML).toBe(
|
|
404
|
+
expect(el.innerHTML).toBe('')
|
|
405
405
|
})
|
|
406
406
|
})
|
|
407
407
|
|
|
408
408
|
// ─── For + NativeItem (createTemplate path — what the benchmark uses) ─────
|
|
409
409
|
|
|
410
|
-
describe(
|
|
410
|
+
describe('mount — For + NativeItem (createTemplate)', () => {
|
|
411
411
|
type RR = { id: number; label: ReturnType<typeof cell<string>> }
|
|
412
412
|
|
|
413
413
|
function makeRR(id: number, text: string): RR {
|
|
414
414
|
return { id, label: cell(text) }
|
|
415
415
|
}
|
|
416
416
|
|
|
417
|
-
const rowFactory = createTemplate<RR>(
|
|
417
|
+
const rowFactory = createTemplate<RR>('<tr><td>\x00</td><td>\x00</td></tr>', (tr, row) => {
|
|
418
418
|
const td1 = tr.firstChild as HTMLElement
|
|
419
419
|
const td2 = td1.nextSibling as HTMLElement
|
|
420
420
|
const t1 = td1.firstChild as Text
|
|
@@ -427,302 +427,302 @@ describe("mount — For + NativeItem (createTemplate)", () => {
|
|
|
427
427
|
return null
|
|
428
428
|
})
|
|
429
429
|
|
|
430
|
-
test(
|
|
430
|
+
test('renders initial list with correct text', () => {
|
|
431
431
|
const el = container()
|
|
432
|
-
const items = signal<RR[]>([makeRR(1,
|
|
432
|
+
const items = signal<RR[]>([makeRR(1, 'a'), makeRR(2, 'b'), makeRR(3, 'c')])
|
|
433
433
|
mount(
|
|
434
434
|
h(
|
|
435
|
-
|
|
435
|
+
'table',
|
|
436
436
|
null,
|
|
437
|
-
h(
|
|
437
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
438
438
|
),
|
|
439
439
|
el,
|
|
440
440
|
)
|
|
441
|
-
const trs = el.querySelectorAll(
|
|
441
|
+
const trs = el.querySelectorAll('tr')
|
|
442
442
|
expect(trs.length).toBe(3)
|
|
443
|
-
expect(trs[0]?.querySelectorAll(
|
|
444
|
-
expect(trs[2]?.querySelectorAll(
|
|
443
|
+
expect(trs[0]?.querySelectorAll('td')[1]?.textContent).toBe('a')
|
|
444
|
+
expect(trs[2]?.querySelectorAll('td')[1]?.textContent).toBe('c')
|
|
445
445
|
})
|
|
446
446
|
|
|
447
|
-
test(
|
|
447
|
+
test('cell.set() updates text in-place (partial update)', () => {
|
|
448
448
|
const el = container()
|
|
449
|
-
const rows = [makeRR(1,
|
|
449
|
+
const rows = [makeRR(1, 'hello'), makeRR(2, 'world')]
|
|
450
450
|
const items = signal<RR[]>(rows)
|
|
451
451
|
mount(
|
|
452
452
|
h(
|
|
453
|
-
|
|
453
|
+
'table',
|
|
454
454
|
null,
|
|
455
|
-
h(
|
|
455
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
456
456
|
),
|
|
457
457
|
el,
|
|
458
458
|
)
|
|
459
459
|
// Update label via cell — should change DOM without re-rendering list
|
|
460
460
|
const first = rows[0]
|
|
461
|
-
if (!first) throw new Error(
|
|
462
|
-
first.label.set(
|
|
463
|
-
expect(el.querySelectorAll(
|
|
461
|
+
if (!first) throw new Error('missing row')
|
|
462
|
+
first.label.set('changed')
|
|
463
|
+
expect(el.querySelectorAll('tr')[0]?.querySelectorAll('td')[1]?.textContent).toBe('changed')
|
|
464
464
|
// Second row untouched
|
|
465
|
-
expect(el.querySelectorAll(
|
|
465
|
+
expect(el.querySelectorAll('tr')[1]?.querySelectorAll('td')[1]?.textContent).toBe('world')
|
|
466
466
|
})
|
|
467
467
|
|
|
468
|
-
test(
|
|
468
|
+
test('replace all rows', () => {
|
|
469
469
|
const el = container()
|
|
470
|
-
const items = signal<RR[]>([makeRR(1,
|
|
470
|
+
const items = signal<RR[]>([makeRR(1, 'old')])
|
|
471
471
|
mount(
|
|
472
472
|
h(
|
|
473
|
-
|
|
473
|
+
'table',
|
|
474
474
|
null,
|
|
475
|
-
h(
|
|
475
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
476
476
|
),
|
|
477
477
|
el,
|
|
478
478
|
)
|
|
479
|
-
items.set([makeRR(10,
|
|
480
|
-
const trs = el.querySelectorAll(
|
|
479
|
+
items.set([makeRR(10, 'new1'), makeRR(11, 'new2')])
|
|
480
|
+
const trs = el.querySelectorAll('tr')
|
|
481
481
|
expect(trs.length).toBe(2)
|
|
482
|
-
expect(trs[0]?.querySelectorAll(
|
|
483
|
-
expect(trs[1]?.querySelectorAll(
|
|
482
|
+
expect(trs[0]?.querySelectorAll('td')[0]?.textContent).toBe('10')
|
|
483
|
+
expect(trs[1]?.querySelectorAll('td')[1]?.textContent).toBe('new2')
|
|
484
484
|
})
|
|
485
485
|
|
|
486
|
-
test(
|
|
486
|
+
test('swap rows preserves DOM identity', () => {
|
|
487
487
|
const el = container()
|
|
488
|
-
const r1 = makeRR(1,
|
|
489
|
-
const r2 = makeRR(2,
|
|
490
|
-
const r3 = makeRR(3,
|
|
488
|
+
const r1 = makeRR(1, 'a')
|
|
489
|
+
const r2 = makeRR(2, 'b')
|
|
490
|
+
const r3 = makeRR(3, 'c')
|
|
491
491
|
const items = signal<RR[]>([r1, r2, r3])
|
|
492
492
|
mount(
|
|
493
493
|
h(
|
|
494
|
-
|
|
494
|
+
'table',
|
|
495
495
|
null,
|
|
496
|
-
h(
|
|
496
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
497
497
|
),
|
|
498
498
|
el,
|
|
499
499
|
)
|
|
500
|
-
const origTr2 = el.querySelectorAll(
|
|
501
|
-
const origTr3 = el.querySelectorAll(
|
|
500
|
+
const origTr2 = el.querySelectorAll('tr')[1]
|
|
501
|
+
const origTr3 = el.querySelectorAll('tr')[2]
|
|
502
502
|
// Swap positions 1 and 2
|
|
503
503
|
items.set([r1, r3, r2])
|
|
504
|
-
const trs = el.querySelectorAll(
|
|
505
|
-
expect(trs[1]?.querySelectorAll(
|
|
506
|
-
expect(trs[2]?.querySelectorAll(
|
|
504
|
+
const trs = el.querySelectorAll('tr')
|
|
505
|
+
expect(trs[1]?.querySelectorAll('td')[1]?.textContent).toBe('c')
|
|
506
|
+
expect(trs[2]?.querySelectorAll('td')[1]?.textContent).toBe('b')
|
|
507
507
|
// Same DOM nodes reused, just moved
|
|
508
508
|
expect(trs[1]).toBe(origTr3)
|
|
509
509
|
expect(trs[2]).toBe(origTr2)
|
|
510
510
|
})
|
|
511
511
|
|
|
512
|
-
test(
|
|
512
|
+
test('clear removes all rows', () => {
|
|
513
513
|
const el = container()
|
|
514
|
-
const items = signal<RR[]>([makeRR(1,
|
|
514
|
+
const items = signal<RR[]>([makeRR(1, 'x'), makeRR(2, 'y')])
|
|
515
515
|
mount(
|
|
516
516
|
h(
|
|
517
|
-
|
|
517
|
+
'table',
|
|
518
518
|
null,
|
|
519
|
-
h(
|
|
519
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
520
520
|
),
|
|
521
521
|
el,
|
|
522
522
|
)
|
|
523
523
|
items.set([])
|
|
524
|
-
expect(el.querySelectorAll(
|
|
524
|
+
expect(el.querySelectorAll('tr').length).toBe(0)
|
|
525
525
|
})
|
|
526
526
|
|
|
527
|
-
test(
|
|
527
|
+
test('clear then re-create works', () => {
|
|
528
528
|
const el = container()
|
|
529
|
-
const items = signal<RR[]>([makeRR(1,
|
|
529
|
+
const items = signal<RR[]>([makeRR(1, 'first')])
|
|
530
530
|
mount(
|
|
531
531
|
h(
|
|
532
|
-
|
|
532
|
+
'table',
|
|
533
533
|
null,
|
|
534
|
-
h(
|
|
534
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
535
535
|
),
|
|
536
536
|
el,
|
|
537
537
|
)
|
|
538
538
|
items.set([])
|
|
539
|
-
expect(el.querySelectorAll(
|
|
540
|
-
items.set([makeRR(5,
|
|
541
|
-
expect(el.querySelectorAll(
|
|
542
|
-
expect(el.querySelectorAll(
|
|
539
|
+
expect(el.querySelectorAll('tr').length).toBe(0)
|
|
540
|
+
items.set([makeRR(5, 'back')])
|
|
541
|
+
expect(el.querySelectorAll('tr').length).toBe(1)
|
|
542
|
+
expect(el.querySelectorAll('td')[1]?.textContent).toBe('back')
|
|
543
543
|
})
|
|
544
544
|
|
|
545
|
-
test(
|
|
545
|
+
test('append items to existing list', () => {
|
|
546
546
|
const el = container()
|
|
547
|
-
const r1 = makeRR(1,
|
|
547
|
+
const r1 = makeRR(1, 'a')
|
|
548
548
|
const items = signal<RR[]>([r1])
|
|
549
549
|
mount(
|
|
550
550
|
h(
|
|
551
|
-
|
|
551
|
+
'table',
|
|
552
552
|
null,
|
|
553
|
-
h(
|
|
553
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
554
554
|
),
|
|
555
555
|
el,
|
|
556
556
|
)
|
|
557
|
-
items.set([r1, makeRR(2,
|
|
558
|
-
expect(el.querySelectorAll(
|
|
559
|
-
expect(el.querySelectorAll(
|
|
557
|
+
items.set([r1, makeRR(2, 'b'), makeRR(3, 'c')])
|
|
558
|
+
expect(el.querySelectorAll('tr').length).toBe(3)
|
|
559
|
+
expect(el.querySelectorAll('tr')[2]?.querySelectorAll('td')[1]?.textContent).toBe('c')
|
|
560
560
|
})
|
|
561
561
|
|
|
562
|
-
test(
|
|
562
|
+
test('remove items from middle', () => {
|
|
563
563
|
const el = container()
|
|
564
|
-
const r1 = makeRR(1,
|
|
565
|
-
const r2 = makeRR(2,
|
|
566
|
-
const r3 = makeRR(3,
|
|
564
|
+
const r1 = makeRR(1, 'a')
|
|
565
|
+
const r2 = makeRR(2, 'b')
|
|
566
|
+
const r3 = makeRR(3, 'c')
|
|
567
567
|
const items = signal<RR[]>([r1, r2, r3])
|
|
568
568
|
mount(
|
|
569
569
|
h(
|
|
570
|
-
|
|
570
|
+
'table',
|
|
571
571
|
null,
|
|
572
|
-
h(
|
|
572
|
+
h('tbody', null, For({ each: items, by: (r) => r.id, children: rowFactory })),
|
|
573
573
|
),
|
|
574
574
|
el,
|
|
575
575
|
)
|
|
576
576
|
items.set([r1, r3])
|
|
577
|
-
const trs = el.querySelectorAll(
|
|
577
|
+
const trs = el.querySelectorAll('tr')
|
|
578
578
|
expect(trs.length).toBe(2)
|
|
579
|
-
expect(trs[0]?.querySelectorAll(
|
|
580
|
-
expect(trs[1]?.querySelectorAll(
|
|
579
|
+
expect(trs[0]?.querySelectorAll('td')[1]?.textContent).toBe('a')
|
|
580
|
+
expect(trs[1]?.querySelectorAll('td')[1]?.textContent).toBe('c')
|
|
581
581
|
})
|
|
582
582
|
})
|
|
583
583
|
|
|
584
584
|
// ─── Portal ───────────────────────────────────────────────────────────────────
|
|
585
585
|
|
|
586
|
-
describe(
|
|
587
|
-
test(
|
|
586
|
+
describe('mount — Portal', () => {
|
|
587
|
+
test('renders into target instead of parent', () => {
|
|
588
588
|
const src = container()
|
|
589
589
|
const target = container()
|
|
590
|
-
mount(Portal({ target, children: h(
|
|
590
|
+
mount(Portal({ target, children: h('span', null, 'portaled') }), src)
|
|
591
591
|
// content appears in target, not in src
|
|
592
|
-
expect(target.querySelector(
|
|
593
|
-
expect(src.querySelector(
|
|
592
|
+
expect(target.querySelector('span')?.textContent).toBe('portaled')
|
|
593
|
+
expect(src.querySelector('span')).toBeNull()
|
|
594
594
|
})
|
|
595
595
|
|
|
596
|
-
test(
|
|
596
|
+
test('unmount removes content from target', () => {
|
|
597
597
|
const src = container()
|
|
598
598
|
const target = container()
|
|
599
|
-
const unmount = mount(Portal({ target, children: h(
|
|
600
|
-
expect(target.querySelector(
|
|
599
|
+
const unmount = mount(Portal({ target, children: h('p', null, 'bye') }), src)
|
|
600
|
+
expect(target.querySelector('p')).not.toBeNull()
|
|
601
601
|
unmount()
|
|
602
|
-
expect(target.querySelector(
|
|
602
|
+
expect(target.querySelector('p')).toBeNull()
|
|
603
603
|
})
|
|
604
604
|
|
|
605
|
-
test(
|
|
605
|
+
test('portal content updates reactively', () => {
|
|
606
606
|
const src = container()
|
|
607
607
|
const target = container()
|
|
608
|
-
const text = signal(
|
|
609
|
-
mount(Portal({ target, children: h(
|
|
610
|
-
expect(target.querySelector(
|
|
611
|
-
text.set(
|
|
612
|
-
expect(target.querySelector(
|
|
608
|
+
const text = signal('hello')
|
|
609
|
+
mount(Portal({ target, children: h('span', null, () => text()) }), src)
|
|
610
|
+
expect(target.querySelector('span')?.textContent).toBe('hello')
|
|
611
|
+
text.set('world')
|
|
612
|
+
expect(target.querySelector('span')?.textContent).toBe('world')
|
|
613
613
|
})
|
|
614
614
|
|
|
615
|
-
test(
|
|
615
|
+
test('portal inside component renders into target', () => {
|
|
616
616
|
const src = container()
|
|
617
617
|
const target = container()
|
|
618
|
-
const Modal = () => Portal({ target, children: h(
|
|
618
|
+
const Modal = () => Portal({ target, children: h('dialog', null, 'modal content') })
|
|
619
619
|
mount(h(Modal, null), src)
|
|
620
|
-
expect(target.querySelector(
|
|
621
|
-
expect(src.querySelector(
|
|
620
|
+
expect(target.querySelector('dialog')?.textContent).toBe('modal content')
|
|
621
|
+
expect(src.querySelector('dialog')).toBeNull()
|
|
622
622
|
})
|
|
623
623
|
|
|
624
|
-
test(
|
|
624
|
+
test('portal re-mount after unmount works correctly', () => {
|
|
625
625
|
const src = container()
|
|
626
626
|
const target = container()
|
|
627
|
-
const unmount1 = mount(Portal({ target, children: h(
|
|
628
|
-
expect(target.querySelector(
|
|
627
|
+
const unmount1 = mount(Portal({ target, children: h('span', null, 'first') }), src)
|
|
628
|
+
expect(target.querySelector('span')?.textContent).toBe('first')
|
|
629
629
|
unmount1()
|
|
630
|
-
expect(target.querySelector(
|
|
630
|
+
expect(target.querySelector('span')).toBeNull()
|
|
631
631
|
// Re-mount into same target
|
|
632
|
-
const unmount2 = mount(Portal({ target, children: h(
|
|
633
|
-
expect(target.querySelector(
|
|
632
|
+
const unmount2 = mount(Portal({ target, children: h('span', null, 'second') }), src)
|
|
633
|
+
expect(target.querySelector('span')?.textContent).toBe('second')
|
|
634
634
|
unmount2()
|
|
635
|
-
expect(target.querySelector(
|
|
635
|
+
expect(target.querySelector('span')).toBeNull()
|
|
636
636
|
})
|
|
637
637
|
|
|
638
|
-
test(
|
|
638
|
+
test('multiple portals into same target', () => {
|
|
639
639
|
const src = container()
|
|
640
640
|
const target = container()
|
|
641
|
-
const unmount1 = mount(Portal({ target, children: h(
|
|
642
|
-
const unmount2 = mount(Portal({ target, children: h(
|
|
643
|
-
expect(target.querySelectorAll(
|
|
644
|
-
expect(target.querySelector(
|
|
645
|
-
expect(target.querySelector(
|
|
641
|
+
const unmount1 = mount(Portal({ target, children: h('span', { class: 'a' }, 'A') }), src)
|
|
642
|
+
const unmount2 = mount(Portal({ target, children: h('span', { class: 'b' }, 'B') }), src)
|
|
643
|
+
expect(target.querySelectorAll('span').length).toBe(2)
|
|
644
|
+
expect(target.querySelector('.a')?.textContent).toBe('A')
|
|
645
|
+
expect(target.querySelector('.b')?.textContent).toBe('B')
|
|
646
646
|
unmount1()
|
|
647
|
-
expect(target.querySelectorAll(
|
|
648
|
-
expect(target.querySelector(
|
|
647
|
+
expect(target.querySelectorAll('span').length).toBe(1)
|
|
648
|
+
expect(target.querySelector('.b')?.textContent).toBe('B')
|
|
649
649
|
unmount2()
|
|
650
|
-
expect(target.querySelectorAll(
|
|
650
|
+
expect(target.querySelectorAll('span').length).toBe(0)
|
|
651
651
|
})
|
|
652
652
|
|
|
653
|
-
test(
|
|
653
|
+
test('portal with reactive Show toggle', () => {
|
|
654
654
|
const src = container()
|
|
655
655
|
const target = container()
|
|
656
656
|
const visible = signal(true)
|
|
657
657
|
mount(
|
|
658
|
-
h(
|
|
659
|
-
visible() ? Portal({ target, children: h(
|
|
658
|
+
h('div', null, () =>
|
|
659
|
+
visible() ? Portal({ target, children: h('span', null, 'vis') }) : null,
|
|
660
660
|
),
|
|
661
661
|
src,
|
|
662
662
|
)
|
|
663
|
-
expect(target.querySelector(
|
|
663
|
+
expect(target.querySelector('span')?.textContent).toBe('vis')
|
|
664
664
|
visible.set(false)
|
|
665
|
-
expect(target.querySelector(
|
|
665
|
+
expect(target.querySelector('span')).toBeNull()
|
|
666
666
|
visible.set(true)
|
|
667
|
-
expect(target.querySelector(
|
|
667
|
+
expect(target.querySelector('span')?.textContent).toBe('vis')
|
|
668
668
|
})
|
|
669
669
|
})
|
|
670
670
|
|
|
671
671
|
// ─── ErrorBoundary ────────────────────────────────────────────────────────────
|
|
672
672
|
|
|
673
|
-
describe(
|
|
674
|
-
test(
|
|
673
|
+
describe('ErrorBoundary', () => {
|
|
674
|
+
test('renders fallback when child throws', () => {
|
|
675
675
|
const el = container()
|
|
676
676
|
function Broken(): never {
|
|
677
|
-
throw new Error(
|
|
677
|
+
throw new Error('boom')
|
|
678
678
|
}
|
|
679
679
|
mount(
|
|
680
680
|
h(ErrorBoundary, {
|
|
681
|
-
fallback: (err: unknown) => h(
|
|
681
|
+
fallback: (err: unknown) => h('p', { id: 'fb' }, String(err)),
|
|
682
682
|
children: h(Broken, null),
|
|
683
683
|
}),
|
|
684
684
|
el,
|
|
685
685
|
)
|
|
686
|
-
expect(el.querySelector(
|
|
686
|
+
expect(el.querySelector('#fb')?.textContent).toContain('boom')
|
|
687
687
|
})
|
|
688
688
|
|
|
689
|
-
test(
|
|
689
|
+
test('renders children when no error', () => {
|
|
690
690
|
const el = container()
|
|
691
691
|
function Fine() {
|
|
692
|
-
return h(
|
|
692
|
+
return h('p', { id: 'ok' }, 'works')
|
|
693
693
|
}
|
|
694
694
|
mount(
|
|
695
695
|
h(ErrorBoundary, {
|
|
696
|
-
fallback: () => h(
|
|
696
|
+
fallback: () => h('p', null, 'error'),
|
|
697
697
|
children: h(Fine, null),
|
|
698
698
|
}),
|
|
699
699
|
el,
|
|
700
700
|
)
|
|
701
|
-
expect(el.querySelector(
|
|
701
|
+
expect(el.querySelector('#ok')?.textContent).toBe('works')
|
|
702
702
|
})
|
|
703
703
|
|
|
704
|
-
test(
|
|
704
|
+
test('reset() clears error and re-renders children', () => {
|
|
705
705
|
const el = container()
|
|
706
706
|
let shouldThrow = true
|
|
707
707
|
|
|
708
708
|
function MaybeThrow() {
|
|
709
|
-
if (shouldThrow) throw new Error(
|
|
710
|
-
return h(
|
|
709
|
+
if (shouldThrow) throw new Error('recoverable')
|
|
710
|
+
return h('p', { id: 'recovered' }, 'back')
|
|
711
711
|
}
|
|
712
712
|
|
|
713
713
|
mount(
|
|
714
714
|
h(ErrorBoundary, {
|
|
715
715
|
fallback: (_err: unknown, reset: () => void) =>
|
|
716
716
|
h(
|
|
717
|
-
|
|
717
|
+
'button',
|
|
718
718
|
{
|
|
719
|
-
id:
|
|
719
|
+
id: 'retry',
|
|
720
720
|
onClick: () => {
|
|
721
721
|
shouldThrow = false
|
|
722
722
|
reset()
|
|
723
723
|
},
|
|
724
724
|
},
|
|
725
|
-
|
|
725
|
+
'retry',
|
|
726
726
|
),
|
|
727
727
|
children: h(MaybeThrow, null),
|
|
728
728
|
}),
|
|
@@ -730,77 +730,77 @@ describe("ErrorBoundary", () => {
|
|
|
730
730
|
)
|
|
731
731
|
|
|
732
732
|
// Fallback rendered
|
|
733
|
-
expect(el.querySelector(
|
|
734
|
-
expect(el.querySelector(
|
|
733
|
+
expect(el.querySelector('#retry')).not.toBeNull()
|
|
734
|
+
expect(el.querySelector('#recovered')).toBeNull()
|
|
735
735
|
|
|
736
736
|
// Click retry — reset() fires, shouldThrow is false, children re-render
|
|
737
|
-
;(el.querySelector(
|
|
737
|
+
;(el.querySelector('#retry') as HTMLButtonElement).click()
|
|
738
738
|
|
|
739
|
-
expect(el.querySelector(
|
|
740
|
-
expect(el.querySelector(
|
|
739
|
+
expect(el.querySelector('#recovered')?.textContent).toBe('back')
|
|
740
|
+
expect(el.querySelector('#retry')).toBeNull()
|
|
741
741
|
})
|
|
742
742
|
|
|
743
|
-
test(
|
|
743
|
+
test('reset() with signal-driven children', () => {
|
|
744
744
|
const el = container()
|
|
745
745
|
const broken = signal(true)
|
|
746
746
|
|
|
747
747
|
function Reactive() {
|
|
748
|
-
if (broken()) throw new Error(
|
|
749
|
-
return h(
|
|
748
|
+
if (broken()) throw new Error('signal error')
|
|
749
|
+
return h('p', { id: 'signal-ok' }, 'fixed')
|
|
750
750
|
}
|
|
751
751
|
|
|
752
752
|
mount(
|
|
753
753
|
h(ErrorBoundary, {
|
|
754
754
|
fallback: (_err: unknown, reset: () => void) =>
|
|
755
755
|
h(
|
|
756
|
-
|
|
756
|
+
'button',
|
|
757
757
|
{
|
|
758
|
-
id:
|
|
758
|
+
id: 'fix',
|
|
759
759
|
onClick: () => {
|
|
760
760
|
broken.set(false)
|
|
761
761
|
reset()
|
|
762
762
|
},
|
|
763
763
|
},
|
|
764
|
-
|
|
764
|
+
'fix',
|
|
765
765
|
),
|
|
766
766
|
children: h(Reactive, null),
|
|
767
767
|
}),
|
|
768
768
|
el,
|
|
769
769
|
)
|
|
770
770
|
|
|
771
|
-
expect(el.querySelector(
|
|
772
|
-
;(el.querySelector(
|
|
773
|
-
expect(el.querySelector(
|
|
771
|
+
expect(el.querySelector('#fix')).not.toBeNull()
|
|
772
|
+
;(el.querySelector('#fix') as HTMLButtonElement).click()
|
|
773
|
+
expect(el.querySelector('#signal-ok')?.textContent).toBe('fixed')
|
|
774
774
|
})
|
|
775
775
|
})
|
|
776
776
|
|
|
777
777
|
// ─── Transition component ─────────────────────────────────────────────────────
|
|
778
778
|
|
|
779
|
-
describe(
|
|
780
|
-
test(
|
|
779
|
+
describe('Transition', () => {
|
|
780
|
+
test('mounts child when show starts true', () => {
|
|
781
781
|
const el = container()
|
|
782
782
|
const visible = signal(true)
|
|
783
|
-
mount(h(Transition, { show: visible, children: h(
|
|
784
|
-
expect(el.querySelector(
|
|
783
|
+
mount(h(Transition, { show: visible, children: h('div', { id: 'target' }, 'hi') }), el)
|
|
784
|
+
expect(el.querySelector('#target')).not.toBeNull()
|
|
785
785
|
})
|
|
786
786
|
|
|
787
|
-
test(
|
|
787
|
+
test('does not mount child when show starts false', () => {
|
|
788
788
|
const el = container()
|
|
789
789
|
const visible = signal(false)
|
|
790
|
-
mount(h(Transition, { show: visible, children: h(
|
|
791
|
-
expect(el.querySelector(
|
|
790
|
+
mount(h(Transition, { show: visible, children: h('div', { id: 'target' }, 'hi') }), el)
|
|
791
|
+
expect(el.querySelector('#target')).toBeNull()
|
|
792
792
|
})
|
|
793
793
|
|
|
794
|
-
test(
|
|
794
|
+
test('mounts child reactively when show becomes true', () => {
|
|
795
795
|
const el = container()
|
|
796
796
|
const visible = signal(false)
|
|
797
|
-
mount(h(Transition, { show: visible, children: h(
|
|
798
|
-
expect(el.querySelector(
|
|
797
|
+
mount(h(Transition, { show: visible, children: h('div', { id: 'target' }, 'hi') }), el)
|
|
798
|
+
expect(el.querySelector('#target')).toBeNull()
|
|
799
799
|
visible.set(true)
|
|
800
|
-
expect(el.querySelector(
|
|
800
|
+
expect(el.querySelector('#target')).not.toBeNull()
|
|
801
801
|
})
|
|
802
802
|
|
|
803
|
-
test(
|
|
803
|
+
test('calls onBeforeEnter when entering', async () => {
|
|
804
804
|
const el = container()
|
|
805
805
|
const visible = signal(false)
|
|
806
806
|
let called = false
|
|
@@ -810,7 +810,7 @@ describe("Transition", () => {
|
|
|
810
810
|
onBeforeEnter: () => {
|
|
811
811
|
called = true
|
|
812
812
|
},
|
|
813
|
-
children: h(
|
|
813
|
+
children: h('div', { id: 't' }),
|
|
814
814
|
}),
|
|
815
815
|
el,
|
|
816
816
|
)
|
|
@@ -823,190 +823,190 @@ describe("Transition", () => {
|
|
|
823
823
|
|
|
824
824
|
// ─── Show component ───────────────────────────────────────────────────────────
|
|
825
825
|
|
|
826
|
-
describe(
|
|
827
|
-
test(
|
|
826
|
+
describe('Show', () => {
|
|
827
|
+
test('renders children when when() is truthy', () => {
|
|
828
828
|
const el = container()
|
|
829
|
-
mount(h(Show, { when: () => true }, h(
|
|
830
|
-
expect(el.querySelector(
|
|
829
|
+
mount(h(Show, { when: () => true }, h('span', { id: 's' }, 'yes')), el)
|
|
830
|
+
expect(el.querySelector('#s')).not.toBeNull()
|
|
831
831
|
})
|
|
832
832
|
|
|
833
|
-
test(
|
|
833
|
+
test('renders fallback when when() is falsy', () => {
|
|
834
834
|
const el = container()
|
|
835
835
|
mount(
|
|
836
836
|
h(
|
|
837
837
|
Show,
|
|
838
|
-
{ when: () => false, fallback: h(
|
|
839
|
-
h(
|
|
838
|
+
{ when: () => false, fallback: h('span', { id: 'fb' }, 'no') },
|
|
839
|
+
h('span', { id: 's' }, 'yes'),
|
|
840
840
|
),
|
|
841
841
|
el,
|
|
842
842
|
)
|
|
843
|
-
expect(el.querySelector(
|
|
844
|
-
expect(el.querySelector(
|
|
843
|
+
expect(el.querySelector('#s')).toBeNull()
|
|
844
|
+
expect(el.querySelector('#fb')).not.toBeNull()
|
|
845
845
|
})
|
|
846
846
|
|
|
847
|
-
test(
|
|
847
|
+
test('reactively toggles on signal change', () => {
|
|
848
848
|
const el = container()
|
|
849
849
|
const show = signal(false)
|
|
850
|
-
mount(h(Show, { when: show }, h(
|
|
851
|
-
expect(el.querySelector(
|
|
850
|
+
mount(h(Show, { when: show }, h('div', { id: 't' }, 'visible')), el)
|
|
851
|
+
expect(el.querySelector('#t')).toBeNull()
|
|
852
852
|
show.set(true)
|
|
853
|
-
expect(el.querySelector(
|
|
853
|
+
expect(el.querySelector('#t')).not.toBeNull()
|
|
854
854
|
show.set(false)
|
|
855
|
-
expect(el.querySelector(
|
|
855
|
+
expect(el.querySelector('#t')).toBeNull()
|
|
856
856
|
})
|
|
857
857
|
|
|
858
|
-
test(
|
|
858
|
+
test('renders nothing when falsy and no fallback', () => {
|
|
859
859
|
const el = container()
|
|
860
|
-
mount(h(Show, { when: () => false }, h(
|
|
861
|
-
expect(el.textContent).toBe(
|
|
860
|
+
mount(h(Show, { when: () => false }, h('div', null, 'hi')), el)
|
|
861
|
+
expect(el.textContent).toBe('')
|
|
862
862
|
})
|
|
863
863
|
})
|
|
864
864
|
|
|
865
865
|
// ─── Switch / Match components ────────────────────────────────────────────────
|
|
866
866
|
|
|
867
|
-
describe(
|
|
868
|
-
test(
|
|
867
|
+
describe('Switch / Match', () => {
|
|
868
|
+
test('renders first matching branch', () => {
|
|
869
869
|
const el = container()
|
|
870
|
-
const route = signal(
|
|
870
|
+
const route = signal('home')
|
|
871
871
|
mount(
|
|
872
872
|
h(
|
|
873
873
|
Switch,
|
|
874
|
-
{ fallback: h(
|
|
875
|
-
h(Match, { when: () => route() ===
|
|
876
|
-
h(Match, { when: () => route() ===
|
|
874
|
+
{ fallback: h('span', { id: 'notfound' }) },
|
|
875
|
+
h(Match, { when: () => route() === 'home' }, h('span', { id: 'home' })),
|
|
876
|
+
h(Match, { when: () => route() === 'about' }, h('span', { id: 'about' })),
|
|
877
877
|
),
|
|
878
878
|
el,
|
|
879
879
|
)
|
|
880
|
-
expect(el.querySelector(
|
|
881
|
-
expect(el.querySelector(
|
|
882
|
-
expect(el.querySelector(
|
|
880
|
+
expect(el.querySelector('#home')).not.toBeNull()
|
|
881
|
+
expect(el.querySelector('#about')).toBeNull()
|
|
882
|
+
expect(el.querySelector('#notfound')).toBeNull()
|
|
883
883
|
})
|
|
884
884
|
|
|
885
|
-
test(
|
|
885
|
+
test('renders fallback when no match', () => {
|
|
886
886
|
const el = container()
|
|
887
|
-
const route = signal(
|
|
887
|
+
const route = signal('other')
|
|
888
888
|
mount(
|
|
889
889
|
h(
|
|
890
890
|
Switch,
|
|
891
|
-
{ fallback: h(
|
|
892
|
-
h(Match, { when: () => route() ===
|
|
891
|
+
{ fallback: h('span', { id: 'notfound' }) },
|
|
892
|
+
h(Match, { when: () => route() === 'home' }, h('span', { id: 'home' })),
|
|
893
893
|
),
|
|
894
894
|
el,
|
|
895
895
|
)
|
|
896
|
-
expect(el.querySelector(
|
|
897
|
-
expect(el.querySelector(
|
|
896
|
+
expect(el.querySelector('#notfound')).not.toBeNull()
|
|
897
|
+
expect(el.querySelector('#home')).toBeNull()
|
|
898
898
|
})
|
|
899
899
|
|
|
900
|
-
test(
|
|
900
|
+
test('switches branch reactively', () => {
|
|
901
901
|
const el = container()
|
|
902
|
-
const route = signal(
|
|
902
|
+
const route = signal('home')
|
|
903
903
|
mount(
|
|
904
904
|
h(
|
|
905
905
|
Switch,
|
|
906
|
-
{ fallback: h(
|
|
907
|
-
h(Match, { when: () => route() ===
|
|
908
|
-
h(Match, { when: () => route() ===
|
|
906
|
+
{ fallback: h('span', { id: 'notfound' }) },
|
|
907
|
+
h(Match, { when: () => route() === 'home' }, h('span', { id: 'home' })),
|
|
908
|
+
h(Match, { when: () => route() === 'about' }, h('span', { id: 'about' })),
|
|
909
909
|
),
|
|
910
910
|
el,
|
|
911
911
|
)
|
|
912
|
-
expect(el.querySelector(
|
|
913
|
-
route.set(
|
|
914
|
-
expect(el.querySelector(
|
|
915
|
-
expect(el.querySelector(
|
|
916
|
-
route.set(
|
|
917
|
-
expect(el.querySelector(
|
|
912
|
+
expect(el.querySelector('#home')).not.toBeNull()
|
|
913
|
+
route.set('about')
|
|
914
|
+
expect(el.querySelector('#home')).toBeNull()
|
|
915
|
+
expect(el.querySelector('#about')).not.toBeNull()
|
|
916
|
+
route.set('other')
|
|
917
|
+
expect(el.querySelector('#notfound')).not.toBeNull()
|
|
918
918
|
})
|
|
919
919
|
})
|
|
920
920
|
|
|
921
921
|
// ─── Props (extended coverage) ───────────────────────────────────────────────
|
|
922
922
|
|
|
923
|
-
describe(
|
|
924
|
-
test(
|
|
923
|
+
describe('mount — props (extended)', () => {
|
|
924
|
+
test('style as string sets cssText', () => {
|
|
925
925
|
const el = container()
|
|
926
|
-
mount(h(
|
|
927
|
-
const div = el.querySelector(
|
|
928
|
-
expect(div.style.color).toBe(
|
|
929
|
-
expect(div.style.fontSize).toBe(
|
|
926
|
+
mount(h('div', { style: 'color: red; font-size: 14px' }), el)
|
|
927
|
+
const div = el.querySelector('div') as HTMLElement
|
|
928
|
+
expect(div.style.color).toBe('red')
|
|
929
|
+
expect(div.style.fontSize).toBe('14px')
|
|
930
930
|
})
|
|
931
931
|
|
|
932
|
-
test(
|
|
932
|
+
test('style as object sets individual properties', () => {
|
|
933
933
|
const el = container()
|
|
934
|
-
mount(h(
|
|
935
|
-
const div = el.querySelector(
|
|
936
|
-
expect(div.style.color).toBe(
|
|
937
|
-
expect(div.style.marginTop).toBe(
|
|
934
|
+
mount(h('div', { style: { color: 'blue', marginTop: '10px' } }), el)
|
|
935
|
+
const div = el.querySelector('div') as HTMLElement
|
|
936
|
+
expect(div.style.color).toBe('blue')
|
|
937
|
+
expect(div.style.marginTop).toBe('10px')
|
|
938
938
|
})
|
|
939
939
|
|
|
940
|
-
test(
|
|
940
|
+
test('style object auto-appends px to numeric values', () => {
|
|
941
941
|
const el = container()
|
|
942
|
-
mount(h(
|
|
943
|
-
const div = el.querySelector(
|
|
944
|
-
expect(div.style.height).toBe(
|
|
945
|
-
expect(div.style.marginTop).toBe(
|
|
946
|
-
expect(div.style.opacity).toBe(
|
|
947
|
-
expect(div.style.zIndex).toBe(
|
|
942
|
+
mount(h('div', { style: { height: 100, marginTop: 20, opacity: 0.5, zIndex: 10 } }), el)
|
|
943
|
+
const div = el.querySelector('div') as HTMLElement
|
|
944
|
+
expect(div.style.height).toBe('100px')
|
|
945
|
+
expect(div.style.marginTop).toBe('20px')
|
|
946
|
+
expect(div.style.opacity).toBe('0.5')
|
|
947
|
+
expect(div.style.zIndex).toBe('10')
|
|
948
948
|
})
|
|
949
949
|
|
|
950
|
-
test(
|
|
950
|
+
test('style object handles CSS custom properties', () => {
|
|
951
951
|
const el = container()
|
|
952
|
-
mount(h(
|
|
953
|
-
const div = el.querySelector(
|
|
954
|
-
expect(div.style.getPropertyValue(
|
|
952
|
+
mount(h('div', { style: { '--my-color': 'red' } }), el)
|
|
953
|
+
const div = el.querySelector('div') as HTMLElement
|
|
954
|
+
expect(div.style.getPropertyValue('--my-color')).toBe('red')
|
|
955
955
|
})
|
|
956
956
|
|
|
957
|
-
test(
|
|
957
|
+
test('className sets class attribute', () => {
|
|
958
958
|
const el = container()
|
|
959
|
-
mount(h(
|
|
960
|
-
expect(el.querySelector(
|
|
959
|
+
mount(h('div', { className: 'my-class' }), el)
|
|
960
|
+
expect(el.querySelector('div')?.getAttribute('class')).toBe('my-class')
|
|
961
961
|
})
|
|
962
962
|
|
|
963
|
-
test(
|
|
963
|
+
test('class null sets empty class', () => {
|
|
964
964
|
const el = container()
|
|
965
|
-
mount(h(
|
|
966
|
-
expect(el.querySelector(
|
|
965
|
+
mount(h('div', { class: null }), el)
|
|
966
|
+
expect(el.querySelector('div')?.getAttribute('class')).toBe('')
|
|
967
967
|
})
|
|
968
968
|
|
|
969
|
-
test(
|
|
969
|
+
test('boolean attribute true sets empty attr', () => {
|
|
970
970
|
const el = container()
|
|
971
|
-
mount(h(
|
|
972
|
-
const input = el.querySelector(
|
|
971
|
+
mount(h('input', { disabled: true }), el)
|
|
972
|
+
const input = el.querySelector('input') as HTMLInputElement
|
|
973
973
|
expect(input.disabled).toBe(true)
|
|
974
974
|
})
|
|
975
975
|
|
|
976
|
-
test(
|
|
976
|
+
test('boolean attribute false removes attr', () => {
|
|
977
977
|
const el = container()
|
|
978
|
-
mount(h(
|
|
979
|
-
const input = el.querySelector(
|
|
978
|
+
mount(h('input', { disabled: false }), el)
|
|
979
|
+
const input = el.querySelector('input') as HTMLInputElement
|
|
980
980
|
expect(input.disabled).toBe(false)
|
|
981
981
|
})
|
|
982
982
|
|
|
983
|
-
test(
|
|
983
|
+
test('event handler receives event object', () => {
|
|
984
984
|
const el = container()
|
|
985
985
|
let receivedEvent: Event | null = null
|
|
986
986
|
mount(
|
|
987
987
|
h(
|
|
988
|
-
|
|
988
|
+
'button',
|
|
989
989
|
{
|
|
990
990
|
onClick: (e: Event) => {
|
|
991
991
|
receivedEvent = e
|
|
992
992
|
},
|
|
993
993
|
},
|
|
994
|
-
|
|
994
|
+
'click',
|
|
995
995
|
),
|
|
996
996
|
el,
|
|
997
997
|
)
|
|
998
|
-
el.querySelector(
|
|
998
|
+
el.querySelector('button')?.click()
|
|
999
999
|
expect(receivedEvent).not.toBeNull()
|
|
1000
1000
|
expect(receivedEvent).toBeInstanceOf(Event)
|
|
1001
1001
|
})
|
|
1002
1002
|
|
|
1003
|
-
test(
|
|
1003
|
+
test('multiple event handlers on same element', () => {
|
|
1004
1004
|
const el = container()
|
|
1005
1005
|
let mouseDown = false
|
|
1006
1006
|
let mouseUp = false
|
|
1007
1007
|
mount(
|
|
1008
1008
|
h(
|
|
1009
|
-
|
|
1009
|
+
'div',
|
|
1010
1010
|
{
|
|
1011
1011
|
onMousedown: () => {
|
|
1012
1012
|
mouseDown = true
|
|
@@ -1015,212 +1015,212 @@ describe("mount — props (extended)", () => {
|
|
|
1015
1015
|
mouseUp = true
|
|
1016
1016
|
},
|
|
1017
1017
|
},
|
|
1018
|
-
|
|
1018
|
+
'target',
|
|
1019
1019
|
),
|
|
1020
1020
|
el,
|
|
1021
1021
|
)
|
|
1022
|
-
const div = el.querySelector(
|
|
1023
|
-
div.dispatchEvent(new MouseEvent(
|
|
1024
|
-
div.dispatchEvent(new MouseEvent(
|
|
1022
|
+
const div = el.querySelector('div') as HTMLElement
|
|
1023
|
+
div.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }))
|
|
1024
|
+
div.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }))
|
|
1025
1025
|
expect(mouseDown).toBe(true)
|
|
1026
1026
|
expect(mouseUp).toBe(true)
|
|
1027
1027
|
})
|
|
1028
1028
|
|
|
1029
|
-
test(
|
|
1029
|
+
test('event handler cleanup on unmount', () => {
|
|
1030
1030
|
const el = container()
|
|
1031
1031
|
let count = 0
|
|
1032
1032
|
const unmount = mount(
|
|
1033
1033
|
h(
|
|
1034
|
-
|
|
1034
|
+
'button',
|
|
1035
1035
|
{
|
|
1036
1036
|
onClick: () => {
|
|
1037
1037
|
count++
|
|
1038
1038
|
},
|
|
1039
1039
|
},
|
|
1040
|
-
|
|
1040
|
+
'click',
|
|
1041
1041
|
),
|
|
1042
1042
|
el,
|
|
1043
1043
|
)
|
|
1044
|
-
el.querySelector(
|
|
1044
|
+
el.querySelector('button')?.click()
|
|
1045
1045
|
expect(count).toBe(1)
|
|
1046
1046
|
unmount()
|
|
1047
1047
|
// Button removed from DOM so click won't reach it
|
|
1048
1048
|
expect(count).toBe(1)
|
|
1049
1049
|
})
|
|
1050
1050
|
|
|
1051
|
-
test(
|
|
1051
|
+
test('sanitizes javascript: in href', () => {
|
|
1052
1052
|
const el = container()
|
|
1053
|
-
mount(h(
|
|
1054
|
-
const a = el.querySelector(
|
|
1053
|
+
mount(h('a', { href: 'javascript:alert(1)' }), el)
|
|
1054
|
+
const a = el.querySelector('a') as HTMLAnchorElement
|
|
1055
1055
|
// Should not have the dangerous href set
|
|
1056
|
-
expect(a.getAttribute(
|
|
1056
|
+
expect(a.getAttribute('href')).not.toBe('javascript:alert(1)')
|
|
1057
1057
|
})
|
|
1058
1058
|
|
|
1059
|
-
test(
|
|
1059
|
+
test('sanitizes data: in src', () => {
|
|
1060
1060
|
const el = container()
|
|
1061
|
-
mount(h(
|
|
1062
|
-
const img = el.querySelector(
|
|
1063
|
-
expect(img.getAttribute(
|
|
1061
|
+
mount(h('img', { src: 'data:text/html,<script>alert(1)</script>' }), el)
|
|
1062
|
+
const img = el.querySelector('img') as HTMLImageElement
|
|
1063
|
+
expect(img.getAttribute('src')).not.toBe('data:text/html,<script>alert(1)</script>')
|
|
1064
1064
|
})
|
|
1065
1065
|
|
|
1066
|
-
test(
|
|
1066
|
+
test('allows safe href values', () => {
|
|
1067
1067
|
const el = container()
|
|
1068
|
-
mount(h(
|
|
1069
|
-
const a = el.querySelector(
|
|
1070
|
-
expect(a.href).toContain(
|
|
1068
|
+
mount(h('a', { href: 'https://example.com' }), el)
|
|
1069
|
+
const a = el.querySelector('a') as HTMLAnchorElement
|
|
1070
|
+
expect(a.href).toContain('https://example.com')
|
|
1071
1071
|
})
|
|
1072
1072
|
|
|
1073
|
-
test(
|
|
1073
|
+
test('innerHTML sets content', () => {
|
|
1074
1074
|
const el = container()
|
|
1075
|
-
mount(h(
|
|
1076
|
-
const div = el.querySelector(
|
|
1077
|
-
expect(div.innerHTML).toBe(
|
|
1075
|
+
mount(h('div', { innerHTML: '<b>bold</b>' }), el)
|
|
1076
|
+
const div = el.querySelector('div') as HTMLElement
|
|
1077
|
+
expect(div.innerHTML).toBe('<b>bold</b>')
|
|
1078
1078
|
})
|
|
1079
1079
|
|
|
1080
|
-
test(
|
|
1080
|
+
test('dangerouslySetInnerHTML sets __html content', () => {
|
|
1081
1081
|
const el = container()
|
|
1082
|
-
mount(h(
|
|
1083
|
-
const div = el.querySelector(
|
|
1084
|
-
expect(div.innerHTML).toBe(
|
|
1082
|
+
mount(h('div', { dangerouslySetInnerHTML: { __html: '<em>raw</em>' } }), el)
|
|
1083
|
+
const div = el.querySelector('div') as HTMLElement
|
|
1084
|
+
expect(div.innerHTML).toBe('<em>raw</em>')
|
|
1085
1085
|
})
|
|
1086
1086
|
|
|
1087
|
-
test(
|
|
1087
|
+
test('reactive style updates', () => {
|
|
1088
1088
|
const el = container()
|
|
1089
|
-
const color = signal(
|
|
1090
|
-
mount(h(
|
|
1091
|
-
const div = el.querySelector(
|
|
1092
|
-
expect(div.style.color).toBe(
|
|
1093
|
-
color.set(
|
|
1094
|
-
expect(div.style.color).toBe(
|
|
1089
|
+
const color = signal('red')
|
|
1090
|
+
mount(h('div', { style: () => `color: ${color()}` }), el)
|
|
1091
|
+
const div = el.querySelector('div') as HTMLElement
|
|
1092
|
+
expect(div.style.color).toBe('red')
|
|
1093
|
+
color.set('blue')
|
|
1094
|
+
expect(div.style.color).toBe('blue')
|
|
1095
1095
|
})
|
|
1096
1096
|
|
|
1097
|
-
test(
|
|
1097
|
+
test('DOM property (value) set via prop', () => {
|
|
1098
1098
|
const el = container()
|
|
1099
|
-
mount(h(
|
|
1100
|
-
const input = el.querySelector(
|
|
1101
|
-
expect(input.value).toBe(
|
|
1099
|
+
mount(h('input', { value: 'hello' }), el)
|
|
1100
|
+
const input = el.querySelector('input') as HTMLInputElement
|
|
1101
|
+
expect(input.value).toBe('hello')
|
|
1102
1102
|
})
|
|
1103
1103
|
|
|
1104
|
-
test(
|
|
1104
|
+
test('data-* attributes set correctly', () => {
|
|
1105
1105
|
const el = container()
|
|
1106
|
-
mount(h(
|
|
1107
|
-
const div = el.querySelector(
|
|
1108
|
-
expect(div.getAttribute(
|
|
1109
|
-
expect(div.getAttribute(
|
|
1106
|
+
mount(h('div', { 'data-testid': 'foo', 'data-count': '42' }), el)
|
|
1107
|
+
const div = el.querySelector('div') as HTMLElement
|
|
1108
|
+
expect(div.getAttribute('data-testid')).toBe('foo')
|
|
1109
|
+
expect(div.getAttribute('data-count')).toBe('42')
|
|
1110
1110
|
})
|
|
1111
1111
|
})
|
|
1112
1112
|
|
|
1113
1113
|
// ─── Keyed list (nodes.ts) — additional reorder patterns ────────────────────
|
|
1114
1114
|
|
|
1115
|
-
describe(
|
|
1115
|
+
describe('mount — For keyed list reorder patterns', () => {
|
|
1116
1116
|
type Item = { id: number; label: string }
|
|
1117
1117
|
|
|
1118
1118
|
function mountList(el: HTMLElement, items: ReturnType<typeof signal<Item[]>>) {
|
|
1119
1119
|
mount(
|
|
1120
1120
|
h(
|
|
1121
|
-
|
|
1121
|
+
'ul',
|
|
1122
1122
|
null,
|
|
1123
1123
|
For({
|
|
1124
1124
|
each: items,
|
|
1125
1125
|
by: (r) => r.id,
|
|
1126
|
-
children: (r) => h(
|
|
1126
|
+
children: (r) => h('li', { key: r.id }, r.label),
|
|
1127
1127
|
}),
|
|
1128
1128
|
),
|
|
1129
1129
|
el,
|
|
1130
1130
|
)
|
|
1131
1131
|
}
|
|
1132
1132
|
|
|
1133
|
-
test(
|
|
1133
|
+
test('reverse order', () => {
|
|
1134
1134
|
const el = container()
|
|
1135
1135
|
const items = signal<Item[]>([
|
|
1136
|
-
{ id: 1, label:
|
|
1137
|
-
{ id: 2, label:
|
|
1138
|
-
{ id: 3, label:
|
|
1139
|
-
{ id: 4, label:
|
|
1140
|
-
{ id: 5, label:
|
|
1136
|
+
{ id: 1, label: 'a' },
|
|
1137
|
+
{ id: 2, label: 'b' },
|
|
1138
|
+
{ id: 3, label: 'c' },
|
|
1139
|
+
{ id: 4, label: 'd' },
|
|
1140
|
+
{ id: 5, label: 'e' },
|
|
1141
1141
|
])
|
|
1142
1142
|
mountList(el, items)
|
|
1143
1143
|
items.set([
|
|
1144
|
-
{ id: 5, label:
|
|
1145
|
-
{ id: 4, label:
|
|
1146
|
-
{ id: 3, label:
|
|
1147
|
-
{ id: 2, label:
|
|
1148
|
-
{ id: 1, label:
|
|
1144
|
+
{ id: 5, label: 'e' },
|
|
1145
|
+
{ id: 4, label: 'd' },
|
|
1146
|
+
{ id: 3, label: 'c' },
|
|
1147
|
+
{ id: 2, label: 'b' },
|
|
1148
|
+
{ id: 1, label: 'a' },
|
|
1149
1149
|
])
|
|
1150
|
-
const lis = el.querySelectorAll(
|
|
1150
|
+
const lis = el.querySelectorAll('li')
|
|
1151
1151
|
expect(lis.length).toBe(5)
|
|
1152
|
-
expect(lis[0]?.textContent).toBe(
|
|
1153
|
-
expect(lis[1]?.textContent).toBe(
|
|
1154
|
-
expect(lis[2]?.textContent).toBe(
|
|
1155
|
-
expect(lis[3]?.textContent).toBe(
|
|
1156
|
-
expect(lis[4]?.textContent).toBe(
|
|
1152
|
+
expect(lis[0]?.textContent).toBe('e')
|
|
1153
|
+
expect(lis[1]?.textContent).toBe('d')
|
|
1154
|
+
expect(lis[2]?.textContent).toBe('c')
|
|
1155
|
+
expect(lis[3]?.textContent).toBe('b')
|
|
1156
|
+
expect(lis[4]?.textContent).toBe('a')
|
|
1157
1157
|
})
|
|
1158
1158
|
|
|
1159
|
-
test(
|
|
1159
|
+
test('move single item to front', () => {
|
|
1160
1160
|
const el = container()
|
|
1161
1161
|
const items = signal<Item[]>([
|
|
1162
|
-
{ id: 1, label:
|
|
1163
|
-
{ id: 2, label:
|
|
1164
|
-
{ id: 3, label:
|
|
1162
|
+
{ id: 1, label: 'a' },
|
|
1163
|
+
{ id: 2, label: 'b' },
|
|
1164
|
+
{ id: 3, label: 'c' },
|
|
1165
1165
|
])
|
|
1166
1166
|
mountList(el, items)
|
|
1167
1167
|
items.set([
|
|
1168
|
-
{ id: 3, label:
|
|
1169
|
-
{ id: 1, label:
|
|
1170
|
-
{ id: 2, label:
|
|
1168
|
+
{ id: 3, label: 'c' },
|
|
1169
|
+
{ id: 1, label: 'a' },
|
|
1170
|
+
{ id: 2, label: 'b' },
|
|
1171
1171
|
])
|
|
1172
|
-
const lis = el.querySelectorAll(
|
|
1173
|
-
expect(lis[0]?.textContent).toBe(
|
|
1174
|
-
expect(lis[1]?.textContent).toBe(
|
|
1175
|
-
expect(lis[2]?.textContent).toBe(
|
|
1172
|
+
const lis = el.querySelectorAll('li')
|
|
1173
|
+
expect(lis[0]?.textContent).toBe('c')
|
|
1174
|
+
expect(lis[1]?.textContent).toBe('a')
|
|
1175
|
+
expect(lis[2]?.textContent).toBe('b')
|
|
1176
1176
|
})
|
|
1177
1177
|
|
|
1178
|
-
test(
|
|
1178
|
+
test('prepend items', () => {
|
|
1179
1179
|
const el = container()
|
|
1180
1180
|
const items = signal<Item[]>([
|
|
1181
|
-
{ id: 3, label:
|
|
1182
|
-
{ id: 4, label:
|
|
1181
|
+
{ id: 3, label: 'c' },
|
|
1182
|
+
{ id: 4, label: 'd' },
|
|
1183
1183
|
])
|
|
1184
1184
|
mountList(el, items)
|
|
1185
1185
|
items.set([
|
|
1186
|
-
{ id: 1, label:
|
|
1187
|
-
{ id: 2, label:
|
|
1188
|
-
{ id: 3, label:
|
|
1189
|
-
{ id: 4, label:
|
|
1186
|
+
{ id: 1, label: 'a' },
|
|
1187
|
+
{ id: 2, label: 'b' },
|
|
1188
|
+
{ id: 3, label: 'c' },
|
|
1189
|
+
{ id: 4, label: 'd' },
|
|
1190
1190
|
])
|
|
1191
|
-
const lis = el.querySelectorAll(
|
|
1191
|
+
const lis = el.querySelectorAll('li')
|
|
1192
1192
|
expect(lis.length).toBe(4)
|
|
1193
|
-
expect(lis[0]?.textContent).toBe(
|
|
1194
|
-
expect(lis[1]?.textContent).toBe(
|
|
1195
|
-
expect(lis[2]?.textContent).toBe(
|
|
1196
|
-
expect(lis[3]?.textContent).toBe(
|
|
1193
|
+
expect(lis[0]?.textContent).toBe('a')
|
|
1194
|
+
expect(lis[1]?.textContent).toBe('b')
|
|
1195
|
+
expect(lis[2]?.textContent).toBe('c')
|
|
1196
|
+
expect(lis[3]?.textContent).toBe('d')
|
|
1197
1197
|
})
|
|
1198
1198
|
|
|
1199
|
-
test(
|
|
1199
|
+
test('interleave new items', () => {
|
|
1200
1200
|
const el = container()
|
|
1201
1201
|
const items = signal<Item[]>([
|
|
1202
|
-
{ id: 1, label:
|
|
1203
|
-
{ id: 3, label:
|
|
1204
|
-
{ id: 5, label:
|
|
1202
|
+
{ id: 1, label: 'a' },
|
|
1203
|
+
{ id: 3, label: 'c' },
|
|
1204
|
+
{ id: 5, label: 'e' },
|
|
1205
1205
|
])
|
|
1206
1206
|
mountList(el, items)
|
|
1207
1207
|
items.set([
|
|
1208
|
-
{ id: 1, label:
|
|
1209
|
-
{ id: 2, label:
|
|
1210
|
-
{ id: 3, label:
|
|
1211
|
-
{ id: 4, label:
|
|
1212
|
-
{ id: 5, label:
|
|
1208
|
+
{ id: 1, label: 'a' },
|
|
1209
|
+
{ id: 2, label: 'b' },
|
|
1210
|
+
{ id: 3, label: 'c' },
|
|
1211
|
+
{ id: 4, label: 'd' },
|
|
1212
|
+
{ id: 5, label: 'e' },
|
|
1213
1213
|
])
|
|
1214
|
-
const lis = el.querySelectorAll(
|
|
1214
|
+
const lis = el.querySelectorAll('li')
|
|
1215
1215
|
expect(lis.length).toBe(5)
|
|
1216
|
-
expect(lis[0]?.textContent).toBe(
|
|
1217
|
-
expect(lis[1]?.textContent).toBe(
|
|
1218
|
-
expect(lis[2]?.textContent).toBe(
|
|
1219
|
-
expect(lis[3]?.textContent).toBe(
|
|
1220
|
-
expect(lis[4]?.textContent).toBe(
|
|
1216
|
+
expect(lis[0]?.textContent).toBe('a')
|
|
1217
|
+
expect(lis[1]?.textContent).toBe('b')
|
|
1218
|
+
expect(lis[2]?.textContent).toBe('c')
|
|
1219
|
+
expect(lis[3]?.textContent).toBe('d')
|
|
1220
|
+
expect(lis[4]?.textContent).toBe('e')
|
|
1221
1221
|
})
|
|
1222
1222
|
|
|
1223
|
-
test(
|
|
1223
|
+
test('large reorder triggers LIS fallback (>8 diffs)', () => {
|
|
1224
1224
|
const el = container()
|
|
1225
1225
|
const initial = Array.from({ length: 20 }, (_, i) => ({
|
|
1226
1226
|
id: i + 1,
|
|
@@ -1232,119 +1232,119 @@ describe("mount — For keyed list reorder patterns", () => {
|
|
|
1232
1232
|
const shuffled = [...initial]
|
|
1233
1233
|
shuffled.splice(0, 15, ...shuffled.slice(0, 15).reverse())
|
|
1234
1234
|
items.set(shuffled)
|
|
1235
|
-
const lis = el.querySelectorAll(
|
|
1235
|
+
const lis = el.querySelectorAll('li')
|
|
1236
1236
|
expect(lis.length).toBe(20)
|
|
1237
1237
|
for (let i = 0; i < 20; i++) {
|
|
1238
1238
|
expect(lis[i]?.textContent).toBe(shuffled[i]?.label)
|
|
1239
1239
|
}
|
|
1240
1240
|
})
|
|
1241
1241
|
|
|
1242
|
-
test(
|
|
1242
|
+
test('remove from front and back simultaneously', () => {
|
|
1243
1243
|
const el = container()
|
|
1244
1244
|
const items = signal<Item[]>([
|
|
1245
|
-
{ id: 1, label:
|
|
1246
|
-
{ id: 2, label:
|
|
1247
|
-
{ id: 3, label:
|
|
1248
|
-
{ id: 4, label:
|
|
1249
|
-
{ id: 5, label:
|
|
1245
|
+
{ id: 1, label: 'a' },
|
|
1246
|
+
{ id: 2, label: 'b' },
|
|
1247
|
+
{ id: 3, label: 'c' },
|
|
1248
|
+
{ id: 4, label: 'd' },
|
|
1249
|
+
{ id: 5, label: 'e' },
|
|
1250
1250
|
])
|
|
1251
1251
|
mountList(el, items)
|
|
1252
1252
|
items.set([
|
|
1253
|
-
{ id: 2, label:
|
|
1254
|
-
{ id: 3, label:
|
|
1255
|
-
{ id: 4, label:
|
|
1253
|
+
{ id: 2, label: 'b' },
|
|
1254
|
+
{ id: 3, label: 'c' },
|
|
1255
|
+
{ id: 4, label: 'd' },
|
|
1256
1256
|
])
|
|
1257
|
-
const lis = el.querySelectorAll(
|
|
1257
|
+
const lis = el.querySelectorAll('li')
|
|
1258
1258
|
expect(lis.length).toBe(3)
|
|
1259
|
-
expect(lis[0]?.textContent).toBe(
|
|
1260
|
-
expect(lis[2]?.textContent).toBe(
|
|
1259
|
+
expect(lis[0]?.textContent).toBe('b')
|
|
1260
|
+
expect(lis[2]?.textContent).toBe('d')
|
|
1261
1261
|
})
|
|
1262
1262
|
|
|
1263
|
-
test(
|
|
1263
|
+
test('swap first and last', () => {
|
|
1264
1264
|
const el = container()
|
|
1265
1265
|
const items = signal<Item[]>([
|
|
1266
|
-
{ id: 1, label:
|
|
1267
|
-
{ id: 2, label:
|
|
1268
|
-
{ id: 3, label:
|
|
1266
|
+
{ id: 1, label: 'a' },
|
|
1267
|
+
{ id: 2, label: 'b' },
|
|
1268
|
+
{ id: 3, label: 'c' },
|
|
1269
1269
|
])
|
|
1270
1270
|
mountList(el, items)
|
|
1271
1271
|
items.set([
|
|
1272
|
-
{ id: 3, label:
|
|
1273
|
-
{ id: 2, label:
|
|
1274
|
-
{ id: 1, label:
|
|
1272
|
+
{ id: 3, label: 'c' },
|
|
1273
|
+
{ id: 2, label: 'b' },
|
|
1274
|
+
{ id: 1, label: 'a' },
|
|
1275
1275
|
])
|
|
1276
|
-
const lis = el.querySelectorAll(
|
|
1277
|
-
expect(lis[0]?.textContent).toBe(
|
|
1278
|
-
expect(lis[1]?.textContent).toBe(
|
|
1279
|
-
expect(lis[2]?.textContent).toBe(
|
|
1276
|
+
const lis = el.querySelectorAll('li')
|
|
1277
|
+
expect(lis[0]?.textContent).toBe('c')
|
|
1278
|
+
expect(lis[1]?.textContent).toBe('b')
|
|
1279
|
+
expect(lis[2]?.textContent).toBe('a')
|
|
1280
1280
|
})
|
|
1281
1281
|
|
|
1282
|
-
test(
|
|
1282
|
+
test('multiple rapid updates', () => {
|
|
1283
1283
|
const el = container()
|
|
1284
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
1284
|
+
const items = signal<Item[]>([{ id: 1, label: 'a' }])
|
|
1285
1285
|
mountList(el, items)
|
|
1286
1286
|
items.set([
|
|
1287
|
-
{ id: 1, label:
|
|
1288
|
-
{ id: 2, label:
|
|
1287
|
+
{ id: 1, label: 'a' },
|
|
1288
|
+
{ id: 2, label: 'b' },
|
|
1289
1289
|
])
|
|
1290
1290
|
items.set([
|
|
1291
|
-
{ id: 2, label:
|
|
1292
|
-
{ id: 3, label:
|
|
1291
|
+
{ id: 2, label: 'b' },
|
|
1292
|
+
{ id: 3, label: 'c' },
|
|
1293
1293
|
])
|
|
1294
|
-
items.set([{ id: 4, label:
|
|
1295
|
-
const lis = el.querySelectorAll(
|
|
1294
|
+
items.set([{ id: 4, label: 'd' }])
|
|
1295
|
+
const lis = el.querySelectorAll('li')
|
|
1296
1296
|
expect(lis.length).toBe(1)
|
|
1297
|
-
expect(lis[0]?.textContent).toBe(
|
|
1297
|
+
expect(lis[0]?.textContent).toBe('d')
|
|
1298
1298
|
})
|
|
1299
1299
|
})
|
|
1300
1300
|
|
|
1301
1301
|
// ─── Transition (extended coverage) ──────────────────────────────────────────
|
|
1302
1302
|
|
|
1303
|
-
describe(
|
|
1304
|
-
test(
|
|
1303
|
+
describe('Transition — extended', () => {
|
|
1304
|
+
test('custom class names', () => {
|
|
1305
1305
|
const el = container()
|
|
1306
1306
|
const visible = signal(false)
|
|
1307
1307
|
mount(
|
|
1308
1308
|
h(Transition, {
|
|
1309
1309
|
show: visible,
|
|
1310
|
-
enterFrom:
|
|
1311
|
-
enterActive:
|
|
1312
|
-
enterTo:
|
|
1313
|
-
children: h(
|
|
1310
|
+
enterFrom: 'my-enter-from',
|
|
1311
|
+
enterActive: 'my-enter-active',
|
|
1312
|
+
enterTo: 'my-enter-to',
|
|
1313
|
+
children: h('div', { id: 'custom' }, 'content'),
|
|
1314
1314
|
}),
|
|
1315
1315
|
el,
|
|
1316
1316
|
)
|
|
1317
|
-
expect(el.querySelector(
|
|
1317
|
+
expect(el.querySelector('#custom')).toBeNull()
|
|
1318
1318
|
visible.set(true)
|
|
1319
|
-
expect(el.querySelector(
|
|
1319
|
+
expect(el.querySelector('#custom')).not.toBeNull()
|
|
1320
1320
|
})
|
|
1321
1321
|
|
|
1322
|
-
test(
|
|
1322
|
+
test('leave hides element after animation', async () => {
|
|
1323
1323
|
const el = container()
|
|
1324
1324
|
const visible = signal(true)
|
|
1325
1325
|
mount(
|
|
1326
1326
|
h(Transition, {
|
|
1327
1327
|
show: visible,
|
|
1328
|
-
children: h(
|
|
1328
|
+
children: h('div', { id: 'leave-test' }, 'content'),
|
|
1329
1329
|
}),
|
|
1330
1330
|
el,
|
|
1331
1331
|
)
|
|
1332
|
-
expect(el.querySelector(
|
|
1332
|
+
expect(el.querySelector('#leave-test')).not.toBeNull()
|
|
1333
1333
|
visible.set(false)
|
|
1334
1334
|
// After rAF + transitionend, the element should be removed
|
|
1335
1335
|
// In happy-dom, we simulate the transitionend
|
|
1336
|
-
const target = el.querySelector(
|
|
1336
|
+
const target = el.querySelector('#leave-test')
|
|
1337
1337
|
if (target) {
|
|
1338
1338
|
// Wait for the requestAnimationFrame callback
|
|
1339
1339
|
await new Promise<void>((r) => requestAnimationFrame(() => r()))
|
|
1340
|
-
target.dispatchEvent(new Event(
|
|
1340
|
+
target.dispatchEvent(new Event('transitionend'))
|
|
1341
1341
|
}
|
|
1342
1342
|
// isMounted should now be false
|
|
1343
1343
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1344
|
-
expect(el.querySelector(
|
|
1344
|
+
expect(el.querySelector('#leave-test')).toBeNull()
|
|
1345
1345
|
})
|
|
1346
1346
|
|
|
1347
|
-
test(
|
|
1347
|
+
test('appear triggers enter animation on initial mount', async () => {
|
|
1348
1348
|
const el = container()
|
|
1349
1349
|
const visible = signal(true)
|
|
1350
1350
|
let beforeEnterCalled = false
|
|
@@ -1355,7 +1355,7 @@ describe("Transition — extended", () => {
|
|
|
1355
1355
|
onBeforeEnter: () => {
|
|
1356
1356
|
beforeEnterCalled = true
|
|
1357
1357
|
},
|
|
1358
|
-
children: h(
|
|
1358
|
+
children: h('div', { id: 'appear-test' }, 'content'),
|
|
1359
1359
|
}),
|
|
1360
1360
|
el,
|
|
1361
1361
|
)
|
|
@@ -1363,7 +1363,7 @@ describe("Transition — extended", () => {
|
|
|
1363
1363
|
expect(beforeEnterCalled).toBe(true)
|
|
1364
1364
|
})
|
|
1365
1365
|
|
|
1366
|
-
test(
|
|
1366
|
+
test('calls onBeforeLeave when leaving', async () => {
|
|
1367
1367
|
const el = container()
|
|
1368
1368
|
const visible = signal(true)
|
|
1369
1369
|
let beforeLeaveCalled = false
|
|
@@ -1373,7 +1373,7 @@ describe("Transition — extended", () => {
|
|
|
1373
1373
|
onBeforeLeave: () => {
|
|
1374
1374
|
beforeLeaveCalled = true
|
|
1375
1375
|
},
|
|
1376
|
-
children: h(
|
|
1376
|
+
children: h('div', { id: 'leave-cb' }, 'content'),
|
|
1377
1377
|
}),
|
|
1378
1378
|
el,
|
|
1379
1379
|
)
|
|
@@ -1382,14 +1382,14 @@ describe("Transition — extended", () => {
|
|
|
1382
1382
|
expect(beforeLeaveCalled).toBe(true)
|
|
1383
1383
|
})
|
|
1384
1384
|
|
|
1385
|
-
test(
|
|
1385
|
+
test('re-entering during leave cancels leave', async () => {
|
|
1386
1386
|
const el = container()
|
|
1387
1387
|
const visible = signal(true)
|
|
1388
1388
|
mount(
|
|
1389
1389
|
h(Transition, {
|
|
1390
1390
|
show: visible,
|
|
1391
|
-
name:
|
|
1392
|
-
children: h(
|
|
1391
|
+
name: 'fade',
|
|
1392
|
+
children: h('div', { id: 'reenter' }, 'content'),
|
|
1393
1393
|
}),
|
|
1394
1394
|
el,
|
|
1395
1395
|
)
|
|
@@ -1398,157 +1398,157 @@ describe("Transition — extended", () => {
|
|
|
1398
1398
|
// Before the leave animation finishes, re-enter
|
|
1399
1399
|
visible.set(true)
|
|
1400
1400
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1401
|
-
expect(el.querySelector(
|
|
1401
|
+
expect(el.querySelector('#reenter')).not.toBeNull()
|
|
1402
1402
|
})
|
|
1403
1403
|
|
|
1404
|
-
test(
|
|
1404
|
+
test('transition with name prefix', () => {
|
|
1405
1405
|
const el = container()
|
|
1406
1406
|
const visible = signal(true)
|
|
1407
1407
|
mount(
|
|
1408
1408
|
h(Transition, {
|
|
1409
1409
|
show: visible,
|
|
1410
|
-
name:
|
|
1411
|
-
children: h(
|
|
1410
|
+
name: 'slide',
|
|
1411
|
+
children: h('div', { id: 'named' }, 'content'),
|
|
1412
1412
|
}),
|
|
1413
1413
|
el,
|
|
1414
1414
|
)
|
|
1415
|
-
expect(el.querySelector(
|
|
1415
|
+
expect(el.querySelector('#named')).not.toBeNull()
|
|
1416
1416
|
})
|
|
1417
1417
|
})
|
|
1418
1418
|
|
|
1419
1419
|
// ─── Hydration ───────────────────────────────────────────────────────────────
|
|
1420
1420
|
|
|
1421
|
-
describe(
|
|
1422
|
-
test(
|
|
1421
|
+
describe('hydrateRoot', () => {
|
|
1422
|
+
test('hydrates basic element', async () => {
|
|
1423
1423
|
const el = container()
|
|
1424
|
-
el.innerHTML =
|
|
1425
|
-
const cleanup = hydrateRoot(el, h(
|
|
1426
|
-
expect(el.querySelector(
|
|
1424
|
+
el.innerHTML = '<div><span>hello</span></div>'
|
|
1425
|
+
const cleanup = hydrateRoot(el, h('div', null, h('span', null, 'hello')))
|
|
1426
|
+
expect(el.querySelector('span')?.textContent).toBe('hello')
|
|
1427
1427
|
cleanup()
|
|
1428
1428
|
})
|
|
1429
1429
|
|
|
1430
|
-
test(
|
|
1430
|
+
test('hydrates and attaches event handler', async () => {
|
|
1431
1431
|
const el = container()
|
|
1432
|
-
el.innerHTML =
|
|
1432
|
+
el.innerHTML = '<button>click me</button>'
|
|
1433
1433
|
let clicked = false
|
|
1434
1434
|
hydrateRoot(
|
|
1435
1435
|
el,
|
|
1436
1436
|
h(
|
|
1437
|
-
|
|
1437
|
+
'button',
|
|
1438
1438
|
{
|
|
1439
1439
|
onClick: () => {
|
|
1440
1440
|
clicked = true
|
|
1441
1441
|
},
|
|
1442
1442
|
},
|
|
1443
|
-
|
|
1443
|
+
'click me',
|
|
1444
1444
|
),
|
|
1445
1445
|
)
|
|
1446
|
-
el.querySelector(
|
|
1446
|
+
el.querySelector('button')?.click()
|
|
1447
1447
|
expect(clicked).toBe(true)
|
|
1448
1448
|
})
|
|
1449
1449
|
|
|
1450
|
-
test(
|
|
1450
|
+
test('hydrates text content', async () => {
|
|
1451
1451
|
const el = container()
|
|
1452
|
-
el.innerHTML =
|
|
1453
|
-
const cleanup = hydrateRoot(el, h(
|
|
1454
|
-
expect(el.querySelector(
|
|
1452
|
+
el.innerHTML = '<p>some text</p>'
|
|
1453
|
+
const cleanup = hydrateRoot(el, h('p', null, 'some text'))
|
|
1454
|
+
expect(el.querySelector('p')?.textContent).toBe('some text')
|
|
1455
1455
|
cleanup()
|
|
1456
1456
|
})
|
|
1457
1457
|
|
|
1458
|
-
test(
|
|
1458
|
+
test('hydrates reactive text', async () => {
|
|
1459
1459
|
const el = container()
|
|
1460
|
-
el.innerHTML =
|
|
1461
|
-
const text = signal(
|
|
1460
|
+
el.innerHTML = '<div>initial</div>'
|
|
1461
|
+
const text = signal('initial')
|
|
1462
1462
|
hydrateRoot(
|
|
1463
1463
|
el,
|
|
1464
|
-
h(
|
|
1464
|
+
h('div', null, () => text()),
|
|
1465
1465
|
)
|
|
1466
|
-
expect(el.querySelector(
|
|
1467
|
-
text.set(
|
|
1468
|
-
expect(el.querySelector(
|
|
1466
|
+
expect(el.querySelector('div')?.textContent).toBe('initial')
|
|
1467
|
+
text.set('updated')
|
|
1468
|
+
expect(el.querySelector('div')?.textContent).toBe('updated')
|
|
1469
1469
|
})
|
|
1470
1470
|
|
|
1471
|
-
test(
|
|
1471
|
+
test('hydrates nested elements', async () => {
|
|
1472
1472
|
const el = container()
|
|
1473
|
-
el.innerHTML =
|
|
1474
|
-
const cleanup = hydrateRoot(el, h(
|
|
1475
|
-
expect(el.querySelector(
|
|
1473
|
+
el.innerHTML = '<div><p><span>deep</span></p></div>'
|
|
1474
|
+
const cleanup = hydrateRoot(el, h('div', null, h('p', null, h('span', null, 'deep'))))
|
|
1475
|
+
expect(el.querySelector('span')?.textContent).toBe('deep')
|
|
1476
1476
|
cleanup()
|
|
1477
1477
|
})
|
|
1478
1478
|
|
|
1479
|
-
test(
|
|
1479
|
+
test('hydrates component', async () => {
|
|
1480
1480
|
const el = container()
|
|
1481
|
-
el.innerHTML =
|
|
1482
|
-
const Greeting = defineComponent(() => h(
|
|
1481
|
+
el.innerHTML = '<p>Hello, World!</p>'
|
|
1482
|
+
const Greeting = defineComponent(() => h('p', null, 'Hello, World!'))
|
|
1483
1483
|
const cleanup = hydrateRoot(el, h(Greeting, null))
|
|
1484
|
-
expect(el.querySelector(
|
|
1484
|
+
expect(el.querySelector('p')?.textContent).toBe('Hello, World!')
|
|
1485
1485
|
cleanup()
|
|
1486
1486
|
})
|
|
1487
1487
|
})
|
|
1488
1488
|
|
|
1489
1489
|
// ─── Mount edge cases ────────────────────────────────────────────────────────
|
|
1490
1490
|
|
|
1491
|
-
describe(
|
|
1492
|
-
test(
|
|
1491
|
+
describe('mount — edge cases', () => {
|
|
1492
|
+
test('null children in fragment', () => {
|
|
1493
1493
|
const el = container()
|
|
1494
|
-
mount(h(Fragment, null, null,
|
|
1495
|
-
expect(el.textContent).toBe(
|
|
1494
|
+
mount(h(Fragment, null, null, 'text', null), el)
|
|
1495
|
+
expect(el.textContent).toBe('text')
|
|
1496
1496
|
})
|
|
1497
1497
|
|
|
1498
|
-
test(
|
|
1498
|
+
test('deeply nested fragments', () => {
|
|
1499
1499
|
const el = container()
|
|
1500
|
-
mount(h(Fragment, null, h(Fragment, null, h(Fragment, null, h(
|
|
1501
|
-
expect(el.querySelector(
|
|
1500
|
+
mount(h(Fragment, null, h(Fragment, null, h(Fragment, null, h('span', null, 'deep')))), el)
|
|
1501
|
+
expect(el.querySelector('span')?.textContent).toBe('deep')
|
|
1502
1502
|
})
|
|
1503
1503
|
|
|
1504
|
-
test(
|
|
1504
|
+
test('component returning null', () => {
|
|
1505
1505
|
const el = container()
|
|
1506
1506
|
const NullComp = defineComponent(() => null)
|
|
1507
1507
|
mount(h(NullComp, null), el)
|
|
1508
|
-
expect(el.innerHTML).toBe(
|
|
1508
|
+
expect(el.innerHTML).toBe('')
|
|
1509
1509
|
})
|
|
1510
1510
|
|
|
1511
|
-
test(
|
|
1511
|
+
test('component returning fragment with mixed children', () => {
|
|
1512
1512
|
const el = container()
|
|
1513
|
-
const Mixed = defineComponent(() => h(Fragment, null,
|
|
1513
|
+
const Mixed = defineComponent(() => h(Fragment, null, 'text', h('b', null, 'bold'), null, 42))
|
|
1514
1514
|
mount(h(Mixed, null), el)
|
|
1515
|
-
expect(el.textContent).toContain(
|
|
1516
|
-
expect(el.querySelector(
|
|
1517
|
-
expect(el.textContent).toContain(
|
|
1515
|
+
expect(el.textContent).toContain('text')
|
|
1516
|
+
expect(el.querySelector('b')?.textContent).toBe('bold')
|
|
1517
|
+
expect(el.textContent).toContain('42')
|
|
1518
1518
|
})
|
|
1519
1519
|
|
|
1520
|
-
test(
|
|
1520
|
+
test('mounting array of children', () => {
|
|
1521
1521
|
const el = container()
|
|
1522
|
-
mount(h(
|
|
1523
|
-
expect(el.querySelectorAll(
|
|
1522
|
+
mount(h('div', null, ...[h('span', null, 'a'), h('span', null, 'b'), h('span', null, 'c')]), el)
|
|
1523
|
+
expect(el.querySelectorAll('span').length).toBe(3)
|
|
1524
1524
|
})
|
|
1525
1525
|
|
|
1526
|
-
test(
|
|
1526
|
+
test('reactive child toggling between null and element', () => {
|
|
1527
1527
|
const el = container()
|
|
1528
1528
|
const show = signal(false)
|
|
1529
1529
|
mount(
|
|
1530
|
-
h(
|
|
1530
|
+
h('div', null, () => (show() ? h('span', { id: 'toggle' }, 'yes') : null)),
|
|
1531
1531
|
el,
|
|
1532
1532
|
)
|
|
1533
|
-
expect(el.querySelector(
|
|
1533
|
+
expect(el.querySelector('#toggle')).toBeNull()
|
|
1534
1534
|
show.set(true)
|
|
1535
|
-
expect(el.querySelector(
|
|
1535
|
+
expect(el.querySelector('#toggle')).not.toBeNull()
|
|
1536
1536
|
show.set(false)
|
|
1537
|
-
expect(el.querySelector(
|
|
1537
|
+
expect(el.querySelector('#toggle')).toBeNull()
|
|
1538
1538
|
})
|
|
1539
1539
|
|
|
1540
|
-
test(
|
|
1540
|
+
test('reactive child returning component renders content', () => {
|
|
1541
1541
|
const el = container()
|
|
1542
|
-
const Dashboard = () => h(
|
|
1543
|
-
const Store = () => h(
|
|
1544
|
-
const activeTab = signal(
|
|
1542
|
+
const Dashboard = () => h('div', { id: 'dashboard' }, 'Dashboard content')
|
|
1543
|
+
const Store = () => h('div', { id: 'store' }, 'Store content')
|
|
1544
|
+
const activeTab = signal('dashboard')
|
|
1545
1545
|
const tabs = [
|
|
1546
|
-
{ id:
|
|
1547
|
-
{ id:
|
|
1546
|
+
{ id: 'dashboard', component: Dashboard },
|
|
1547
|
+
{ id: 'store', component: Store },
|
|
1548
1548
|
]
|
|
1549
1549
|
|
|
1550
1550
|
mount(
|
|
1551
|
-
h(
|
|
1551
|
+
h('div', null, () => {
|
|
1552
1552
|
const tab = tabs.find((t) => t.id === activeTab())
|
|
1553
1553
|
if (!tab) return null
|
|
1554
1554
|
const Component = tab.component
|
|
@@ -1557,528 +1557,528 @@ describe("mount — edge cases", () => {
|
|
|
1557
1557
|
el,
|
|
1558
1558
|
)
|
|
1559
1559
|
|
|
1560
|
-
expect(el.querySelector(
|
|
1561
|
-
expect(el.querySelector(
|
|
1560
|
+
expect(el.querySelector('#dashboard')).not.toBeNull()
|
|
1561
|
+
expect(el.querySelector('#dashboard')?.textContent).toBe('Dashboard content')
|
|
1562
1562
|
|
|
1563
|
-
activeTab.set(
|
|
1564
|
-
expect(el.querySelector(
|
|
1565
|
-
expect(el.querySelector(
|
|
1566
|
-
expect(el.querySelector(
|
|
1563
|
+
activeTab.set('store')
|
|
1564
|
+
expect(el.querySelector('#store')).not.toBeNull()
|
|
1565
|
+
expect(el.querySelector('#store')?.textContent).toBe('Store content')
|
|
1566
|
+
expect(el.querySelector('#dashboard')).toBeNull()
|
|
1567
1567
|
})
|
|
1568
1568
|
|
|
1569
|
-
test(
|
|
1569
|
+
test('reactive child returning component with internal signals', () => {
|
|
1570
1570
|
const el = container()
|
|
1571
1571
|
const Dashboard = () => {
|
|
1572
1572
|
const count = signal(0)
|
|
1573
|
-
return h(
|
|
1573
|
+
return h('div', { id: 'dashboard' }, () => `Count: ${count()}`)
|
|
1574
1574
|
}
|
|
1575
1575
|
const Settings = () => {
|
|
1576
|
-
return h(
|
|
1576
|
+
return h('div', { id: 'settings' }, h('span', null, 'Settings page'))
|
|
1577
1577
|
}
|
|
1578
|
-
const activeTab = signal<string>(
|
|
1578
|
+
const activeTab = signal<string>('dashboard')
|
|
1579
1579
|
|
|
1580
1580
|
mount(
|
|
1581
|
-
h(
|
|
1581
|
+
h('div', null, () => {
|
|
1582
1582
|
const tab = activeTab()
|
|
1583
|
-
if (tab ===
|
|
1584
|
-
if (tab ===
|
|
1583
|
+
if (tab === 'dashboard') return h(Dashboard, null)
|
|
1584
|
+
if (tab === 'settings') return h(Settings, null)
|
|
1585
1585
|
return null
|
|
1586
1586
|
}),
|
|
1587
1587
|
el,
|
|
1588
1588
|
)
|
|
1589
1589
|
|
|
1590
|
-
expect(el.querySelector(
|
|
1591
|
-
expect(el.querySelector(
|
|
1590
|
+
expect(el.querySelector('#dashboard')).not.toBeNull()
|
|
1591
|
+
expect(el.querySelector('#dashboard')?.textContent).toBe('Count: 0')
|
|
1592
1592
|
|
|
1593
|
-
activeTab.set(
|
|
1594
|
-
expect(el.querySelector(
|
|
1595
|
-
expect(el.querySelector(
|
|
1596
|
-
expect(el.querySelector(
|
|
1593
|
+
activeTab.set('settings')
|
|
1594
|
+
expect(el.querySelector('#settings')).not.toBeNull()
|
|
1595
|
+
expect(el.querySelector('#settings')?.textContent).toBe('Settings page')
|
|
1596
|
+
expect(el.querySelector('#dashboard')).toBeNull()
|
|
1597
1597
|
|
|
1598
|
-
activeTab.set(
|
|
1599
|
-
expect(el.querySelector(
|
|
1600
|
-
expect(el.querySelector(
|
|
1598
|
+
activeTab.set('none')
|
|
1599
|
+
expect(el.querySelector('#settings')).toBeNull()
|
|
1600
|
+
expect(el.querySelector('#dashboard')).toBeNull()
|
|
1601
1601
|
})
|
|
1602
1602
|
|
|
1603
|
-
test(
|
|
1603
|
+
test('reactive Dynamic component switching', () => {
|
|
1604
1604
|
const el = container()
|
|
1605
|
-
const Dashboard = () => h(
|
|
1606
|
-
const Settings = () => h(
|
|
1607
|
-
const activeTab = signal<string>(
|
|
1605
|
+
const Dashboard = () => h('div', { id: 'dashboard' }, 'Dashboard')
|
|
1606
|
+
const Settings = () => h('div', { id: 'settings' }, 'Settings')
|
|
1607
|
+
const activeTab = signal<string>('dashboard')
|
|
1608
1608
|
const components: Record<string, ComponentFn> = { dashboard: Dashboard, settings: Settings }
|
|
1609
1609
|
|
|
1610
1610
|
mount(
|
|
1611
|
-
h(
|
|
1611
|
+
h('div', null, () => h(Dynamic, { component: components[activeTab()] })),
|
|
1612
1612
|
el,
|
|
1613
1613
|
)
|
|
1614
1614
|
|
|
1615
|
-
expect(el.querySelector(
|
|
1615
|
+
expect(el.querySelector('#dashboard')?.textContent).toBe('Dashboard')
|
|
1616
1616
|
|
|
1617
|
-
activeTab.set(
|
|
1618
|
-
expect(el.querySelector(
|
|
1619
|
-
expect(el.querySelector(
|
|
1617
|
+
activeTab.set('settings')
|
|
1618
|
+
expect(el.querySelector('#settings')?.textContent).toBe('Settings')
|
|
1619
|
+
expect(el.querySelector('#dashboard')).toBeNull()
|
|
1620
1620
|
})
|
|
1621
1621
|
|
|
1622
|
-
test(
|
|
1622
|
+
test('boolean false renders nothing', () => {
|
|
1623
1623
|
const el = container()
|
|
1624
|
-
mount(h(
|
|
1625
|
-
expect(el.querySelector(
|
|
1624
|
+
mount(h('div', null, false), el)
|
|
1625
|
+
expect(el.querySelector('div')?.textContent).toBe('')
|
|
1626
1626
|
})
|
|
1627
1627
|
|
|
1628
|
-
test(
|
|
1628
|
+
test('number 0 renders as text', () => {
|
|
1629
1629
|
const el = container()
|
|
1630
|
-
mount(h(
|
|
1631
|
-
expect(el.querySelector(
|
|
1630
|
+
mount(h('div', null, 0), el)
|
|
1631
|
+
expect(el.querySelector('div')?.textContent).toBe('0')
|
|
1632
1632
|
})
|
|
1633
1633
|
|
|
1634
|
-
test(
|
|
1634
|
+
test('empty string renders as text node', () => {
|
|
1635
1635
|
const el = container()
|
|
1636
|
-
mount(h(
|
|
1637
|
-
expect(el.querySelector(
|
|
1636
|
+
mount(h('div', null, ''), el)
|
|
1637
|
+
expect(el.querySelector('div')?.textContent).toBe('')
|
|
1638
1638
|
})
|
|
1639
1639
|
|
|
1640
|
-
test(
|
|
1640
|
+
test('component with children prop', () => {
|
|
1641
1641
|
const el = container()
|
|
1642
1642
|
const Wrapper = defineComponent((props: { children?: VNodeChild }) => {
|
|
1643
|
-
return h(
|
|
1643
|
+
return h('div', { id: 'wrapper' }, props.children)
|
|
1644
1644
|
})
|
|
1645
|
-
mount(h(Wrapper, null, h(
|
|
1646
|
-
expect(el.querySelector(
|
|
1645
|
+
mount(h(Wrapper, null, h('span', null, 'child')), el)
|
|
1646
|
+
expect(el.querySelector('#wrapper span')?.textContent).toBe('child')
|
|
1647
1647
|
})
|
|
1648
1648
|
})
|
|
1649
1649
|
|
|
1650
1650
|
// ─── KeepAlive ───────────────────────────────────────────────────────────────
|
|
1651
1651
|
|
|
1652
|
-
describe(
|
|
1653
|
-
test(
|
|
1652
|
+
describe('KeepAlive', () => {
|
|
1653
|
+
test('mounts children and preserves them when toggled', async () => {
|
|
1654
1654
|
const el = container()
|
|
1655
1655
|
const active = signal(true)
|
|
1656
|
-
mount(h(KeepAlive, { active }, h(
|
|
1656
|
+
mount(h(KeepAlive, { active }, h('div', { id: 'kept' }, 'alive')), el)
|
|
1657
1657
|
// KeepAlive mounts in onMount which fires sync in this framework
|
|
1658
1658
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1659
|
-
expect(el.querySelector(
|
|
1659
|
+
expect(el.querySelector('#kept')).not.toBeNull()
|
|
1660
1660
|
active.set(false)
|
|
1661
1661
|
// Content should still exist in DOM but container hidden
|
|
1662
|
-
expect(el.querySelector(
|
|
1662
|
+
expect(el.querySelector('#kept')).not.toBeNull()
|
|
1663
1663
|
})
|
|
1664
1664
|
|
|
1665
|
-
test(
|
|
1665
|
+
test('cleanup disposes effect and child cleanup', async () => {
|
|
1666
1666
|
const el = container()
|
|
1667
1667
|
const active = signal(true)
|
|
1668
|
-
const unmount = mount(h(KeepAlive, { active }, h(
|
|
1668
|
+
const unmount = mount(h(KeepAlive, { active }, h('div', { id: 'ka-cleanup' }, 'content')), el)
|
|
1669
1669
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1670
|
-
expect(el.querySelector(
|
|
1670
|
+
expect(el.querySelector('#ka-cleanup')).not.toBeNull()
|
|
1671
1671
|
unmount()
|
|
1672
1672
|
// After unmount, the KeepAlive container is gone
|
|
1673
|
-
expect(el.innerHTML).toBe(
|
|
1673
|
+
expect(el.innerHTML).toBe('')
|
|
1674
1674
|
})
|
|
1675
1675
|
|
|
1676
|
-
test(
|
|
1676
|
+
test('active defaults to true when not provided', async () => {
|
|
1677
1677
|
const el = container()
|
|
1678
|
-
mount(h(KeepAlive, {}, h(
|
|
1678
|
+
mount(h(KeepAlive, {}, h('div', { id: 'ka-default' }, 'visible')), el)
|
|
1679
1679
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1680
|
-
expect(el.querySelector(
|
|
1680
|
+
expect(el.querySelector('#ka-default')).not.toBeNull()
|
|
1681
1681
|
// Container should be visible (display not set to none)
|
|
1682
|
-
const wrapper = el.querySelector(
|
|
1682
|
+
const wrapper = el.querySelector('div[style]') as HTMLElement | null
|
|
1683
1683
|
// If wrapper exists, display should not be none
|
|
1684
|
-
if (wrapper) expect(wrapper.style.display).not.toBe(
|
|
1684
|
+
if (wrapper) expect(wrapper.style.display).not.toBe('none')
|
|
1685
1685
|
})
|
|
1686
1686
|
|
|
1687
|
-
test(
|
|
1687
|
+
test('toggles display:none when active changes', async () => {
|
|
1688
1688
|
const el = container()
|
|
1689
1689
|
const active = signal(true)
|
|
1690
|
-
mount(h(KeepAlive, { active }, h(
|
|
1690
|
+
mount(h(KeepAlive, { active }, h('span', { id: 'ka-toggle' }, 'x')), el)
|
|
1691
1691
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
1692
1692
|
// Find the container div that KeepAlive creates
|
|
1693
|
-
const containers = el.querySelectorAll(
|
|
1694
|
-
const keepAliveContainer = Array.from(containers).find((d) => d.querySelector(
|
|
1693
|
+
const containers = el.querySelectorAll('div')
|
|
1694
|
+
const keepAliveContainer = Array.from(containers).find((d) => d.querySelector('#ka-toggle')) as
|
|
1695
1695
|
| HTMLElement
|
|
1696
1696
|
| undefined
|
|
1697
1697
|
if (keepAliveContainer) {
|
|
1698
|
-
expect(keepAliveContainer.style.display).not.toBe(
|
|
1698
|
+
expect(keepAliveContainer.style.display).not.toBe('none')
|
|
1699
1699
|
active.set(false)
|
|
1700
|
-
expect(keepAliveContainer.style.display).toBe(
|
|
1700
|
+
expect(keepAliveContainer.style.display).toBe('none')
|
|
1701
1701
|
active.set(true)
|
|
1702
|
-
expect(keepAliveContainer.style.display).toBe(
|
|
1702
|
+
expect(keepAliveContainer.style.display).toBe('')
|
|
1703
1703
|
}
|
|
1704
1704
|
})
|
|
1705
1705
|
})
|
|
1706
1706
|
|
|
1707
1707
|
// ─── Hydration (extended coverage) ───────────────────────────────────────────
|
|
1708
1708
|
|
|
1709
|
-
describe(
|
|
1710
|
-
test(
|
|
1709
|
+
describe('hydrateRoot — extended', () => {
|
|
1710
|
+
test('hydrates Fragment children', async () => {
|
|
1711
1711
|
const el = container()
|
|
1712
|
-
el.innerHTML =
|
|
1713
|
-
const cleanup = hydrateRoot(el, h(Fragment, null, h(
|
|
1714
|
-
const spans = el.querySelectorAll(
|
|
1712
|
+
el.innerHTML = '<span>a</span><span>b</span>'
|
|
1713
|
+
const cleanup = hydrateRoot(el, h(Fragment, null, h('span', null, 'a'), h('span', null, 'b')))
|
|
1714
|
+
const spans = el.querySelectorAll('span')
|
|
1715
1715
|
expect(spans.length).toBe(2)
|
|
1716
|
-
expect(spans[0]?.textContent).toBe(
|
|
1717
|
-
expect(spans[1]?.textContent).toBe(
|
|
1716
|
+
expect(spans[0]?.textContent).toBe('a')
|
|
1717
|
+
expect(spans[1]?.textContent).toBe('b')
|
|
1718
1718
|
cleanup()
|
|
1719
1719
|
})
|
|
1720
1720
|
|
|
1721
|
-
test(
|
|
1721
|
+
test('hydrates array children', async () => {
|
|
1722
1722
|
const el = container()
|
|
1723
|
-
el.innerHTML =
|
|
1724
|
-
const cleanup = hydrateRoot(el, h(
|
|
1725
|
-
expect(el.querySelectorAll(
|
|
1723
|
+
el.innerHTML = '<div><span>x</span><span>y</span></div>'
|
|
1724
|
+
const cleanup = hydrateRoot(el, h('div', null, h('span', null, 'x'), h('span', null, 'y')))
|
|
1725
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
1726
1726
|
cleanup()
|
|
1727
1727
|
})
|
|
1728
1728
|
|
|
1729
|
-
test(
|
|
1729
|
+
test('hydrates null/false child — returns noop', async () => {
|
|
1730
1730
|
const el = container()
|
|
1731
|
-
el.innerHTML =
|
|
1732
|
-
const cleanup = hydrateRoot(el, h(
|
|
1733
|
-
expect(el.querySelector(
|
|
1731
|
+
el.innerHTML = '<div></div>'
|
|
1732
|
+
const cleanup = hydrateRoot(el, h('div', null, null, false))
|
|
1733
|
+
expect(el.querySelector('div')).not.toBeNull()
|
|
1734
1734
|
cleanup()
|
|
1735
1735
|
})
|
|
1736
1736
|
|
|
1737
|
-
test(
|
|
1737
|
+
test('hydrates reactive accessor returning null initially', async () => {
|
|
1738
1738
|
const el = container()
|
|
1739
|
-
el.innerHTML =
|
|
1739
|
+
el.innerHTML = '<div></div>'
|
|
1740
1740
|
const show = signal<string | null>(null)
|
|
1741
1741
|
const cleanup = hydrateRoot(
|
|
1742
1742
|
el,
|
|
1743
|
-
h(
|
|
1743
|
+
h('div', null, () => show()),
|
|
1744
1744
|
)
|
|
1745
1745
|
// Initially null — a comment marker is inserted
|
|
1746
|
-
show.set(
|
|
1746
|
+
show.set('hello')
|
|
1747
1747
|
// After update, the text should appear
|
|
1748
|
-
expect(el.textContent).toContain(
|
|
1748
|
+
expect(el.textContent).toContain('hello')
|
|
1749
1749
|
cleanup()
|
|
1750
1750
|
})
|
|
1751
1751
|
|
|
1752
|
-
test(
|
|
1752
|
+
test('hydrates reactive text that mismatches DOM node type', async () => {
|
|
1753
1753
|
const el = container()
|
|
1754
|
-
el.innerHTML =
|
|
1755
|
-
const text = signal(
|
|
1754
|
+
el.innerHTML = '<div><span>wrong</span></div>'
|
|
1755
|
+
const text = signal('hello')
|
|
1756
1756
|
// Reactive text expects a TextNode but finds a SPAN — should fall back
|
|
1757
1757
|
const cleanup = hydrateRoot(
|
|
1758
1758
|
el,
|
|
1759
|
-
h(
|
|
1759
|
+
h('div', null, () => text()),
|
|
1760
1760
|
)
|
|
1761
1761
|
cleanup()
|
|
1762
1762
|
})
|
|
1763
1763
|
|
|
1764
|
-
test(
|
|
1764
|
+
test('hydrates reactive VNode (complex initial value)', async () => {
|
|
1765
1765
|
const el = container()
|
|
1766
|
-
el.innerHTML =
|
|
1767
|
-
const content = signal<VNodeChild>(h(
|
|
1768
|
-
const cleanup = hydrateRoot(el, h(
|
|
1766
|
+
el.innerHTML = '<div><p>old</p></div>'
|
|
1767
|
+
const content = signal<VNodeChild>(h('p', null, 'old'))
|
|
1768
|
+
const cleanup = hydrateRoot(el, h('div', null, (() => content()) as unknown as VNodeChild))
|
|
1769
1769
|
cleanup()
|
|
1770
1770
|
})
|
|
1771
1771
|
|
|
1772
|
-
test(
|
|
1772
|
+
test('hydrates static text node', async () => {
|
|
1773
1773
|
const el = container()
|
|
1774
|
-
el.innerHTML =
|
|
1775
|
-
const cleanup = hydrateRoot(el,
|
|
1776
|
-
expect(el.textContent).toContain(
|
|
1774
|
+
el.innerHTML = 'just text'
|
|
1775
|
+
const cleanup = hydrateRoot(el, 'just text')
|
|
1776
|
+
expect(el.textContent).toContain('just text')
|
|
1777
1777
|
cleanup()
|
|
1778
1778
|
})
|
|
1779
1779
|
|
|
1780
|
-
test(
|
|
1780
|
+
test('hydrates number as text', async () => {
|
|
1781
1781
|
const el = container()
|
|
1782
|
-
el.innerHTML =
|
|
1782
|
+
el.innerHTML = '42'
|
|
1783
1783
|
const cleanup = hydrateRoot(el, 42)
|
|
1784
|
-
expect(el.textContent).toContain(
|
|
1784
|
+
expect(el.textContent).toContain('42')
|
|
1785
1785
|
cleanup()
|
|
1786
1786
|
})
|
|
1787
1787
|
|
|
1788
|
-
test(
|
|
1788
|
+
test('hydration tag mismatch falls back to mount', async () => {
|
|
1789
1789
|
const el = container()
|
|
1790
|
-
el.innerHTML =
|
|
1790
|
+
el.innerHTML = '<div>wrong</div>'
|
|
1791
1791
|
// Expect span but find div — should fall back
|
|
1792
|
-
const cleanup = hydrateRoot(el, h(
|
|
1792
|
+
const cleanup = hydrateRoot(el, h('span', null, 'right'))
|
|
1793
1793
|
// The span should have been mounted via fallback
|
|
1794
1794
|
cleanup()
|
|
1795
1795
|
})
|
|
1796
1796
|
|
|
1797
|
-
test(
|
|
1797
|
+
test('hydrates element with ref', async () => {
|
|
1798
1798
|
const el = container()
|
|
1799
|
-
el.innerHTML =
|
|
1799
|
+
el.innerHTML = '<button>click</button>'
|
|
1800
1800
|
const ref = createRef<HTMLButtonElement>()
|
|
1801
|
-
const cleanup = hydrateRoot(el, h(
|
|
1801
|
+
const cleanup = hydrateRoot(el, h('button', { ref }, 'click'))
|
|
1802
1802
|
expect(ref.current).not.toBeNull()
|
|
1803
|
-
expect(ref.current?.tagName).toBe(
|
|
1803
|
+
expect(ref.current?.tagName).toBe('BUTTON')
|
|
1804
1804
|
cleanup()
|
|
1805
1805
|
expect(ref.current).toBeNull()
|
|
1806
1806
|
})
|
|
1807
1807
|
|
|
1808
|
-
test(
|
|
1808
|
+
test('hydrates Portal — always remounts', async () => {
|
|
1809
1809
|
const el = container()
|
|
1810
1810
|
const target = container()
|
|
1811
|
-
el.innerHTML =
|
|
1812
|
-
const cleanup = hydrateRoot(el, Portal({ target, children: h(
|
|
1813
|
-
expect(target.querySelector(
|
|
1811
|
+
el.innerHTML = ''
|
|
1812
|
+
const cleanup = hydrateRoot(el, Portal({ target, children: h('span', null, 'portaled') }))
|
|
1813
|
+
expect(target.querySelector('span')?.textContent).toBe('portaled')
|
|
1814
1814
|
cleanup()
|
|
1815
1815
|
})
|
|
1816
1816
|
|
|
1817
|
-
test(
|
|
1817
|
+
test('hydrates component with children prop', async () => {
|
|
1818
1818
|
const el = container()
|
|
1819
|
-
el.innerHTML =
|
|
1819
|
+
el.innerHTML = '<div><p>child content</p></div>'
|
|
1820
1820
|
const Wrapper = defineComponent((props: { children?: VNodeChild }) =>
|
|
1821
|
-
h(
|
|
1821
|
+
h('div', null, props.children),
|
|
1822
1822
|
)
|
|
1823
|
-
const cleanup = hydrateRoot(el, h(Wrapper, null, h(
|
|
1824
|
-
expect(el.querySelector(
|
|
1823
|
+
const cleanup = hydrateRoot(el, h(Wrapper, null, h('p', null, 'child content')))
|
|
1824
|
+
expect(el.querySelector('p')?.textContent).toBe('child content')
|
|
1825
1825
|
cleanup()
|
|
1826
1826
|
})
|
|
1827
1827
|
|
|
1828
|
-
test(
|
|
1828
|
+
test('hydrates component that throws — error handled gracefully', async () => {
|
|
1829
1829
|
const el = container()
|
|
1830
|
-
el.innerHTML =
|
|
1830
|
+
el.innerHTML = '<p>content</p>'
|
|
1831
1831
|
const Broken = defineComponent((): never => {
|
|
1832
|
-
throw new Error(
|
|
1832
|
+
throw new Error('hydration boom')
|
|
1833
1833
|
})
|
|
1834
1834
|
// Should not throw — error is caught internally
|
|
1835
1835
|
const cleanup = hydrateRoot(el, h(Broken, null))
|
|
1836
1836
|
cleanup()
|
|
1837
1837
|
})
|
|
1838
1838
|
|
|
1839
|
-
test(
|
|
1839
|
+
test('hydrates with For — fresh mount fallback (no markers)', async () => {
|
|
1840
1840
|
const el = container()
|
|
1841
|
-
el.innerHTML =
|
|
1842
|
-
const items = signal([{ id: 1, label:
|
|
1841
|
+
el.innerHTML = '<ul></ul>'
|
|
1842
|
+
const items = signal([{ id: 1, label: 'a' }])
|
|
1843
1843
|
const cleanup = hydrateRoot(
|
|
1844
1844
|
el,
|
|
1845
1845
|
h(
|
|
1846
|
-
|
|
1846
|
+
'ul',
|
|
1847
1847
|
null,
|
|
1848
1848
|
For({
|
|
1849
1849
|
each: items,
|
|
1850
1850
|
by: (r: { id: number }) => r.id,
|
|
1851
|
-
children: (r: { id: number; label: string }) => h(
|
|
1851
|
+
children: (r: { id: number; label: string }) => h('li', null, r.label),
|
|
1852
1852
|
}),
|
|
1853
1853
|
),
|
|
1854
1854
|
)
|
|
1855
1855
|
cleanup()
|
|
1856
1856
|
})
|
|
1857
1857
|
|
|
1858
|
-
test(
|
|
1858
|
+
test('hydrates with For — SSR markers present', async () => {
|
|
1859
1859
|
const el = container()
|
|
1860
|
-
el.innerHTML =
|
|
1861
|
-
const items = signal([{ id: 1, label:
|
|
1860
|
+
el.innerHTML = '<!--pyreon-for--><li>a</li><!--/pyreon-for-->'
|
|
1861
|
+
const items = signal([{ id: 1, label: 'a' }])
|
|
1862
1862
|
const cleanup = hydrateRoot(
|
|
1863
1863
|
el,
|
|
1864
1864
|
For({
|
|
1865
1865
|
each: items,
|
|
1866
1866
|
by: (r: { id: number }) => r.id,
|
|
1867
|
-
children: (r: { id: number; label: string }) => h(
|
|
1867
|
+
children: (r: { id: number; label: string }) => h('li', null, r.label),
|
|
1868
1868
|
}),
|
|
1869
1869
|
)
|
|
1870
1870
|
cleanup()
|
|
1871
1871
|
})
|
|
1872
1872
|
|
|
1873
|
-
test(
|
|
1873
|
+
test('hydration skips comment and whitespace text nodes', async () => {
|
|
1874
1874
|
const el = container()
|
|
1875
1875
|
// Simulate SSR output with comments and whitespace
|
|
1876
|
-
el.innerHTML =
|
|
1877
|
-
const cleanup = hydrateRoot(el, h(
|
|
1878
|
-
expect(el.querySelector(
|
|
1876
|
+
el.innerHTML = '<!-- comment --> <p>real</p>'
|
|
1877
|
+
const cleanup = hydrateRoot(el, h('p', null, 'real'))
|
|
1878
|
+
expect(el.querySelector('p')?.textContent).toBe('real')
|
|
1879
1879
|
cleanup()
|
|
1880
1880
|
})
|
|
1881
1881
|
|
|
1882
|
-
test(
|
|
1882
|
+
test('hydrates with missing DOM node (null domNode)', async () => {
|
|
1883
1883
|
const el = container()
|
|
1884
|
-
el.innerHTML =
|
|
1884
|
+
el.innerHTML = ''
|
|
1885
1885
|
// VNode expects content but DOM is empty — should fall back
|
|
1886
|
-
const cleanup = hydrateRoot(el, h(
|
|
1886
|
+
const cleanup = hydrateRoot(el, h('div', null, 'content'))
|
|
1887
1887
|
cleanup()
|
|
1888
1888
|
})
|
|
1889
1889
|
|
|
1890
|
-
test(
|
|
1890
|
+
test('hydrates reactive accessor returning VNode with no domNode', async () => {
|
|
1891
1891
|
const el = container()
|
|
1892
|
-
el.innerHTML =
|
|
1893
|
-
const content = signal<VNodeChild>(h(
|
|
1892
|
+
el.innerHTML = ''
|
|
1893
|
+
const content = signal<VNodeChild>(h('p', null, 'dynamic'))
|
|
1894
1894
|
const cleanup = hydrateRoot(el, (() => content()) as unknown as VNodeChild)
|
|
1895
1895
|
cleanup()
|
|
1896
1896
|
})
|
|
1897
1897
|
|
|
1898
|
-
test(
|
|
1898
|
+
test('hydrates component with onMount hooks', async () => {
|
|
1899
1899
|
const el = container()
|
|
1900
|
-
el.innerHTML =
|
|
1900
|
+
el.innerHTML = '<span>mounted</span>'
|
|
1901
1901
|
let mountCalled = false
|
|
1902
1902
|
const Comp = defineComponent(() => {
|
|
1903
1903
|
onMount(() => {
|
|
1904
1904
|
mountCalled = true
|
|
1905
1905
|
})
|
|
1906
|
-
return h(
|
|
1906
|
+
return h('span', null, 'mounted')
|
|
1907
1907
|
})
|
|
1908
1908
|
const cleanup = hydrateRoot(el, h(Comp, null))
|
|
1909
1909
|
expect(mountCalled).toBe(true)
|
|
1910
1910
|
cleanup()
|
|
1911
1911
|
})
|
|
1912
1912
|
|
|
1913
|
-
test(
|
|
1913
|
+
test('hydrates text mismatch for static string — falls back', async () => {
|
|
1914
1914
|
const el = container()
|
|
1915
1915
|
// Put an element where text is expected
|
|
1916
|
-
el.innerHTML =
|
|
1917
|
-
const cleanup = hydrateRoot(el,
|
|
1916
|
+
el.innerHTML = '<span>not text</span>'
|
|
1917
|
+
const cleanup = hydrateRoot(el, 'plain text')
|
|
1918
1918
|
cleanup()
|
|
1919
1919
|
})
|
|
1920
1920
|
})
|
|
1921
1921
|
|
|
1922
1922
|
// ─── mountFor — additional edge cases ────────────────────────────────────────
|
|
1923
1923
|
|
|
1924
|
-
describe(
|
|
1924
|
+
describe('mountFor — edge cases', () => {
|
|
1925
1925
|
type Item = { id: number; label: string }
|
|
1926
1926
|
|
|
1927
1927
|
function mountForList(el: HTMLElement, items: ReturnType<typeof signal<Item[]>>) {
|
|
1928
1928
|
mount(
|
|
1929
1929
|
h(
|
|
1930
|
-
|
|
1930
|
+
'ul',
|
|
1931
1931
|
null,
|
|
1932
1932
|
For({
|
|
1933
1933
|
each: items,
|
|
1934
1934
|
by: (r) => r.id,
|
|
1935
|
-
children: (r) => h(
|
|
1935
|
+
children: (r) => h('li', { key: r.id }, r.label),
|
|
1936
1936
|
}),
|
|
1937
1937
|
),
|
|
1938
1938
|
el,
|
|
1939
1939
|
)
|
|
1940
1940
|
}
|
|
1941
1941
|
|
|
1942
|
-
test(
|
|
1942
|
+
test('empty initial → add items (fresh render path)', () => {
|
|
1943
1943
|
const el = container()
|
|
1944
1944
|
const items = signal<Item[]>([])
|
|
1945
1945
|
mountForList(el, items)
|
|
1946
|
-
expect(el.querySelectorAll(
|
|
1946
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
1947
1947
|
items.set([
|
|
1948
|
-
{ id: 1, label:
|
|
1949
|
-
{ id: 2, label:
|
|
1948
|
+
{ id: 1, label: 'a' },
|
|
1949
|
+
{ id: 2, label: 'b' },
|
|
1950
1950
|
])
|
|
1951
|
-
expect(el.querySelectorAll(
|
|
1952
|
-
expect(el.querySelectorAll(
|
|
1951
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
1952
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('a')
|
|
1953
1953
|
})
|
|
1954
1954
|
|
|
1955
|
-
test(
|
|
1955
|
+
test('clear then add uses fresh render path', () => {
|
|
1956
1956
|
const el = container()
|
|
1957
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
1957
|
+
const items = signal<Item[]>([{ id: 1, label: 'x' }])
|
|
1958
1958
|
mountForList(el, items)
|
|
1959
1959
|
items.set([])
|
|
1960
|
-
expect(el.querySelectorAll(
|
|
1960
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
1961
1961
|
items.set([
|
|
1962
|
-
{ id: 2, label:
|
|
1963
|
-
{ id: 3, label:
|
|
1962
|
+
{ id: 2, label: 'y' },
|
|
1963
|
+
{ id: 3, label: 'z' },
|
|
1964
1964
|
])
|
|
1965
|
-
expect(el.querySelectorAll(
|
|
1966
|
-
expect(el.querySelectorAll(
|
|
1965
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
1966
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('y')
|
|
1967
1967
|
})
|
|
1968
1968
|
|
|
1969
|
-
test(
|
|
1969
|
+
test('clear path with parent-swap optimization', () => {
|
|
1970
1970
|
// When the For's markers are the first and last children of a parent,
|
|
1971
1971
|
// the clear path uses parent-swap for O(1) clear.
|
|
1972
1972
|
const el = container()
|
|
1973
1973
|
const items = signal<Item[]>([
|
|
1974
|
-
{ id: 1, label:
|
|
1975
|
-
{ id: 2, label:
|
|
1976
|
-
{ id: 3, label:
|
|
1974
|
+
{ id: 1, label: 'a' },
|
|
1975
|
+
{ id: 2, label: 'b' },
|
|
1976
|
+
{ id: 3, label: 'c' },
|
|
1977
1977
|
])
|
|
1978
1978
|
// Mount directly in the ul so markers are first/last children
|
|
1979
1979
|
mount(
|
|
1980
1980
|
h(
|
|
1981
|
-
|
|
1981
|
+
'ul',
|
|
1982
1982
|
null,
|
|
1983
1983
|
For({
|
|
1984
1984
|
each: items,
|
|
1985
1985
|
by: (r) => r.id,
|
|
1986
|
-
children: (r) => h(
|
|
1986
|
+
children: (r) => h('li', { key: r.id }, r.label),
|
|
1987
1987
|
}),
|
|
1988
1988
|
),
|
|
1989
1989
|
el,
|
|
1990
1990
|
)
|
|
1991
|
-
expect(el.querySelectorAll(
|
|
1991
|
+
expect(el.querySelectorAll('li').length).toBe(3)
|
|
1992
1992
|
items.set([])
|
|
1993
|
-
expect(el.querySelectorAll(
|
|
1993
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
1994
1994
|
})
|
|
1995
1995
|
|
|
1996
|
-
test(
|
|
1996
|
+
test('clear path without parent-swap (markers not first/last)', () => {
|
|
1997
1997
|
const el = container()
|
|
1998
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
1998
|
+
const items = signal<Item[]>([{ id: 1, label: 'a' }])
|
|
1999
1999
|
// Mount with extra siblings so markers are not first/last
|
|
2000
2000
|
mount(
|
|
2001
2001
|
h(
|
|
2002
|
-
|
|
2002
|
+
'div',
|
|
2003
2003
|
null,
|
|
2004
|
-
h(
|
|
2005
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
2006
|
-
h(
|
|
2004
|
+
h('span', null, 'before'),
|
|
2005
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
2006
|
+
h('span', null, 'after'),
|
|
2007
2007
|
),
|
|
2008
2008
|
el,
|
|
2009
2009
|
)
|
|
2010
|
-
expect(el.querySelectorAll(
|
|
2010
|
+
expect(el.querySelectorAll('li').length).toBe(1)
|
|
2011
2011
|
items.set([])
|
|
2012
|
-
expect(el.querySelectorAll(
|
|
2012
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
2013
2013
|
// The before/after spans should still be present
|
|
2014
|
-
expect(el.querySelectorAll(
|
|
2014
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
2015
2015
|
})
|
|
2016
2016
|
|
|
2017
|
-
test(
|
|
2017
|
+
test('replace-all with parent-swap optimization', () => {
|
|
2018
2018
|
const el = container()
|
|
2019
2019
|
const items = signal<Item[]>([
|
|
2020
|
-
{ id: 1, label:
|
|
2021
|
-
{ id: 2, label:
|
|
2020
|
+
{ id: 1, label: 'old1' },
|
|
2021
|
+
{ id: 2, label: 'old2' },
|
|
2022
2022
|
])
|
|
2023
2023
|
mount(
|
|
2024
2024
|
h(
|
|
2025
|
-
|
|
2025
|
+
'ul',
|
|
2026
2026
|
null,
|
|
2027
2027
|
For({
|
|
2028
2028
|
each: items,
|
|
2029
2029
|
by: (r) => r.id,
|
|
2030
|
-
children: (r) => h(
|
|
2030
|
+
children: (r) => h('li', { key: r.id }, r.label),
|
|
2031
2031
|
}),
|
|
2032
2032
|
),
|
|
2033
2033
|
el,
|
|
2034
2034
|
)
|
|
2035
2035
|
// Replace with completely new keys
|
|
2036
2036
|
items.set([
|
|
2037
|
-
{ id: 10, label:
|
|
2038
|
-
{ id: 11, label:
|
|
2037
|
+
{ id: 10, label: 'new1' },
|
|
2038
|
+
{ id: 11, label: 'new2' },
|
|
2039
2039
|
])
|
|
2040
|
-
expect(el.querySelectorAll(
|
|
2041
|
-
expect(el.querySelectorAll(
|
|
2040
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
2041
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('new1')
|
|
2042
2042
|
})
|
|
2043
2043
|
|
|
2044
|
-
test(
|
|
2044
|
+
test('replace-all without parent-swap (extra siblings)', () => {
|
|
2045
2045
|
const el = container()
|
|
2046
|
-
const items = signal<Item[]>([{ id: 1, label:
|
|
2046
|
+
const items = signal<Item[]>([{ id: 1, label: 'old' }])
|
|
2047
2047
|
mount(
|
|
2048
2048
|
h(
|
|
2049
|
-
|
|
2049
|
+
'div',
|
|
2050
2050
|
null,
|
|
2051
|
-
h(
|
|
2052
|
-
For({ each: items, by: (r) => r.id, children: (r) => h(
|
|
2053
|
-
h(
|
|
2051
|
+
h('span', null, 'before'),
|
|
2052
|
+
For({ each: items, by: (r) => r.id, children: (r) => h('li', { key: r.id }, r.label) }),
|
|
2053
|
+
h('span', null, 'after'),
|
|
2054
2054
|
),
|
|
2055
2055
|
el,
|
|
2056
2056
|
)
|
|
2057
|
-
items.set([{ id: 10, label:
|
|
2058
|
-
expect(el.querySelectorAll(
|
|
2059
|
-
expect(el.querySelectorAll(
|
|
2060
|
-
expect(el.querySelectorAll(
|
|
2057
|
+
items.set([{ id: 10, label: 'new' }])
|
|
2058
|
+
expect(el.querySelectorAll('li').length).toBe(1)
|
|
2059
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('new')
|
|
2060
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
2061
2061
|
})
|
|
2062
2062
|
|
|
2063
|
-
test(
|
|
2063
|
+
test('remove stale entries', () => {
|
|
2064
2064
|
const el = container()
|
|
2065
2065
|
const items = signal<Item[]>([
|
|
2066
|
-
{ id: 1, label:
|
|
2067
|
-
{ id: 2, label:
|
|
2068
|
-
{ id: 3, label:
|
|
2066
|
+
{ id: 1, label: 'a' },
|
|
2067
|
+
{ id: 2, label: 'b' },
|
|
2068
|
+
{ id: 3, label: 'c' },
|
|
2069
2069
|
])
|
|
2070
2070
|
mountForList(el, items)
|
|
2071
2071
|
// Remove middle item — hits stale entry removal path
|
|
2072
2072
|
items.set([
|
|
2073
|
-
{ id: 1, label:
|
|
2074
|
-
{ id: 3, label:
|
|
2073
|
+
{ id: 1, label: 'a' },
|
|
2074
|
+
{ id: 3, label: 'c' },
|
|
2075
2075
|
])
|
|
2076
|
-
expect(el.querySelectorAll(
|
|
2077
|
-
expect(el.querySelectorAll(
|
|
2078
|
-
expect(el.querySelectorAll(
|
|
2076
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
2077
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('a')
|
|
2078
|
+
expect(el.querySelectorAll('li')[1]?.textContent).toBe('c')
|
|
2079
2079
|
})
|
|
2080
2080
|
|
|
2081
|
-
test(
|
|
2081
|
+
test('LIS fallback for complex reorder (>8 diffs, same length)', () => {
|
|
2082
2082
|
const el = container()
|
|
2083
2083
|
// Create 15 items, then reverse all — forces > SMALL_K diffs and LIS path
|
|
2084
2084
|
const initial = Array.from({ length: 15 }, (_, i) => ({
|
|
@@ -2089,13 +2089,13 @@ describe("mountFor — edge cases", () => {
|
|
|
2089
2089
|
mountForList(el, items)
|
|
2090
2090
|
// Reverse all items: 15 diffs > SMALL_K (8)
|
|
2091
2091
|
items.set([...initial].reverse())
|
|
2092
|
-
const lis = el.querySelectorAll(
|
|
2092
|
+
const lis = el.querySelectorAll('li')
|
|
2093
2093
|
expect(lis.length).toBe(15)
|
|
2094
|
-
expect(lis[0]?.textContent).toBe(
|
|
2095
|
-
expect(lis[14]?.textContent).toBe(
|
|
2094
|
+
expect(lis[0]?.textContent).toBe('o') // last letter reversed
|
|
2095
|
+
expect(lis[14]?.textContent).toBe('a')
|
|
2096
2096
|
})
|
|
2097
2097
|
|
|
2098
|
-
test(
|
|
2098
|
+
test('LIS fallback for reorder with different length', () => {
|
|
2099
2099
|
const el = container()
|
|
2100
2100
|
const initial = Array.from({ length: 10 }, (_, i) => ({
|
|
2101
2101
|
id: i + 1,
|
|
@@ -2105,268 +2105,268 @@ describe("mountFor — edge cases", () => {
|
|
|
2105
2105
|
mountForList(el, items)
|
|
2106
2106
|
// Reverse and add one — different length triggers LIS
|
|
2107
2107
|
const reversed = [...initial].reverse()
|
|
2108
|
-
reversed.push({ id: 99, label:
|
|
2108
|
+
reversed.push({ id: 99, label: 'z' })
|
|
2109
2109
|
items.set(reversed)
|
|
2110
|
-
const lis = el.querySelectorAll(
|
|
2110
|
+
const lis = el.querySelectorAll('li')
|
|
2111
2111
|
expect(lis.length).toBe(11)
|
|
2112
|
-
expect(lis[0]?.textContent).toBe(
|
|
2113
|
-
expect(lis[10]?.textContent).toBe(
|
|
2112
|
+
expect(lis[0]?.textContent).toBe('j')
|
|
2113
|
+
expect(lis[10]?.textContent).toBe('z')
|
|
2114
2114
|
})
|
|
2115
2115
|
|
|
2116
|
-
test(
|
|
2116
|
+
test('small-k reorder path (<=8 diffs, same length)', () => {
|
|
2117
2117
|
const el = container()
|
|
2118
2118
|
const items = signal<Item[]>([
|
|
2119
|
-
{ id: 1, label:
|
|
2120
|
-
{ id: 2, label:
|
|
2121
|
-
{ id: 3, label:
|
|
2122
|
-
{ id: 4, label:
|
|
2119
|
+
{ id: 1, label: 'a' },
|
|
2120
|
+
{ id: 2, label: 'b' },
|
|
2121
|
+
{ id: 3, label: 'c' },
|
|
2122
|
+
{ id: 4, label: 'd' },
|
|
2123
2123
|
])
|
|
2124
2124
|
mountForList(el, items)
|
|
2125
2125
|
// Swap positions 1 and 2 — only 2 diffs < SMALL_K
|
|
2126
2126
|
items.set([
|
|
2127
|
-
{ id: 1, label:
|
|
2128
|
-
{ id: 3, label:
|
|
2129
|
-
{ id: 2, label:
|
|
2130
|
-
{ id: 4, label:
|
|
2127
|
+
{ id: 1, label: 'a' },
|
|
2128
|
+
{ id: 3, label: 'c' },
|
|
2129
|
+
{ id: 2, label: 'b' },
|
|
2130
|
+
{ id: 4, label: 'd' },
|
|
2131
2131
|
])
|
|
2132
|
-
const lis = el.querySelectorAll(
|
|
2133
|
-
expect(lis[1]?.textContent).toBe(
|
|
2134
|
-
expect(lis[2]?.textContent).toBe(
|
|
2132
|
+
const lis = el.querySelectorAll('li')
|
|
2133
|
+
expect(lis[1]?.textContent).toBe('c')
|
|
2134
|
+
expect(lis[2]?.textContent).toBe('b')
|
|
2135
2135
|
})
|
|
2136
2136
|
|
|
2137
|
-
test(
|
|
2137
|
+
test('add and remove items simultaneously', () => {
|
|
2138
2138
|
const el = container()
|
|
2139
2139
|
const items = signal<Item[]>([
|
|
2140
|
-
{ id: 1, label:
|
|
2141
|
-
{ id: 2, label:
|
|
2142
|
-
{ id: 3, label:
|
|
2140
|
+
{ id: 1, label: 'a' },
|
|
2141
|
+
{ id: 2, label: 'b' },
|
|
2142
|
+
{ id: 3, label: 'c' },
|
|
2143
2143
|
])
|
|
2144
2144
|
mountForList(el, items)
|
|
2145
2145
|
// Remove 2, add 4 and 5
|
|
2146
2146
|
items.set([
|
|
2147
|
-
{ id: 1, label:
|
|
2148
|
-
{ id: 4, label:
|
|
2149
|
-
{ id: 3, label:
|
|
2150
|
-
{ id: 5, label:
|
|
2147
|
+
{ id: 1, label: 'a' },
|
|
2148
|
+
{ id: 4, label: 'd' },
|
|
2149
|
+
{ id: 3, label: 'c' },
|
|
2150
|
+
{ id: 5, label: 'e' },
|
|
2151
2151
|
])
|
|
2152
|
-
const lis = el.querySelectorAll(
|
|
2152
|
+
const lis = el.querySelectorAll('li')
|
|
2153
2153
|
expect(lis.length).toBe(4)
|
|
2154
|
-
expect(lis[0]?.textContent).toBe(
|
|
2154
|
+
expect(lis[0]?.textContent).toBe('a')
|
|
2155
2155
|
// Verify all expected items are present
|
|
2156
2156
|
const texts = Array.from(lis).map((li) => li.textContent)
|
|
2157
|
-
expect(texts).toContain(
|
|
2158
|
-
expect(texts).toContain(
|
|
2159
|
-
expect(texts).toContain(
|
|
2160
|
-
expect(texts).toContain(
|
|
2157
|
+
expect(texts).toContain('a')
|
|
2158
|
+
expect(texts).toContain('c')
|
|
2159
|
+
expect(texts).toContain('d')
|
|
2160
|
+
expect(texts).toContain('e')
|
|
2161
2161
|
})
|
|
2162
2162
|
|
|
2163
|
-
test(
|
|
2163
|
+
test('unmount For cleanup disposes all entries', () => {
|
|
2164
2164
|
const el = container()
|
|
2165
2165
|
const items = signal<Item[]>([
|
|
2166
|
-
{ id: 1, label:
|
|
2167
|
-
{ id: 2, label:
|
|
2166
|
+
{ id: 1, label: 'a' },
|
|
2167
|
+
{ id: 2, label: 'b' },
|
|
2168
2168
|
])
|
|
2169
2169
|
const unmount = mount(
|
|
2170
2170
|
h(
|
|
2171
|
-
|
|
2171
|
+
'ul',
|
|
2172
2172
|
null,
|
|
2173
2173
|
For({
|
|
2174
2174
|
each: items,
|
|
2175
2175
|
by: (r) => r.id,
|
|
2176
|
-
children: (r) => h(
|
|
2176
|
+
children: (r) => h('li', { key: r.id }, r.label),
|
|
2177
2177
|
}),
|
|
2178
2178
|
),
|
|
2179
2179
|
el,
|
|
2180
2180
|
)
|
|
2181
|
-
expect(el.querySelectorAll(
|
|
2181
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
2182
2182
|
unmount()
|
|
2183
|
-
expect(el.innerHTML).toBe(
|
|
2183
|
+
expect(el.innerHTML).toBe('')
|
|
2184
2184
|
})
|
|
2185
2185
|
})
|
|
2186
2186
|
|
|
2187
2187
|
// ─── mountKeyedList — additional coverage ────────────────────────────────────
|
|
2188
2188
|
|
|
2189
|
-
describe(
|
|
2190
|
-
test(
|
|
2189
|
+
describe('mountKeyedList — via reactive keyed array', () => {
|
|
2190
|
+
test('reactive accessor returning keyed VNode array uses mountKeyedList', () => {
|
|
2191
2191
|
const el = container()
|
|
2192
2192
|
const items = signal([
|
|
2193
|
-
{ id: 1, text:
|
|
2194
|
-
{ id: 2, text:
|
|
2193
|
+
{ id: 1, text: 'a' },
|
|
2194
|
+
{ id: 2, text: 'b' },
|
|
2195
2195
|
])
|
|
2196
2196
|
mount(
|
|
2197
|
-
h(
|
|
2197
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2198
2198
|
el,
|
|
2199
2199
|
)
|
|
2200
|
-
expect(el.querySelectorAll(
|
|
2201
|
-
expect(el.querySelectorAll(
|
|
2200
|
+
expect(el.querySelectorAll('li').length).toBe(2)
|
|
2201
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('a')
|
|
2202
2202
|
})
|
|
2203
2203
|
|
|
2204
|
-
test(
|
|
2204
|
+
test('mountKeyedList handles clear (empty array)', () => {
|
|
2205
2205
|
const el = container()
|
|
2206
2206
|
const items = signal([
|
|
2207
|
-
{ id: 1, text:
|
|
2208
|
-
{ id: 2, text:
|
|
2207
|
+
{ id: 1, text: 'a' },
|
|
2208
|
+
{ id: 2, text: 'b' },
|
|
2209
2209
|
])
|
|
2210
2210
|
mount(
|
|
2211
|
-
h(
|
|
2211
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2212
2212
|
el,
|
|
2213
2213
|
)
|
|
2214
2214
|
items.set([])
|
|
2215
|
-
expect(el.querySelectorAll(
|
|
2215
|
+
expect(el.querySelectorAll('li').length).toBe(0)
|
|
2216
2216
|
})
|
|
2217
2217
|
|
|
2218
|
-
test(
|
|
2218
|
+
test('mountKeyedList handles reorder', () => {
|
|
2219
2219
|
const el = container()
|
|
2220
2220
|
const items = signal([
|
|
2221
|
-
{ id: 1, text:
|
|
2222
|
-
{ id: 2, text:
|
|
2223
|
-
{ id: 3, text:
|
|
2221
|
+
{ id: 1, text: 'a' },
|
|
2222
|
+
{ id: 2, text: 'b' },
|
|
2223
|
+
{ id: 3, text: 'c' },
|
|
2224
2224
|
])
|
|
2225
2225
|
mount(
|
|
2226
|
-
h(
|
|
2226
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2227
2227
|
el,
|
|
2228
2228
|
)
|
|
2229
2229
|
items.set([
|
|
2230
|
-
{ id: 3, text:
|
|
2231
|
-
{ id: 1, text:
|
|
2232
|
-
{ id: 2, text:
|
|
2230
|
+
{ id: 3, text: 'c' },
|
|
2231
|
+
{ id: 1, text: 'a' },
|
|
2232
|
+
{ id: 2, text: 'b' },
|
|
2233
2233
|
])
|
|
2234
|
-
const lis = el.querySelectorAll(
|
|
2235
|
-
expect(lis[0]?.textContent).toBe(
|
|
2236
|
-
expect(lis[1]?.textContent).toBe(
|
|
2237
|
-
expect(lis[2]?.textContent).toBe(
|
|
2234
|
+
const lis = el.querySelectorAll('li')
|
|
2235
|
+
expect(lis[0]?.textContent).toBe('c')
|
|
2236
|
+
expect(lis[1]?.textContent).toBe('a')
|
|
2237
|
+
expect(lis[2]?.textContent).toBe('b')
|
|
2238
2238
|
})
|
|
2239
2239
|
|
|
2240
|
-
test(
|
|
2240
|
+
test('mountKeyedList removes stale entries', () => {
|
|
2241
2241
|
const el = container()
|
|
2242
2242
|
const items = signal([
|
|
2243
|
-
{ id: 1, text:
|
|
2244
|
-
{ id: 2, text:
|
|
2245
|
-
{ id: 3, text:
|
|
2243
|
+
{ id: 1, text: 'a' },
|
|
2244
|
+
{ id: 2, text: 'b' },
|
|
2245
|
+
{ id: 3, text: 'c' },
|
|
2246
2246
|
])
|
|
2247
2247
|
mount(
|
|
2248
|
-
h(
|
|
2248
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2249
2249
|
el,
|
|
2250
2250
|
)
|
|
2251
|
-
items.set([{ id: 2, text:
|
|
2252
|
-
expect(el.querySelectorAll(
|
|
2253
|
-
expect(el.querySelectorAll(
|
|
2251
|
+
items.set([{ id: 2, text: 'b' }])
|
|
2252
|
+
expect(el.querySelectorAll('li').length).toBe(1)
|
|
2253
|
+
expect(el.querySelectorAll('li')[0]?.textContent).toBe('b')
|
|
2254
2254
|
})
|
|
2255
2255
|
|
|
2256
|
-
test(
|
|
2256
|
+
test('mountKeyedList adds new entries', () => {
|
|
2257
2257
|
const el = container()
|
|
2258
|
-
const items = signal([{ id: 1, text:
|
|
2258
|
+
const items = signal([{ id: 1, text: 'a' }])
|
|
2259
2259
|
mount(
|
|
2260
|
-
h(
|
|
2260
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2261
2261
|
el,
|
|
2262
2262
|
)
|
|
2263
2263
|
items.set([
|
|
2264
|
-
{ id: 1, text:
|
|
2265
|
-
{ id: 2, text:
|
|
2266
|
-
{ id: 3, text:
|
|
2264
|
+
{ id: 1, text: 'a' },
|
|
2265
|
+
{ id: 2, text: 'b' },
|
|
2266
|
+
{ id: 3, text: 'c' },
|
|
2267
2267
|
])
|
|
2268
|
-
expect(el.querySelectorAll(
|
|
2268
|
+
expect(el.querySelectorAll('li').length).toBe(3)
|
|
2269
2269
|
})
|
|
2270
2270
|
|
|
2271
|
-
test(
|
|
2271
|
+
test('mountKeyedList cleanup disposes all entries', () => {
|
|
2272
2272
|
const el = container()
|
|
2273
2273
|
const items = signal([
|
|
2274
|
-
{ id: 1, text:
|
|
2275
|
-
{ id: 2, text:
|
|
2274
|
+
{ id: 1, text: 'a' },
|
|
2275
|
+
{ id: 2, text: 'b' },
|
|
2276
2276
|
])
|
|
2277
2277
|
const unmount = mount(
|
|
2278
|
-
h(
|
|
2278
|
+
h('ul', null, () => items().map((it) => h('li', { key: it.id }, it.text))),
|
|
2279
2279
|
el,
|
|
2280
2280
|
)
|
|
2281
2281
|
unmount()
|
|
2282
|
-
expect(el.innerHTML).toBe(
|
|
2282
|
+
expect(el.innerHTML).toBe('')
|
|
2283
2283
|
})
|
|
2284
2284
|
})
|
|
2285
2285
|
|
|
2286
2286
|
// ─── mountReactive — additional coverage ─────────────────────────────────────
|
|
2287
2287
|
|
|
2288
|
-
describe(
|
|
2289
|
-
test(
|
|
2288
|
+
describe('mountReactive — edge cases', () => {
|
|
2289
|
+
test('reactive accessor returning null then VNode', () => {
|
|
2290
2290
|
const el = container()
|
|
2291
2291
|
const show = signal(false)
|
|
2292
2292
|
mount(
|
|
2293
|
-
h(
|
|
2293
|
+
h('div', null, () => (show() ? h('span', null, 'yes') : null)),
|
|
2294
2294
|
el,
|
|
2295
2295
|
)
|
|
2296
|
-
expect(el.querySelector(
|
|
2296
|
+
expect(el.querySelector('span')).toBeNull()
|
|
2297
2297
|
show.set(true)
|
|
2298
|
-
expect(el.querySelector(
|
|
2298
|
+
expect(el.querySelector('span')?.textContent).toBe('yes')
|
|
2299
2299
|
})
|
|
2300
2300
|
|
|
2301
|
-
test(
|
|
2301
|
+
test('reactive accessor returning false', () => {
|
|
2302
2302
|
const el = container()
|
|
2303
2303
|
const show = signal(false)
|
|
2304
2304
|
mount(
|
|
2305
|
-
h(
|
|
2305
|
+
h('div', null, () => (show() ? 'visible' : false)),
|
|
2306
2306
|
el,
|
|
2307
2307
|
)
|
|
2308
|
-
expect(el.querySelector(
|
|
2308
|
+
expect(el.querySelector('div')?.textContent).toBe('')
|
|
2309
2309
|
show.set(true)
|
|
2310
|
-
expect(el.querySelector(
|
|
2310
|
+
expect(el.querySelector('div')?.textContent).toBe('visible')
|
|
2311
2311
|
})
|
|
2312
2312
|
|
|
2313
|
-
test(
|
|
2313
|
+
test('reactive text fast path — null/undefined fallback', () => {
|
|
2314
2314
|
const el = container()
|
|
2315
|
-
const text = signal<string | null>(
|
|
2315
|
+
const text = signal<string | null>('hello')
|
|
2316
2316
|
mount(
|
|
2317
|
-
h(
|
|
2317
|
+
h('div', null, () => text()),
|
|
2318
2318
|
el,
|
|
2319
2319
|
)
|
|
2320
|
-
expect(el.querySelector(
|
|
2320
|
+
expect(el.querySelector('div')?.textContent).toBe('hello')
|
|
2321
2321
|
text.set(null)
|
|
2322
|
-
expect(el.querySelector(
|
|
2322
|
+
expect(el.querySelector('div')?.textContent).toBe('')
|
|
2323
2323
|
})
|
|
2324
2324
|
|
|
2325
|
-
test(
|
|
2325
|
+
test('reactive boolean text fast path', () => {
|
|
2326
2326
|
const el = container()
|
|
2327
2327
|
const val = signal(true)
|
|
2328
2328
|
mount(
|
|
2329
|
-
h(
|
|
2329
|
+
h('div', null, () => val()),
|
|
2330
2330
|
el,
|
|
2331
2331
|
)
|
|
2332
|
-
expect(el.querySelector(
|
|
2332
|
+
expect(el.querySelector('div')?.textContent).toBe('true')
|
|
2333
2333
|
val.set(false)
|
|
2334
|
-
expect(el.querySelector(
|
|
2334
|
+
expect(el.querySelector('div')?.textContent).toBe('')
|
|
2335
2335
|
})
|
|
2336
2336
|
|
|
2337
|
-
test(
|
|
2337
|
+
test('mountReactive cleanup when anchor has no parent', () => {
|
|
2338
2338
|
const el = container()
|
|
2339
2339
|
const show = signal(true)
|
|
2340
2340
|
const unmount = mount(
|
|
2341
|
-
h(
|
|
2341
|
+
h('div', null, () => (show() ? h('span', null, 'content') : null)),
|
|
2342
2342
|
el,
|
|
2343
2343
|
)
|
|
2344
2344
|
unmount()
|
|
2345
2345
|
// Should not throw even though marker may be detached
|
|
2346
|
-
expect(el.innerHTML).toBe(
|
|
2346
|
+
expect(el.innerHTML).toBe('')
|
|
2347
2347
|
})
|
|
2348
2348
|
})
|
|
2349
2349
|
|
|
2350
2350
|
// ─── mount.ts — component branches ──────────────────────────────────────────
|
|
2351
2351
|
|
|
2352
|
-
describe(
|
|
2353
|
-
test(
|
|
2352
|
+
describe('mount — component branches', () => {
|
|
2353
|
+
test('component returning Fragment', () => {
|
|
2354
2354
|
const el = container()
|
|
2355
2355
|
const FragComp = defineComponent(() =>
|
|
2356
|
-
h(Fragment, null, h(
|
|
2356
|
+
h(Fragment, null, h('span', null, 'a'), h('span', null, 'b')),
|
|
2357
2357
|
)
|
|
2358
2358
|
mount(h(FragComp, null), el)
|
|
2359
|
-
expect(el.querySelectorAll(
|
|
2359
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
2360
2360
|
})
|
|
2361
2361
|
|
|
2362
|
-
test(
|
|
2362
|
+
test('component with onMount returning cleanup', async () => {
|
|
2363
2363
|
const el = container()
|
|
2364
2364
|
let cleaned = false
|
|
2365
2365
|
const Comp = defineComponent(() => {
|
|
2366
2366
|
onMount(() => () => {
|
|
2367
2367
|
cleaned = true
|
|
2368
2368
|
})
|
|
2369
|
-
return h(
|
|
2369
|
+
return h('div', null, 'with-cleanup')
|
|
2370
2370
|
})
|
|
2371
2371
|
const unmount = mount(h(Comp, null), el)
|
|
2372
2372
|
expect(cleaned).toBe(false)
|
|
@@ -2374,14 +2374,14 @@ describe("mount — component branches", () => {
|
|
|
2374
2374
|
expect(cleaned).toBe(true)
|
|
2375
2375
|
})
|
|
2376
2376
|
|
|
2377
|
-
test(
|
|
2377
|
+
test('component with onUnmount hook', async () => {
|
|
2378
2378
|
const el = container()
|
|
2379
2379
|
let unmounted = false
|
|
2380
2380
|
const Comp = defineComponent(() => {
|
|
2381
2381
|
onUnmount(() => {
|
|
2382
2382
|
unmounted = true
|
|
2383
2383
|
})
|
|
2384
|
-
return h(
|
|
2384
|
+
return h('div', null, 'unmount-test')
|
|
2385
2385
|
})
|
|
2386
2386
|
const unmount = mount(h(Comp, null), el)
|
|
2387
2387
|
expect(unmounted).toBe(false)
|
|
@@ -2389,7 +2389,7 @@ describe("mount — component branches", () => {
|
|
|
2389
2389
|
expect(unmounted).toBe(true)
|
|
2390
2390
|
})
|
|
2391
2391
|
|
|
2392
|
-
test(
|
|
2392
|
+
test('component with onUpdate hook', async () => {
|
|
2393
2393
|
const el = container()
|
|
2394
2394
|
const Comp = defineComponent(() => {
|
|
2395
2395
|
const count = signal(0)
|
|
@@ -2397,187 +2397,187 @@ describe("mount — component branches", () => {
|
|
|
2397
2397
|
/* update tracked */
|
|
2398
2398
|
})
|
|
2399
2399
|
return h(
|
|
2400
|
-
|
|
2400
|
+
'div',
|
|
2401
2401
|
null,
|
|
2402
|
-
h(
|
|
2403
|
-
h(
|
|
2402
|
+
h('span', null, () => String(count())),
|
|
2403
|
+
h('button', { onClick: () => count.update((n: number) => n + 1) }, '+'),
|
|
2404
2404
|
)
|
|
2405
2405
|
})
|
|
2406
2406
|
mount(h(Comp, null), el)
|
|
2407
2407
|
// Click to trigger update
|
|
2408
|
-
el.querySelector(
|
|
2408
|
+
el.querySelector('button')?.click()
|
|
2409
2409
|
// onUpdate fires via microtask
|
|
2410
2410
|
})
|
|
2411
2411
|
|
|
2412
|
-
test(
|
|
2412
|
+
test('component children merge into props.children', () => {
|
|
2413
2413
|
const el = container()
|
|
2414
2414
|
const Parent = defineComponent((props: { children?: VNodeChild }) =>
|
|
2415
|
-
h(
|
|
2415
|
+
h('div', { id: 'parent' }, props.children),
|
|
2416
2416
|
)
|
|
2417
|
-
mount(h(Parent, null, h(
|
|
2418
|
-
const spans = el.querySelectorAll(
|
|
2417
|
+
mount(h(Parent, null, h('span', null, 'child1'), h('span', null, 'child2')), el)
|
|
2418
|
+
const spans = el.querySelectorAll('#parent span')
|
|
2419
2419
|
expect(spans.length).toBe(2)
|
|
2420
2420
|
})
|
|
2421
2421
|
|
|
2422
|
-
test(
|
|
2422
|
+
test('component with single child merges as singular children prop', () => {
|
|
2423
2423
|
const el = container()
|
|
2424
2424
|
const Parent = defineComponent((props: { children?: VNodeChild }) =>
|
|
2425
|
-
h(
|
|
2425
|
+
h('div', { id: 'single' }, props.children),
|
|
2426
2426
|
)
|
|
2427
|
-
mount(h(Parent, null, h(
|
|
2428
|
-
expect(el.querySelector(
|
|
2427
|
+
mount(h(Parent, null, h('b', null, 'only')), el)
|
|
2428
|
+
expect(el.querySelector('#single b')?.textContent).toBe('only')
|
|
2429
2429
|
})
|
|
2430
2430
|
|
|
2431
|
-
test(
|
|
2431
|
+
test('component props.children already set — no merge', () => {
|
|
2432
2432
|
const el = container()
|
|
2433
2433
|
const Parent = defineComponent((props: { children?: VNodeChild }) =>
|
|
2434
|
-
h(
|
|
2434
|
+
h('div', { id: 'no-merge' }, props.children),
|
|
2435
2435
|
)
|
|
2436
|
-
mount(h(Parent, { children: h(
|
|
2437
|
-
expect(el.querySelector(
|
|
2436
|
+
mount(h(Parent, { children: h('em', null, 'explicit') }, h('b', null, 'ignored')), el)
|
|
2437
|
+
expect(el.querySelector('#no-merge em')?.textContent).toBe('explicit')
|
|
2438
2438
|
})
|
|
2439
2439
|
|
|
2440
|
-
test(
|
|
2440
|
+
test('anonymous component name fallback', () => {
|
|
2441
2441
|
const el = container()
|
|
2442
2442
|
// Use an anonymous arrow function
|
|
2443
|
-
const comp = (() => h(
|
|
2443
|
+
const comp = (() => h('span', null, 'anon')) as unknown as ReturnType<typeof defineComponent>
|
|
2444
2444
|
mount(h(comp, null), el)
|
|
2445
|
-
expect(el.querySelector(
|
|
2445
|
+
expect(el.querySelector('span')?.textContent).toBe('anon')
|
|
2446
2446
|
})
|
|
2447
2447
|
})
|
|
2448
2448
|
|
|
2449
2449
|
// ─── props.ts — additional coverage ──────────────────────────────────────────
|
|
2450
2450
|
|
|
2451
|
-
describe(
|
|
2452
|
-
test(
|
|
2451
|
+
describe('props — additional coverage', () => {
|
|
2452
|
+
test('reactive prop via function', () => {
|
|
2453
2453
|
const el = container()
|
|
2454
|
-
const title = signal(
|
|
2455
|
-
mount(h(
|
|
2456
|
-
const div = el.querySelector(
|
|
2457
|
-
expect(div.title).toBe(
|
|
2458
|
-
title.set(
|
|
2459
|
-
expect(div.title).toBe(
|
|
2454
|
+
const title = signal('hello')
|
|
2455
|
+
mount(h('div', { title: () => title() }), el)
|
|
2456
|
+
const div = el.querySelector('div') as HTMLElement
|
|
2457
|
+
expect(div.title).toBe('hello')
|
|
2458
|
+
title.set('world')
|
|
2459
|
+
expect(div.title).toBe('world')
|
|
2460
2460
|
})
|
|
2461
2461
|
|
|
2462
|
-
test(
|
|
2462
|
+
test('null value removes attribute', () => {
|
|
2463
2463
|
const el = container()
|
|
2464
|
-
mount(h(
|
|
2465
|
-
const div = el.querySelector(
|
|
2466
|
-
expect(div.getAttribute(
|
|
2464
|
+
mount(h('div', { 'data-x': 'initial' }), el)
|
|
2465
|
+
const div = el.querySelector('div') as HTMLElement
|
|
2466
|
+
expect(div.getAttribute('data-x')).toBe('initial')
|
|
2467
2467
|
})
|
|
2468
2468
|
|
|
2469
|
-
test(
|
|
2469
|
+
test('key and ref props are skipped in applyProps', () => {
|
|
2470
2470
|
const el = container()
|
|
2471
2471
|
const ref = createRef<HTMLDivElement>()
|
|
2472
|
-
mount(h(
|
|
2473
|
-
const div = el.querySelector(
|
|
2472
|
+
mount(h('div', { key: 'k', ref, 'data-test': 'yes' }), el)
|
|
2473
|
+
const div = el.querySelector('div') as HTMLElement
|
|
2474
2474
|
// key should not be an attribute
|
|
2475
|
-
expect(div.hasAttribute(
|
|
2475
|
+
expect(div.hasAttribute('key')).toBe(false)
|
|
2476
2476
|
// ref should not be an attribute
|
|
2477
|
-
expect(div.hasAttribute(
|
|
2477
|
+
expect(div.hasAttribute('ref')).toBe(false)
|
|
2478
2478
|
// data-test should be set
|
|
2479
|
-
expect(div.getAttribute(
|
|
2479
|
+
expect(div.getAttribute('data-test')).toBe('yes')
|
|
2480
2480
|
})
|
|
2481
2481
|
|
|
2482
|
-
test(
|
|
2482
|
+
test('sanitizes javascript: in action attribute', () => {
|
|
2483
2483
|
const el = container()
|
|
2484
|
-
mount(h(
|
|
2485
|
-
const form = el.querySelector(
|
|
2486
|
-
expect(form.getAttribute(
|
|
2484
|
+
mount(h('form', { action: 'javascript:void(0)' }), el)
|
|
2485
|
+
const form = el.querySelector('form') as HTMLFormElement
|
|
2486
|
+
expect(form.getAttribute('action')).not.toBe('javascript:void(0)')
|
|
2487
2487
|
})
|
|
2488
2488
|
|
|
2489
|
-
test(
|
|
2489
|
+
test('sanitizes data: in formaction', () => {
|
|
2490
2490
|
const el = container()
|
|
2491
|
-
mount(h(
|
|
2492
|
-
const btn = el.querySelector(
|
|
2493
|
-
expect(btn.getAttribute(
|
|
2491
|
+
mount(h('button', { formaction: 'data:text/html,<script>alert(1)</script>' }), el)
|
|
2492
|
+
const btn = el.querySelector('button') as HTMLButtonElement
|
|
2493
|
+
expect(btn.getAttribute('formaction')).not.toBe('data:text/html,<script>alert(1)</script>')
|
|
2494
2494
|
})
|
|
2495
2495
|
|
|
2496
|
-
test(
|
|
2496
|
+
test('sanitizes javascript: with leading whitespace', () => {
|
|
2497
2497
|
const el = container()
|
|
2498
|
-
mount(h(
|
|
2499
|
-
const a = el.querySelector(
|
|
2500
|
-
expect(a.getAttribute(
|
|
2498
|
+
mount(h('a', { href: ' javascript:alert(1)' }), el)
|
|
2499
|
+
const a = el.querySelector('a') as HTMLAnchorElement
|
|
2500
|
+
expect(a.getAttribute('href')).not.toBe(' javascript:alert(1)')
|
|
2501
2501
|
})
|
|
2502
2502
|
|
|
2503
|
-
test(
|
|
2504
|
-
const result = sanitizeHtml(
|
|
2505
|
-
expect(result).toContain(
|
|
2506
|
-
expect(result).toContain(
|
|
2503
|
+
test('sanitizeHtml preserves safe tags', async () => {
|
|
2504
|
+
const result = sanitizeHtml('<b>bold</b><em>italic</em>')
|
|
2505
|
+
expect(result).toContain('<b>bold</b>')
|
|
2506
|
+
expect(result).toContain('<em>italic</em>')
|
|
2507
2507
|
})
|
|
2508
2508
|
|
|
2509
|
-
test(
|
|
2510
|
-
const result = sanitizeHtml(
|
|
2511
|
-
expect(result).toContain(
|
|
2512
|
-
expect(result).not.toContain(
|
|
2509
|
+
test('sanitizeHtml strips script tags', async () => {
|
|
2510
|
+
const result = sanitizeHtml('<div>safe</div><script>alert(1)</script>')
|
|
2511
|
+
expect(result).toContain('safe')
|
|
2512
|
+
expect(result).not.toContain('<script>')
|
|
2513
2513
|
})
|
|
2514
2514
|
|
|
2515
|
-
test(
|
|
2515
|
+
test('sanitizeHtml strips event handler attributes', async () => {
|
|
2516
2516
|
const result = sanitizeHtml('<div onclick="alert(1)">hello</div>')
|
|
2517
|
-
expect(result).toContain(
|
|
2518
|
-
expect(result).not.toContain(
|
|
2517
|
+
expect(result).toContain('hello')
|
|
2518
|
+
expect(result).not.toContain('onclick')
|
|
2519
2519
|
})
|
|
2520
2520
|
|
|
2521
|
-
test(
|
|
2521
|
+
test('sanitizeHtml strips javascript: URLs', async () => {
|
|
2522
2522
|
const result = sanitizeHtml('<a href="javascript:alert(1)">link</a>')
|
|
2523
|
-
expect(result).not.toContain(
|
|
2523
|
+
expect(result).not.toContain('javascript:')
|
|
2524
2524
|
})
|
|
2525
2525
|
|
|
2526
|
-
test(
|
|
2526
|
+
test('setSanitizer overrides built-in sanitizer', async () => {
|
|
2527
2527
|
// Set custom sanitizer that uppercases everything
|
|
2528
2528
|
setSanitizer((html: string) => html.toUpperCase())
|
|
2529
|
-
expect(sanitizeHtml(
|
|
2529
|
+
expect(sanitizeHtml('<b>hello</b>')).toBe('<B>HELLO</B>')
|
|
2530
2530
|
// Reset to built-in
|
|
2531
2531
|
setSanitizer(null)
|
|
2532
2532
|
// Built-in should work again
|
|
2533
|
-
const result = sanitizeHtml(
|
|
2534
|
-
expect(result).toContain(
|
|
2535
|
-
expect(result).not.toContain(
|
|
2533
|
+
const result = sanitizeHtml('<b>safe</b><script>bad</script>')
|
|
2534
|
+
expect(result).toContain('safe')
|
|
2535
|
+
expect(result).not.toContain('<script>')
|
|
2536
2536
|
})
|
|
2537
2537
|
|
|
2538
|
-
test(
|
|
2538
|
+
test('sanitizeHtml strips iframe and object tags', async () => {
|
|
2539
2539
|
const result = sanitizeHtml(
|
|
2540
2540
|
'<div>ok</div><iframe src="evil"></iframe><object data="x"></object>',
|
|
2541
2541
|
)
|
|
2542
|
-
expect(result).toContain(
|
|
2543
|
-
expect(result).not.toContain(
|
|
2544
|
-
expect(result).not.toContain(
|
|
2542
|
+
expect(result).toContain('ok')
|
|
2543
|
+
expect(result).not.toContain('<iframe')
|
|
2544
|
+
expect(result).not.toContain('<object')
|
|
2545
2545
|
})
|
|
2546
2546
|
|
|
2547
|
-
test(
|
|
2548
|
-
const result = sanitizeHtml(
|
|
2549
|
-
expect(result).not.toContain(
|
|
2547
|
+
test('sanitizeHtml handles nested unsafe elements', async () => {
|
|
2548
|
+
const result = sanitizeHtml('<div><script><script>alert(1)</script></script></div>')
|
|
2549
|
+
expect(result).not.toContain('<script')
|
|
2550
2550
|
})
|
|
2551
2551
|
|
|
2552
|
-
test(
|
|
2552
|
+
test('DOM property for known properties like value', () => {
|
|
2553
2553
|
const el = container()
|
|
2554
|
-
mount(h(
|
|
2555
|
-
const input = el.querySelector(
|
|
2556
|
-
expect(input.value).toBe(
|
|
2554
|
+
mount(h('input', { value: 'test', type: 'text' }), el)
|
|
2555
|
+
const input = el.querySelector('input') as HTMLInputElement
|
|
2556
|
+
expect(input.value).toBe('test')
|
|
2557
2557
|
})
|
|
2558
2558
|
|
|
2559
|
-
test(
|
|
2559
|
+
test('setAttribute fallback for unknown attributes', () => {
|
|
2560
2560
|
const el = container()
|
|
2561
|
-
mount(h(
|
|
2562
|
-
const div = el.querySelector(
|
|
2563
|
-
expect(div.getAttribute(
|
|
2561
|
+
mount(h('div', { 'aria-label': 'test label' }), el)
|
|
2562
|
+
const div = el.querySelector('div') as HTMLElement
|
|
2563
|
+
expect(div.getAttribute('aria-label')).toBe('test label')
|
|
2564
2564
|
})
|
|
2565
2565
|
})
|
|
2566
2566
|
|
|
2567
2567
|
// ─── DevTools ────────────────────────────────────────────────────────────────
|
|
2568
2568
|
|
|
2569
|
-
describe(
|
|
2570
|
-
test(
|
|
2569
|
+
describe('DevTools', () => {
|
|
2570
|
+
test('installDevTools sets __PYREON_DEVTOOLS__ on window', async () => {
|
|
2571
2571
|
installDevTools()
|
|
2572
2572
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as Record<
|
|
2573
2573
|
string,
|
|
2574
2574
|
unknown
|
|
2575
2575
|
>
|
|
2576
2576
|
expect(devtools).not.toBeNull()
|
|
2577
|
-
expect(devtools.version).toBe(
|
|
2577
|
+
expect(devtools.version).toBe('0.1.0')
|
|
2578
2578
|
})
|
|
2579
2579
|
|
|
2580
|
-
test(
|
|
2580
|
+
test('registerComponent and getAllComponents', async () => {
|
|
2581
2581
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2582
2582
|
getAllComponents: () => {
|
|
2583
2583
|
id: string
|
|
@@ -2591,17 +2591,17 @@ describe("DevTools", () => {
|
|
|
2591
2591
|
onComponentUnmount: (cb: (id: string) => void) => () => void
|
|
2592
2592
|
}
|
|
2593
2593
|
|
|
2594
|
-
registerComponent(
|
|
2594
|
+
registerComponent('test-1', 'TestComp', null, null)
|
|
2595
2595
|
const all = devtools.getAllComponents()
|
|
2596
|
-
const found = all.find((c: { id: string }) => c.id ===
|
|
2596
|
+
const found = all.find((c: { id: string }) => c.id === 'test-1')
|
|
2597
2597
|
expect(found).not.toBeUndefined()
|
|
2598
|
-
expect(found?.name).toBe(
|
|
2598
|
+
expect(found?.name).toBe('TestComp')
|
|
2599
2599
|
|
|
2600
2600
|
// Cleanup
|
|
2601
|
-
unregisterComponent(
|
|
2601
|
+
unregisterComponent('test-1')
|
|
2602
2602
|
})
|
|
2603
2603
|
|
|
2604
|
-
test(
|
|
2604
|
+
test('registerComponent with parentId creates parent-child relationship', async () => {
|
|
2605
2605
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2606
2606
|
getAllComponents: () => {
|
|
2607
2607
|
id: string
|
|
@@ -2611,55 +2611,55 @@ describe("DevTools", () => {
|
|
|
2611
2611
|
}[]
|
|
2612
2612
|
}
|
|
2613
2613
|
|
|
2614
|
-
registerComponent(
|
|
2615
|
-
registerComponent(
|
|
2614
|
+
registerComponent('parent-1', 'Parent', null, null)
|
|
2615
|
+
registerComponent('child-1', 'Child', null, 'parent-1')
|
|
2616
2616
|
|
|
2617
|
-
const parent = devtools.getAllComponents().find((c: { id: string }) => c.id ===
|
|
2618
|
-
expect(parent?.childIds).toContain(
|
|
2617
|
+
const parent = devtools.getAllComponents().find((c: { id: string }) => c.id === 'parent-1')
|
|
2618
|
+
expect(parent?.childIds).toContain('child-1')
|
|
2619
2619
|
|
|
2620
|
-
unregisterComponent(
|
|
2621
|
-
const parentAfter = devtools.getAllComponents().find((c: { id: string }) => c.id ===
|
|
2622
|
-
expect(parentAfter?.childIds).not.toContain(
|
|
2620
|
+
unregisterComponent('child-1')
|
|
2621
|
+
const parentAfter = devtools.getAllComponents().find((c: { id: string }) => c.id === 'parent-1')
|
|
2622
|
+
expect(parentAfter?.childIds).not.toContain('child-1')
|
|
2623
2623
|
|
|
2624
|
-
unregisterComponent(
|
|
2624
|
+
unregisterComponent('parent-1')
|
|
2625
2625
|
})
|
|
2626
2626
|
|
|
2627
|
-
test(
|
|
2627
|
+
test('getComponentTree returns only root components', async () => {
|
|
2628
2628
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2629
2629
|
getComponentTree: () => { id: string; parentId: string | null }[]
|
|
2630
2630
|
}
|
|
2631
2631
|
|
|
2632
|
-
registerComponent(
|
|
2633
|
-
registerComponent(
|
|
2632
|
+
registerComponent('root-1', 'Root', null, null)
|
|
2633
|
+
registerComponent('sub-1', 'Sub', null, 'root-1')
|
|
2634
2634
|
|
|
2635
2635
|
const tree = devtools.getComponentTree()
|
|
2636
|
-
const rootInTree = tree.find((c: { id: string }) => c.id ===
|
|
2637
|
-
const subInTree = tree.find((c: { id: string }) => c.id ===
|
|
2636
|
+
const rootInTree = tree.find((c: { id: string }) => c.id === 'root-1')
|
|
2637
|
+
const subInTree = tree.find((c: { id: string }) => c.id === 'sub-1')
|
|
2638
2638
|
expect(rootInTree).not.toBeUndefined()
|
|
2639
2639
|
expect(subInTree).toBeUndefined() // sub is not root
|
|
2640
2640
|
|
|
2641
|
-
unregisterComponent(
|
|
2642
|
-
unregisterComponent(
|
|
2641
|
+
unregisterComponent('sub-1')
|
|
2642
|
+
unregisterComponent('root-1')
|
|
2643
2643
|
})
|
|
2644
2644
|
|
|
2645
|
-
test(
|
|
2645
|
+
test('highlight adds and removes outline', async () => {
|
|
2646
2646
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2647
2647
|
highlight: (id: string) => void
|
|
2648
2648
|
}
|
|
2649
|
-
const el = document.createElement(
|
|
2649
|
+
const el = document.createElement('div')
|
|
2650
2650
|
document.body.appendChild(el)
|
|
2651
|
-
registerComponent(
|
|
2652
|
-
devtools.highlight(
|
|
2653
|
-
expect(el.style.outline).toContain(
|
|
2651
|
+
registerComponent('hl-1', 'Highlight', el, null)
|
|
2652
|
+
devtools.highlight('hl-1')
|
|
2653
|
+
expect(el.style.outline).toContain('#00b4d8')
|
|
2654
2654
|
|
|
2655
2655
|
// Highlight non-existent — should not throw
|
|
2656
|
-
devtools.highlight(
|
|
2656
|
+
devtools.highlight('non-existent')
|
|
2657
2657
|
|
|
2658
|
-
unregisterComponent(
|
|
2658
|
+
unregisterComponent('hl-1')
|
|
2659
2659
|
el.remove()
|
|
2660
2660
|
})
|
|
2661
2661
|
|
|
2662
|
-
test(
|
|
2662
|
+
test('onComponentMount and onComponentUnmount listeners', async () => {
|
|
2663
2663
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2664
2664
|
onComponentMount: (cb: (entry: { id: string; name: string }) => void) => () => void
|
|
2665
2665
|
onComponentUnmount: (cb: (id: string) => void) => () => void
|
|
@@ -2670,150 +2670,150 @@ describe("DevTools", () => {
|
|
|
2670
2670
|
const unsubMount = devtools.onComponentMount((entry) => mountedIds.push(entry.id))
|
|
2671
2671
|
const unsubUnmount = devtools.onComponentUnmount((id) => unmountedIds.push(id))
|
|
2672
2672
|
|
|
2673
|
-
registerComponent(
|
|
2674
|
-
expect(mountedIds).toContain(
|
|
2673
|
+
registerComponent('listen-1', 'ListenComp', null, null)
|
|
2674
|
+
expect(mountedIds).toContain('listen-1')
|
|
2675
2675
|
|
|
2676
|
-
unregisterComponent(
|
|
2677
|
-
expect(unmountedIds).toContain(
|
|
2676
|
+
unregisterComponent('listen-1')
|
|
2677
|
+
expect(unmountedIds).toContain('listen-1')
|
|
2678
2678
|
|
|
2679
2679
|
// Unsub and verify listeners are removed
|
|
2680
2680
|
unsubMount()
|
|
2681
2681
|
unsubUnmount()
|
|
2682
|
-
registerComponent(
|
|
2683
|
-
expect(mountedIds).not.toContain(
|
|
2684
|
-
unregisterComponent(
|
|
2685
|
-
expect(unmountedIds).not.toContain(
|
|
2682
|
+
registerComponent('listen-2', 'ListenComp2', null, null)
|
|
2683
|
+
expect(mountedIds).not.toContain('listen-2')
|
|
2684
|
+
unregisterComponent('listen-2')
|
|
2685
|
+
expect(unmountedIds).not.toContain('listen-2')
|
|
2686
2686
|
})
|
|
2687
2687
|
|
|
2688
|
-
test(
|
|
2688
|
+
test('unregisterComponent is noop for unknown id', async () => {
|
|
2689
2689
|
// Should not throw
|
|
2690
|
-
unregisterComponent(
|
|
2690
|
+
unregisterComponent('does-not-exist')
|
|
2691
2691
|
})
|
|
2692
2692
|
|
|
2693
|
-
test(
|
|
2693
|
+
test('highlight with no el is noop', async () => {
|
|
2694
2694
|
const devtools = (window as unknown as Record<string, unknown>).__PYREON_DEVTOOLS__ as {
|
|
2695
2695
|
highlight: (id: string) => void
|
|
2696
2696
|
}
|
|
2697
|
-
registerComponent(
|
|
2697
|
+
registerComponent('no-el', 'NoEl', null, null)
|
|
2698
2698
|
// Should not throw
|
|
2699
|
-
devtools.highlight(
|
|
2700
|
-
unregisterComponent(
|
|
2699
|
+
devtools.highlight('no-el')
|
|
2700
|
+
unregisterComponent('no-el')
|
|
2701
2701
|
})
|
|
2702
2702
|
})
|
|
2703
2703
|
|
|
2704
2704
|
// ─── TransitionGroup ─────────────────────────────────────────────────────────
|
|
2705
2705
|
|
|
2706
|
-
describe(
|
|
2707
|
-
test(
|
|
2706
|
+
describe('TransitionGroup', () => {
|
|
2707
|
+
test('renders container element with specified tag', async () => {
|
|
2708
2708
|
const el = container()
|
|
2709
2709
|
const items = signal([{ id: 1 }, { id: 2 }])
|
|
2710
2710
|
mount(
|
|
2711
2711
|
h(TransitionGroup, {
|
|
2712
|
-
tag:
|
|
2713
|
-
name:
|
|
2712
|
+
tag: 'ul',
|
|
2713
|
+
name: 'list',
|
|
2714
2714
|
items,
|
|
2715
2715
|
keyFn: (item: { id: number }) => item.id,
|
|
2716
|
-
render: (item: { id: number }) => h(
|
|
2716
|
+
render: (item: { id: number }) => h('li', null, String(item.id)),
|
|
2717
2717
|
}),
|
|
2718
2718
|
el,
|
|
2719
2719
|
)
|
|
2720
2720
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2721
|
-
expect(el.querySelector(
|
|
2721
|
+
expect(el.querySelector('ul')).not.toBeNull()
|
|
2722
2722
|
})
|
|
2723
2723
|
|
|
2724
|
-
test(
|
|
2724
|
+
test('renders initial items', async () => {
|
|
2725
2725
|
const el = container()
|
|
2726
2726
|
const items = signal([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
2727
2727
|
mount(
|
|
2728
2728
|
h(TransitionGroup, {
|
|
2729
|
-
tag:
|
|
2729
|
+
tag: 'div',
|
|
2730
2730
|
items,
|
|
2731
2731
|
keyFn: (item: { id: number }) => item.id,
|
|
2732
|
-
render: (item: { id: number }) => h(
|
|
2732
|
+
render: (item: { id: number }) => h('span', { class: 'item' }, String(item.id)),
|
|
2733
2733
|
}),
|
|
2734
2734
|
el,
|
|
2735
2735
|
)
|
|
2736
2736
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2737
|
-
const spans = el.querySelectorAll(
|
|
2737
|
+
const spans = el.querySelectorAll('span.item')
|
|
2738
2738
|
expect(spans.length).toBe(3)
|
|
2739
|
-
expect(spans[0]?.textContent).toBe(
|
|
2740
|
-
expect(spans[2]?.textContent).toBe(
|
|
2739
|
+
expect(spans[0]?.textContent).toBe('1')
|
|
2740
|
+
expect(spans[2]?.textContent).toBe('3')
|
|
2741
2741
|
})
|
|
2742
2742
|
|
|
2743
|
-
test(
|
|
2743
|
+
test('adding items triggers enter animation', async () => {
|
|
2744
2744
|
const el = container()
|
|
2745
2745
|
const items = signal([{ id: 1 }])
|
|
2746
2746
|
mount(
|
|
2747
2747
|
h(TransitionGroup, {
|
|
2748
|
-
tag:
|
|
2749
|
-
name:
|
|
2748
|
+
tag: 'div',
|
|
2749
|
+
name: 'fade',
|
|
2750
2750
|
items,
|
|
2751
2751
|
keyFn: (item: { id: number }) => item.id,
|
|
2752
|
-
render: (item: { id: number }) => h(
|
|
2752
|
+
render: (item: { id: number }) => h('span', { class: 'item' }, String(item.id)),
|
|
2753
2753
|
}),
|
|
2754
2754
|
el,
|
|
2755
2755
|
)
|
|
2756
2756
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2757
|
-
expect(el.querySelectorAll(
|
|
2757
|
+
expect(el.querySelectorAll('span.item').length).toBe(1)
|
|
2758
2758
|
|
|
2759
2759
|
// Add a new item
|
|
2760
2760
|
items.set([{ id: 1 }, { id: 2 }])
|
|
2761
2761
|
// Wait for the microtask (enter animation scheduled via queueMicrotask)
|
|
2762
2762
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2763
2763
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2764
|
-
expect(el.querySelectorAll(
|
|
2764
|
+
expect(el.querySelectorAll('span.item').length).toBe(2)
|
|
2765
2765
|
})
|
|
2766
2766
|
|
|
2767
|
-
test(
|
|
2767
|
+
test('removing items keeps element during leave animation', async () => {
|
|
2768
2768
|
const el = container()
|
|
2769
2769
|
const items = signal([{ id: 1 }, { id: 2 }])
|
|
2770
2770
|
mount(
|
|
2771
2771
|
h(TransitionGroup, {
|
|
2772
|
-
tag:
|
|
2773
|
-
name:
|
|
2772
|
+
tag: 'div',
|
|
2773
|
+
name: 'fade',
|
|
2774
2774
|
items,
|
|
2775
2775
|
keyFn: (item: { id: number }) => item.id,
|
|
2776
|
-
render: (item: { id: number }) => h(
|
|
2776
|
+
render: (item: { id: number }) => h('span', { class: 'item' }, String(item.id)),
|
|
2777
2777
|
}),
|
|
2778
2778
|
el,
|
|
2779
2779
|
)
|
|
2780
2780
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2781
|
-
expect(el.querySelectorAll(
|
|
2781
|
+
expect(el.querySelectorAll('span.item').length).toBe(2)
|
|
2782
2782
|
|
|
2783
2783
|
// Remove item 2
|
|
2784
2784
|
items.set([{ id: 1 }])
|
|
2785
2785
|
// Element should still be in DOM during leave animation
|
|
2786
|
-
expect(el.querySelectorAll(
|
|
2786
|
+
expect(el.querySelectorAll('span.item').length).toBeGreaterThanOrEqual(1)
|
|
2787
2787
|
})
|
|
2788
2788
|
|
|
2789
|
-
test(
|
|
2789
|
+
test('default tag is div and default name is pyreon', async () => {
|
|
2790
2790
|
const el = container()
|
|
2791
2791
|
const items = signal([{ id: 1 }])
|
|
2792
2792
|
mount(
|
|
2793
2793
|
h(TransitionGroup, {
|
|
2794
2794
|
items,
|
|
2795
2795
|
keyFn: (item: { id: number }) => item.id,
|
|
2796
|
-
render: (item: { id: number }) => h(
|
|
2796
|
+
render: (item: { id: number }) => h('span', null, String(item.id)),
|
|
2797
2797
|
}),
|
|
2798
2798
|
el,
|
|
2799
2799
|
)
|
|
2800
2800
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2801
2801
|
// Default tag is div
|
|
2802
|
-
expect(el.querySelector(
|
|
2802
|
+
expect(el.querySelector('div')).not.toBeNull()
|
|
2803
2803
|
})
|
|
2804
2804
|
|
|
2805
|
-
test(
|
|
2805
|
+
test('appear option triggers enter on initial mount', async () => {
|
|
2806
2806
|
const el = container()
|
|
2807
2807
|
let enterCalled = false
|
|
2808
2808
|
const items = signal([{ id: 1 }])
|
|
2809
2809
|
mount(
|
|
2810
2810
|
h(TransitionGroup, {
|
|
2811
|
-
tag:
|
|
2812
|
-
name:
|
|
2811
|
+
tag: 'div',
|
|
2812
|
+
name: 'test',
|
|
2813
2813
|
appear: true,
|
|
2814
2814
|
items,
|
|
2815
2815
|
keyFn: (item: { id: number }) => item.id,
|
|
2816
|
-
render: (item: { id: number }) => h(
|
|
2816
|
+
render: (item: { id: number }) => h('span', { class: 'appear-item' }, String(item.id)),
|
|
2817
2817
|
onBeforeEnter: () => {
|
|
2818
2818
|
enterCalled = true
|
|
2819
2819
|
},
|
|
@@ -2825,40 +2825,40 @@ describe("TransitionGroup", () => {
|
|
|
2825
2825
|
expect(enterCalled).toBe(true)
|
|
2826
2826
|
})
|
|
2827
2827
|
|
|
2828
|
-
test(
|
|
2828
|
+
test('custom class name overrides', async () => {
|
|
2829
2829
|
const el = container()
|
|
2830
2830
|
const items = signal([{ id: 1 }])
|
|
2831
2831
|
mount(
|
|
2832
2832
|
h(TransitionGroup, {
|
|
2833
|
-
tag:
|
|
2834
|
-
enterFrom:
|
|
2835
|
-
enterActive:
|
|
2836
|
-
enterTo:
|
|
2837
|
-
leaveFrom:
|
|
2838
|
-
leaveActive:
|
|
2839
|
-
leaveTo:
|
|
2840
|
-
moveClass:
|
|
2833
|
+
tag: 'div',
|
|
2834
|
+
enterFrom: 'my-enter-from',
|
|
2835
|
+
enterActive: 'my-enter-active',
|
|
2836
|
+
enterTo: 'my-enter-to',
|
|
2837
|
+
leaveFrom: 'my-leave-from',
|
|
2838
|
+
leaveActive: 'my-leave-active',
|
|
2839
|
+
leaveTo: 'my-leave-to',
|
|
2840
|
+
moveClass: 'my-move',
|
|
2841
2841
|
items,
|
|
2842
2842
|
keyFn: (item: { id: number }) => item.id,
|
|
2843
|
-
render: (item: { id: number }) => h(
|
|
2843
|
+
render: (item: { id: number }) => h('span', null, String(item.id)),
|
|
2844
2844
|
}),
|
|
2845
2845
|
el,
|
|
2846
2846
|
)
|
|
2847
2847
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2848
|
-
expect(el.querySelectorAll(
|
|
2848
|
+
expect(el.querySelectorAll('span').length).toBe(1)
|
|
2849
2849
|
})
|
|
2850
2850
|
|
|
2851
|
-
test(
|
|
2851
|
+
test('leave callback with no ref.current removes entry immediately', async () => {
|
|
2852
2852
|
const el = container()
|
|
2853
2853
|
const items = signal([{ id: 1 }, { id: 2 }])
|
|
2854
2854
|
mount(
|
|
2855
2855
|
h(TransitionGroup, {
|
|
2856
|
-
tag:
|
|
2856
|
+
tag: 'div',
|
|
2857
2857
|
items,
|
|
2858
2858
|
keyFn: (item: { id: number }) => item.id,
|
|
2859
2859
|
// Return a non-element VNode (component VNode) so ref won't be injected
|
|
2860
2860
|
render: (item: { id: number }) => {
|
|
2861
|
-
const Comp = () => h(
|
|
2861
|
+
const Comp = () => h('span', null, String(item.id))
|
|
2862
2862
|
return h(Comp, null) as unknown as ReturnType<typeof h>
|
|
2863
2863
|
},
|
|
2864
2864
|
}),
|
|
@@ -2871,44 +2871,44 @@ describe("TransitionGroup", () => {
|
|
|
2871
2871
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2872
2872
|
})
|
|
2873
2873
|
|
|
2874
|
-
test(
|
|
2874
|
+
test('reorder triggers move animation setup', async () => {
|
|
2875
2875
|
const el = container()
|
|
2876
2876
|
const items = signal([{ id: 1 }, { id: 2 }, { id: 3 }])
|
|
2877
2877
|
mount(
|
|
2878
2878
|
h(TransitionGroup, {
|
|
2879
|
-
tag:
|
|
2880
|
-
name:
|
|
2879
|
+
tag: 'div',
|
|
2880
|
+
name: 'list',
|
|
2881
2881
|
items,
|
|
2882
2882
|
keyFn: (item: { id: number }) => item.id,
|
|
2883
|
-
render: (item: { id: number }) => h(
|
|
2883
|
+
render: (item: { id: number }) => h('span', { class: 'reorder-item' }, String(item.id)),
|
|
2884
2884
|
}),
|
|
2885
2885
|
el,
|
|
2886
2886
|
)
|
|
2887
2887
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2888
|
-
expect(el.querySelectorAll(
|
|
2888
|
+
expect(el.querySelectorAll('span.reorder-item').length).toBe(3)
|
|
2889
2889
|
|
|
2890
2890
|
// Reorder items
|
|
2891
2891
|
items.set([{ id: 3 }, { id: 1 }, { id: 2 }])
|
|
2892
2892
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
2893
2893
|
|
|
2894
2894
|
// Items should be reordered
|
|
2895
|
-
const spans = el.querySelectorAll(
|
|
2896
|
-
expect(spans[0]?.textContent).toBe(
|
|
2897
|
-
expect(spans[1]?.textContent).toBe(
|
|
2898
|
-
expect(spans[2]?.textContent).toBe(
|
|
2895
|
+
const spans = el.querySelectorAll('span.reorder-item')
|
|
2896
|
+
expect(spans[0]?.textContent).toBe('3')
|
|
2897
|
+
expect(spans[1]?.textContent).toBe('1')
|
|
2898
|
+
expect(spans[2]?.textContent).toBe('2')
|
|
2899
2899
|
})
|
|
2900
2900
|
|
|
2901
|
-
test(
|
|
2901
|
+
test('onAfterEnter callback fires after enter transition', async () => {
|
|
2902
2902
|
const el = container()
|
|
2903
2903
|
let afterEnterCalled = false
|
|
2904
2904
|
const items = signal<{ id: number }[]>([])
|
|
2905
2905
|
mount(
|
|
2906
2906
|
h(TransitionGroup, {
|
|
2907
|
-
tag:
|
|
2908
|
-
name:
|
|
2907
|
+
tag: 'div',
|
|
2908
|
+
name: 'fade',
|
|
2909
2909
|
items,
|
|
2910
2910
|
keyFn: (item: { id: number }) => item.id,
|
|
2911
|
-
render: (item: { id: number }) => h(
|
|
2911
|
+
render: (item: { id: number }) => h('span', null, String(item.id)),
|
|
2912
2912
|
onAfterEnter: () => {
|
|
2913
2913
|
afterEnterCalled = true
|
|
2914
2914
|
},
|
|
@@ -2924,24 +2924,24 @@ describe("TransitionGroup", () => {
|
|
|
2924
2924
|
|
|
2925
2925
|
// Trigger rAF and transitionend
|
|
2926
2926
|
await new Promise<void>((r) => requestAnimationFrame(() => r()))
|
|
2927
|
-
const span = el.querySelector(
|
|
2927
|
+
const span = el.querySelector('span')
|
|
2928
2928
|
if (span) {
|
|
2929
|
-
span.dispatchEvent(new Event(
|
|
2929
|
+
span.dispatchEvent(new Event('transitionend'))
|
|
2930
2930
|
}
|
|
2931
2931
|
expect(afterEnterCalled).toBe(true)
|
|
2932
2932
|
})
|
|
2933
2933
|
|
|
2934
|
-
test(
|
|
2934
|
+
test('onBeforeLeave and onAfterLeave callbacks fire', async () => {
|
|
2935
2935
|
const el = container()
|
|
2936
2936
|
let beforeLeaveCalled = false
|
|
2937
2937
|
const items = signal([{ id: 1 }])
|
|
2938
2938
|
mount(
|
|
2939
2939
|
h(TransitionGroup, {
|
|
2940
|
-
tag:
|
|
2941
|
-
name:
|
|
2940
|
+
tag: 'div',
|
|
2941
|
+
name: 'fade',
|
|
2942
2942
|
items,
|
|
2943
2943
|
keyFn: (item: { id: number }) => item.id,
|
|
2944
|
-
render: (item: { id: number }) => h(
|
|
2944
|
+
render: (item: { id: number }) => h('span', { class: 'leave-item' }, String(item.id)),
|
|
2945
2945
|
onBeforeLeave: () => {
|
|
2946
2946
|
beforeLeaveCalled = true
|
|
2947
2947
|
},
|
|
@@ -2959,22 +2959,22 @@ describe("TransitionGroup", () => {
|
|
|
2959
2959
|
|
|
2960
2960
|
// Trigger leave animation completion
|
|
2961
2961
|
await new Promise<void>((r) => requestAnimationFrame(() => r()))
|
|
2962
|
-
const span = el.querySelector(
|
|
2962
|
+
const span = el.querySelector('span.leave-item')
|
|
2963
2963
|
if (span) {
|
|
2964
|
-
span.dispatchEvent(new Event(
|
|
2964
|
+
span.dispatchEvent(new Event('transitionend'))
|
|
2965
2965
|
}
|
|
2966
2966
|
// afterLeave fires inside rAF callback, might need another tick
|
|
2967
2967
|
await new Promise<void>((r) => requestAnimationFrame(() => r()))
|
|
2968
2968
|
if (span) {
|
|
2969
|
-
span.dispatchEvent(new Event(
|
|
2969
|
+
span.dispatchEvent(new Event('transitionend'))
|
|
2970
2970
|
}
|
|
2971
2971
|
})
|
|
2972
2972
|
})
|
|
2973
2973
|
|
|
2974
2974
|
// ─── Hydration debug ────────────────────────────────────────────────────────
|
|
2975
2975
|
|
|
2976
|
-
describe(
|
|
2977
|
-
test(
|
|
2976
|
+
describe('hydration warnings', () => {
|
|
2977
|
+
test('enableHydrationWarnings and disableHydrationWarnings', async () => {
|
|
2978
2978
|
// Should not throw
|
|
2979
2979
|
enableHydrationWarnings()
|
|
2980
2980
|
disableHydrationWarnings()
|
|
@@ -2984,119 +2984,119 @@ describe("hydration warnings", () => {
|
|
|
2984
2984
|
|
|
2985
2985
|
// ─── Additional hydrate.ts branch coverage ───────────────────────────────────
|
|
2986
2986
|
|
|
2987
|
-
describe(
|
|
2988
|
-
test(
|
|
2987
|
+
describe('hydrateRoot — branch coverage', () => {
|
|
2988
|
+
test('hydrates raw array child (non-Fragment array path)', async () => {
|
|
2989
2989
|
const el = container()
|
|
2990
|
-
el.innerHTML =
|
|
2990
|
+
el.innerHTML = '<span>a</span><span>b</span>'
|
|
2991
2991
|
// Pass an array directly — hits the Array.isArray branch in hydrateChild
|
|
2992
|
-
const cleanup = hydrateRoot(el, [h(
|
|
2993
|
-
expect(el.querySelectorAll(
|
|
2992
|
+
const cleanup = hydrateRoot(el, [h('span', null, 'a'), h('span', null, 'b')])
|
|
2993
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
2994
2994
|
cleanup()
|
|
2995
2995
|
})
|
|
2996
2996
|
|
|
2997
|
-
test(
|
|
2997
|
+
test('hydrates For with SSR markers (start/end comment pair)', async () => {
|
|
2998
2998
|
const el = container()
|
|
2999
|
-
el.innerHTML =
|
|
2999
|
+
el.innerHTML = '<div><!--pyreon-for--><li>item1</li><li>item2</li><!--/pyreon-for--></div>'
|
|
3000
3000
|
const items = signal([
|
|
3001
|
-
{ id: 1, label:
|
|
3002
|
-
{ id: 2, label:
|
|
3001
|
+
{ id: 1, label: 'item1' },
|
|
3002
|
+
{ id: 2, label: 'item2' },
|
|
3003
3003
|
])
|
|
3004
3004
|
const cleanup = hydrateRoot(
|
|
3005
3005
|
el,
|
|
3006
3006
|
h(
|
|
3007
|
-
|
|
3007
|
+
'div',
|
|
3008
3008
|
null,
|
|
3009
3009
|
For({
|
|
3010
3010
|
each: items,
|
|
3011
3011
|
by: (r: { id: number }) => r.id,
|
|
3012
|
-
children: (r: { id: number; label: string }) => h(
|
|
3012
|
+
children: (r: { id: number; label: string }) => h('li', null, r.label),
|
|
3013
3013
|
}),
|
|
3014
3014
|
),
|
|
3015
3015
|
)
|
|
3016
3016
|
cleanup()
|
|
3017
3017
|
})
|
|
3018
3018
|
|
|
3019
|
-
test(
|
|
3019
|
+
test('hydrates reactive accessor returning null with no domNode', async () => {
|
|
3020
3020
|
const el = container()
|
|
3021
|
-
el.innerHTML =
|
|
3021
|
+
el.innerHTML = '<div></div>'
|
|
3022
3022
|
const show = signal<VNodeChild>(null)
|
|
3023
3023
|
// The div has no children, so domNode will be null inside
|
|
3024
|
-
const cleanup = hydrateRoot(el, h(
|
|
3025
|
-
show.set(
|
|
3024
|
+
const cleanup = hydrateRoot(el, h('div', null, (() => show()) as unknown as VNodeChild))
|
|
3025
|
+
show.set('hello')
|
|
3026
3026
|
cleanup()
|
|
3027
3027
|
})
|
|
3028
3028
|
|
|
3029
|
-
test(
|
|
3029
|
+
test('hydrates reactive VNode accessor with marker when no domNode', async () => {
|
|
3030
3030
|
const el = container()
|
|
3031
|
-
el.innerHTML =
|
|
3032
|
-
const content = signal<VNodeChild>(h(
|
|
3033
|
-
const cleanup = hydrateRoot(el, h(
|
|
3031
|
+
el.innerHTML = '<div></div>'
|
|
3032
|
+
const content = signal<VNodeChild>(h('span', null, 'initial'))
|
|
3033
|
+
const cleanup = hydrateRoot(el, h('div', null, (() => content()) as unknown as VNodeChild))
|
|
3034
3034
|
cleanup()
|
|
3035
3035
|
})
|
|
3036
3036
|
|
|
3037
|
-
test(
|
|
3037
|
+
test('hydrates unknown symbol vnode type — returns noop', async () => {
|
|
3038
3038
|
const el = container()
|
|
3039
|
-
el.innerHTML =
|
|
3040
|
-
const weirdVNode = { type: Symbol(
|
|
3041
|
-
const cleanup = hydrateRoot(el, h(
|
|
3039
|
+
el.innerHTML = '<div></div>'
|
|
3040
|
+
const weirdVNode = { type: Symbol('weird'), props: {}, children: [], key: null }
|
|
3041
|
+
const cleanup = hydrateRoot(el, h('div', null, weirdVNode as VNodeChild))
|
|
3042
3042
|
cleanup()
|
|
3043
3043
|
})
|
|
3044
3044
|
|
|
3045
|
-
test(
|
|
3045
|
+
test('hydration of text that matches existing text node — cleanup removes it', async () => {
|
|
3046
3046
|
const el = container()
|
|
3047
|
-
el.innerHTML =
|
|
3048
|
-
const cleanup = hydrateRoot(el,
|
|
3049
|
-
expect(el.textContent).toBe(
|
|
3047
|
+
el.innerHTML = 'hello'
|
|
3048
|
+
const cleanup = hydrateRoot(el, 'hello')
|
|
3049
|
+
expect(el.textContent).toBe('hello')
|
|
3050
3050
|
cleanup()
|
|
3051
3051
|
})
|
|
3052
3052
|
|
|
3053
|
-
test(
|
|
3053
|
+
test('For with no SSR markers and domNode present', async () => {
|
|
3054
3054
|
const el = container()
|
|
3055
|
-
el.innerHTML =
|
|
3056
|
-
const items = signal([{ id: 1, label:
|
|
3055
|
+
el.innerHTML = '<div><span>existing</span></div>'
|
|
3056
|
+
const items = signal([{ id: 1, label: 'a' }])
|
|
3057
3057
|
const cleanup = hydrateRoot(
|
|
3058
3058
|
el,
|
|
3059
3059
|
h(
|
|
3060
|
-
|
|
3060
|
+
'div',
|
|
3061
3061
|
null,
|
|
3062
3062
|
For({
|
|
3063
3063
|
each: items,
|
|
3064
3064
|
by: (r: { id: number }) => r.id,
|
|
3065
|
-
children: (r: { id: number; label: string }) => h(
|
|
3065
|
+
children: (r: { id: number; label: string }) => h('li', null, r.label),
|
|
3066
3066
|
}),
|
|
3067
3067
|
),
|
|
3068
3068
|
)
|
|
3069
3069
|
cleanup()
|
|
3070
3070
|
})
|
|
3071
3071
|
|
|
3072
|
-
test(
|
|
3072
|
+
test('For with no SSR markers and no domNode', async () => {
|
|
3073
3073
|
const el = container()
|
|
3074
|
-
el.innerHTML =
|
|
3075
|
-
const items = signal([{ id: 1, label:
|
|
3074
|
+
el.innerHTML = '<div></div>'
|
|
3075
|
+
const items = signal([{ id: 1, label: 'a' }])
|
|
3076
3076
|
const cleanup = hydrateRoot(
|
|
3077
3077
|
el,
|
|
3078
3078
|
h(
|
|
3079
|
-
|
|
3079
|
+
'div',
|
|
3080
3080
|
null,
|
|
3081
3081
|
For({
|
|
3082
3082
|
each: items,
|
|
3083
3083
|
by: (r: { id: number }) => r.id,
|
|
3084
|
-
children: (r: { id: number; label: string }) => h(
|
|
3084
|
+
children: (r: { id: number; label: string }) => h('li', null, r.label),
|
|
3085
3085
|
}),
|
|
3086
3086
|
),
|
|
3087
3087
|
)
|
|
3088
3088
|
cleanup()
|
|
3089
3089
|
})
|
|
3090
3090
|
|
|
3091
|
-
test(
|
|
3091
|
+
test('reactive accessor returning null when domNode exists', async () => {
|
|
3092
3092
|
const el = container()
|
|
3093
3093
|
// Put a real DOM node that will be domNode, but accessor returns null
|
|
3094
|
-
el.innerHTML =
|
|
3094
|
+
el.innerHTML = '<div><span>existing</span></div>'
|
|
3095
3095
|
const show = signal<VNodeChild>(null)
|
|
3096
3096
|
// The span is the domNode, but accessor returns null — hits line 91-92
|
|
3097
3097
|
const cleanup = hydrateRoot(
|
|
3098
3098
|
el,
|
|
3099
|
-
h(
|
|
3099
|
+
h('div', null, (() => show()) as unknown as VNodeChild, h('span', null, 'existing')),
|
|
3100
3100
|
)
|
|
3101
3101
|
cleanup()
|
|
3102
3102
|
})
|
|
@@ -3104,22 +3104,22 @@ describe("hydrateRoot — branch coverage", () => {
|
|
|
3104
3104
|
|
|
3105
3105
|
// ─── mount.ts — error handling branches ──────────────────────────────────────
|
|
3106
3106
|
|
|
3107
|
-
describe(
|
|
3108
|
-
test(
|
|
3107
|
+
describe('mount — error handling branches', () => {
|
|
3108
|
+
test('mountChild with raw array', async () => {
|
|
3109
3109
|
const el = container()
|
|
3110
3110
|
// Pass an array directly to mountChild (line 72)
|
|
3111
|
-
const cleanup = mountChild([h(
|
|
3112
|
-
expect(el.querySelectorAll(
|
|
3111
|
+
const cleanup = mountChild([h('span', null, 'x'), h('span', null, 'y')], el, null)
|
|
3112
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
3113
3113
|
cleanup()
|
|
3114
3114
|
})
|
|
3115
3115
|
|
|
3116
|
-
test(
|
|
3116
|
+
test('component subtree throw is caught', () => {
|
|
3117
3117
|
const el = container()
|
|
3118
3118
|
// A component whose render output itself causes an error during mount
|
|
3119
3119
|
const BadRender = defineComponent(() => {
|
|
3120
3120
|
// Return a VNode that includes a broken component child
|
|
3121
3121
|
const Throws = defineComponent((): never => {
|
|
3122
|
-
throw new Error(
|
|
3122
|
+
throw new Error('subtree error')
|
|
3123
3123
|
})
|
|
3124
3124
|
return h(Throws, null)
|
|
3125
3125
|
})
|
|
@@ -3127,26 +3127,26 @@ describe("mount — error handling branches", () => {
|
|
|
3127
3127
|
mount(h(BadRender, null), el)
|
|
3128
3128
|
})
|
|
3129
3129
|
|
|
3130
|
-
test(
|
|
3130
|
+
test('onMount hook that throws is caught', async () => {
|
|
3131
3131
|
const el = container()
|
|
3132
3132
|
const Comp = defineComponent(() => {
|
|
3133
3133
|
onMount(() => {
|
|
3134
|
-
throw new Error(
|
|
3134
|
+
throw new Error('onMount error')
|
|
3135
3135
|
})
|
|
3136
|
-
return h(
|
|
3136
|
+
return h('div', null, 'content')
|
|
3137
3137
|
})
|
|
3138
3138
|
// Should not throw
|
|
3139
3139
|
mount(h(Comp, null), el)
|
|
3140
|
-
expect(el.querySelector(
|
|
3140
|
+
expect(el.querySelector('div')?.textContent).toBe('content')
|
|
3141
3141
|
})
|
|
3142
3142
|
|
|
3143
|
-
test(
|
|
3143
|
+
test('onUnmount hook that throws is caught', async () => {
|
|
3144
3144
|
const el = container()
|
|
3145
3145
|
const Comp = defineComponent(() => {
|
|
3146
3146
|
onUnmount(() => {
|
|
3147
|
-
throw new Error(
|
|
3147
|
+
throw new Error('onUnmount error')
|
|
3148
3148
|
})
|
|
3149
|
-
return h(
|
|
3149
|
+
return h('div', null, 'content')
|
|
3150
3150
|
})
|
|
3151
3151
|
const unmount = mount(h(Comp, null), el)
|
|
3152
3152
|
// Should not throw
|
|
@@ -3156,57 +3156,57 @@ describe("mount — error handling branches", () => {
|
|
|
3156
3156
|
|
|
3157
3157
|
// ─── TransitionGroup — unmount cleanup ───────────────────────────────────────
|
|
3158
3158
|
|
|
3159
|
-
describe(
|
|
3160
|
-
test(
|
|
3159
|
+
describe('TransitionGroup — cleanup', () => {
|
|
3160
|
+
test('unmount disposes effect and cleans up entries', async () => {
|
|
3161
3161
|
const el = container()
|
|
3162
3162
|
const items = signal([{ id: 1 }, { id: 2 }])
|
|
3163
3163
|
const unmount = mount(
|
|
3164
3164
|
h(TransitionGroup, {
|
|
3165
|
-
tag:
|
|
3165
|
+
tag: 'div',
|
|
3166
3166
|
items,
|
|
3167
3167
|
keyFn: (item: { id: number }) => item.id,
|
|
3168
|
-
render: (item: { id: number }) => h(
|
|
3168
|
+
render: (item: { id: number }) => h('span', null, String(item.id)),
|
|
3169
3169
|
}),
|
|
3170
3170
|
el,
|
|
3171
3171
|
)
|
|
3172
3172
|
await new Promise<void>((r) => queueMicrotask(r))
|
|
3173
|
-
expect(el.querySelectorAll(
|
|
3173
|
+
expect(el.querySelectorAll('span').length).toBe(2)
|
|
3174
3174
|
unmount()
|
|
3175
|
-
expect(el.innerHTML).toBe(
|
|
3175
|
+
expect(el.innerHTML).toBe('')
|
|
3176
3176
|
})
|
|
3177
3177
|
})
|
|
3178
3178
|
|
|
3179
3179
|
// ─── Error paths (no ErrorBoundary) ──────────────────────────────────────────
|
|
3180
3180
|
|
|
3181
|
-
describe(
|
|
3182
|
-
test(
|
|
3181
|
+
describe('mount — error paths', () => {
|
|
3182
|
+
test('component that throws during setup fires console.error', () => {
|
|
3183
3183
|
const el = container()
|
|
3184
|
-
const spy = vi.spyOn(console,
|
|
3184
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
3185
3185
|
|
|
3186
3186
|
const Broken: ComponentFn = () => {
|
|
3187
|
-
throw new Error(
|
|
3187
|
+
throw new Error('setup boom')
|
|
3188
3188
|
}
|
|
3189
3189
|
mount(h(Broken, null), el)
|
|
3190
3190
|
|
|
3191
3191
|
expect(spy).toHaveBeenCalledWith(
|
|
3192
|
-
expect.stringContaining(
|
|
3192
|
+
expect.stringContaining('threw during setup'),
|
|
3193
3193
|
expect.any(Error),
|
|
3194
3194
|
)
|
|
3195
3195
|
spy.mockRestore()
|
|
3196
3196
|
})
|
|
3197
3197
|
|
|
3198
|
-
test(
|
|
3198
|
+
test('component that throws during render fires console.error', () => {
|
|
3199
3199
|
const el = container()
|
|
3200
|
-
const spy = vi.spyOn(console,
|
|
3200
|
+
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
3201
3201
|
|
|
3202
3202
|
const BrokenChild: ComponentFn = () => {
|
|
3203
|
-
throw new Error(
|
|
3203
|
+
throw new Error('render boom')
|
|
3204
3204
|
}
|
|
3205
3205
|
// Parent returns a child that throws when mounted (render phase)
|
|
3206
3206
|
const Parent: ComponentFn = () => h(BrokenChild, null)
|
|
3207
3207
|
mount(h(Parent, null), el)
|
|
3208
3208
|
|
|
3209
|
-
expect(spy).toHaveBeenCalledWith(expect.stringContaining(
|
|
3209
|
+
expect(spy).toHaveBeenCalledWith(expect.stringContaining('threw during'), expect.any(Error))
|
|
3210
3210
|
spy.mockRestore()
|
|
3211
3211
|
})
|
|
3212
3212
|
})
|