Repository navigation
Expand file tree
/
Copy pathlifecycle_wasm_test.go
More file actions
490 lines (417 loc) · 12.3 KB
/
Copy pathlifecycle_wasm_test.go
File metadata and controls
490 lines (417 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//go:build wasm
package dom
import (
"syscall/js"
"testing"
)
func TestMain(m *testing.M) {
// wasmbrowsertest provides a minimal HTML page with no #app div.
// Create one in <body> so every test can Render("app", ...).
app := js.Global().Get("document").Call("createElement", "div")
app.Set("id", "app")
js.Global().Get("document").Get("body").Call("appendChild", app)
m.Run()
}
type counterComp struct {
Element
count *SignalString
}
func (c *counterComp) Init(ctx Ctx) {
c.count = NewString("0")
}
func (c *counterComp) Render() *Element {
return NewElement("div").ID("counter-div").
Child(
NewElement("span").ID("count-val").BindText(c.count),
NewElement("button").ID("inc-btn").OnClick(func(e Event) {
c.count.Update(func(v string) string {
if v == "0" {
return "1"
}
return "2"
})
}),
)
}
func TestCounter(t *testing.T) {
c := &counterComp{}
Render("app", c)
val, _ := Get("count-val")
if val.(*elementWasm).val.Get("textContent").String() != "0" {
t.Errorf("Expected 0, got %s", val.(*elementWasm).val.Get("textContent").String())
}
btn, _ := Get("inc-btn")
btn.(*elementWasm).val.Call("click")
if val.(*elementWasm).val.Get("textContent").String() != "1" {
t.Errorf("Expected 1, got %s", val.(*elementWasm).val.Get("textContent").String())
}
// Verify node identity
oldVal := val.(*elementWasm).val
btn.(*elementWasm).val.Call("click")
if val.(*elementWasm).val.Get("textContent").String() != "2" {
t.Errorf("Expected 2, got %s", val.(*elementWasm).val.Get("textContent").String())
}
if !val.(*elementWasm).val.Equal(oldVal) {
t.Error("Node identity lost after signal update")
}
}
type lifecycleComp struct {
Element
inited int
cleaned bool
}
func (c *lifecycleComp) Init(ctx Ctx) {
c.inited++
ctx.OnCleanup(func() {
c.cleaned = true
})
}
func (c *lifecycleComp) Render() *Element {
return NewElement("div").Text("lifecycle")
}
func TestLifecycle(t *testing.T) {
c := &lifecycleComp{}
Render("app", c)
if c.inited != 1 {
t.Errorf("Expected inited 1, got %d", c.inited)
}
Render("app", NewElement("div").Text("replaced"))
if !c.cleaned {
t.Error("OnCleanup not called")
}
}
func TestShow(t *testing.T) {
cond := NewBool(false)
calls := 0
s := Show(cond, func() *Element {
calls++
return NewElement("span").ID("shown").Text("visible")
})
Render("app", s)
// Unmounted while false — lazy mounting
if _, ok := Get("shown"); ok {
t.Fatal("content must NOT be mounted while false")
}
if calls != 0 {
t.Fatalf("expected 0 builder calls while false, got %d", calls)
}
cond.Set(true)
if _, ok := Get("shown"); !ok {
t.Fatal("content must be mounted after Set(true)")
}
if calls != 1 {
t.Fatalf("expected 1 builder call after Set(true), got %d", calls)
}
cond.Set(false)
if _, ok := Get("shown"); ok {
t.Fatal("content must be unmounted after Set(false)")
}
}
func TestTwoWayInput(t *testing.T) {
s := NewString("initial")
input := NewElement("input").ID("io").Bind(s)
Render("app", input)
ref, _ := Get("io")
if ref.Value() != "initial" {
t.Errorf("Expected initial, got %s", ref.Value())
}
ref.(*elementWasm).val.Set("value", "changed")
ref.(*elementWasm).val.Call("dispatchEvent", js.Global().Get("Event").New("input"))
if s.Get() != "changed" {
t.Errorf("Signal not updated from input, got %s", s.Get())
}
s.Set("from-signal")
if ref.Value() != "from-signal" {
t.Errorf("Input not updated from signal, got %s", ref.Value())
}
}
func TestBindChildren(t *testing.T) {
nodes := NewNodes(
NewElement("div").ID("n1").Text("one"),
NewElement("div").ID("n2").Text("two"),
)
list := NewElement("div").ID("list").BindChildren(nodes)
Render("app", list)
if _, ok := Get("n1"); !ok {
t.Error("n1 missing")
}
if _, ok := Get("n2"); !ok {
t.Error("n2 missing")
}
n1ref, _ := Get("n1")
n1val := n1ref.(*elementWasm).val
nodes.Set([]*Element{
NewElement("div").ID("n2").Text("two"),
NewElement("div").ID("n1").Text("one"),
})
if !n1ref.(*elementWasm).val.Equal(n1val) {
t.Error("Node identity lost after reorder")
}
// Verify order in DOM
parent, _ := Get("list")
first := parent.(*elementWasm).val.Get("children").Call("item", 0)
if first.Get("id").String() != "n2" {
t.Errorf("Expected n2 as first child, got %s", first.Get("id").String())
}
}
// TestBindChildrenReplacesUnkeyedFirstRows: rows without a key or id drawn at
// the first render must be replaced by the next Set, not kept beside the new
// rows. Before the fix they carried no reconcile mark, so the update never
// removed them (a select showed its prompt option twice).
func TestBindChildrenReplacesUnkeyedFirstRows(t *testing.T) {
rows := NewNodes(NewElement("option").Text("prompt"))
list := NewElement("select").ID("unkeyed-select").BindChildren(rows)
Render("app", list)
rows.Set([]*Element{NewElement("option").Text("prompt"), NewElement("option").Text("one")})
ref, ok := Get("unkeyed-select")
if !ok {
t.Fatal("unkeyed-select missing")
}
if n := ref.(*elementWasm).val.Get("children").Get("length").Int(); n != 2 {
t.Errorf("select has %d options after Set, want 2 (the first-render row must be replaced)", n)
}
}
// TestBindChildrenInitialRowBindings guards the fix for rows present in a
// BindChildren signal at FIRST render: they are serialized straight into the
// parent's HTML and never pass through reconcileChildren, so their own nested
// bindings must be wired at mount. Before the fix such a row appeared but never
// reacted — a later signal change did not patch it.
func TestBindChildrenInitialRowBindings(t *testing.T) {
on := NewBool(false)
rows := NewNodes(
NewElement("li").ID("wrow1").BindClass("active", on).Text("row"),
)
list := NewElement("ul").ID("wrows").BindChildren(rows)
Render("app", list)
ref, ok := Get("wrow1")
if !ok {
t.Fatal("wrow1 missing at first render")
}
classList := ref.(*elementWasm).val.Get("classList")
if classList.Call("contains", "active").Bool() {
t.Fatal("wrow1 must not carry 'active' before the signal is set")
}
on.Set(true)
if !classList.Call("contains", "active").Bool() {
t.Error("BindClass on a first-render BindChildren row did not react to the signal")
}
on.Set(false)
if classList.Call("contains", "active").Bool() {
t.Error("BindClass on a first-render BindChildren row did not clear on the signal")
}
}
type orderChildComp struct {
Element
mounted bool
}
func (c *orderChildComp) Render() *Element {
return NewElement("span").ID("order-child")
}
func (c *orderChildComp) Mounted() {
c.mounted = true
// Verify it can Get itself
if _, ok := Get("order-child"); !ok {
panic("order-child element not in DOM in orderChildComp.Mounted")
}
}
type orderParentComp struct {
Element
child *orderChildComp
childMountedAt bool
mounted bool
ownElementFound bool
}
func (p *orderParentComp) Init(ctx Ctx) {
p.child = &orderChildComp{}
}
func (p *orderParentComp) Children() []Component {
return []Component{p.child}
}
func (p *orderParentComp) Render() *Element {
return NewElement("div").ID("order-parent").Child(p.child)
}
func (p *orderParentComp) Mounted() {
p.mounted = true
p.childMountedAt = p.child.mounted
if _, ok := Get("order-parent"); ok {
p.ownElementFound = true
}
}
func TestMountedOrderingAndGet(t *testing.T) {
parent := &orderParentComp{}
Render("app", parent)
if !parent.mounted {
t.Error("parent Mounted not called")
}
if !parent.child.mounted {
t.Error("child Mounted not called")
}
if !parent.childMountedAt {
t.Error("parent Mounted called before child Mounted")
}
if !parent.ownElementFound {
t.Error("parent could not Get itself inside Mounted")
}
}
type rootMountedComp struct {
Element
mountedCount int
}
func (c *rootMountedComp) Render() *Element {
return NewElement("div").ID("root-mounted-elem")
}
func (c *rootMountedComp) Mounted() {
c.mountedCount++
}
func TestMountedRootAndAppend(t *testing.T) {
c1 := &rootMountedComp{}
Render("app", c1)
if c1.mountedCount != 1 {
t.Errorf("Expected mountedCount 1 on Render, got %d", c1.mountedCount)
}
c2 := &rootMountedComp{}
Append("app", c2)
if c2.mountedCount != 1 {
t.Errorf("Expected mountedCount 1 on Append, got %d", c2.mountedCount)
}
}
type scrollableItem struct {
Element
}
func (s *scrollableItem) Render() *Element {
return NewElement("div").ID("item-to-scroll").
StyleProp("width", "200px").
StyleProp("height", "100px").
StyleProp("display", "inline-block")
}
type deckComp struct {
Element
child *scrollableItem
mountedCalled bool
childFoundAtMnt bool
}
func (d *deckComp) Init(ctx Ctx) {
d.child = &scrollableItem{}
}
func (d *deckComp) Children() []Component {
return []Component{d.child}
}
func (d *deckComp) Render() *Element {
return NewElement("div").ID("deck-scroller").
StyleProp("width", "100px").
StyleProp("height", "100px").
StyleProp("overflow-x", "auto").
StyleProp("white-space", "nowrap").
Child(
NewElement("div").
StyleProp("width", "50px").
StyleProp("height", "100px").
StyleProp("display", "inline-block"),
d.child,
)
}
func (d *deckComp) Mounted() {
d.mountedCalled = true
if el, ok := Get("item-to-scroll"); ok {
d.childFoundAtMnt = true
el.ScrollIntoView()
}
}
func TestMountedScrollableConsumer(t *testing.T) {
c := &deckComp{}
Render("app", c)
if !c.mountedCalled {
t.Error("deckComp Mounted was not called")
}
if !c.childFoundAtMnt {
t.Error("item-to-scroll was not found in DOM during deckComp.Mounted")
}
// Verify scroller exists and can scroll
scroller, ok := Get("deck-scroller")
if !ok {
t.Fatal("deck-scroller not found")
}
if !scroller.ScrollsX() {
t.Log("Warning: deck-scroller.ScrollsX() is false (expected in some headless environments or without style sheet support, but scroller is successfully wired)")
}
}
// TestScrollOptionsBehaviorDiffers is the precise, low-level proof
// ScrollIntoViewInstant exists for: scrollOptions("smooth") and
// scrollOptions("instant") actually differ, and neither silently produces
// the wrong one. scrollOptions is unexported, so this lives in the package
// root (package dom), not dom/tests — see dom/AGENTS.md's Testing section.
func TestScrollOptionsBehaviorDiffers(t *testing.T) {
Render("app", &scrollableItem{})
ref, ok := Get("item-to-scroll")
if !ok {
t.Fatal("item-to-scroll not found")
}
e, ok := ref.(*elementWasm)
if !ok {
t.Fatal("Get did not return *elementWasm")
}
smooth := e.scrollOptions("smooth")
if got := smooth.Get("behavior").String(); got != "smooth" {
t.Errorf("scrollOptions(smooth).behavior = %q, want smooth", got)
}
instant := e.scrollOptions("instant")
if got := instant.Get("behavior").String(); got != "instant" {
t.Errorf("scrollOptions(instant).behavior = %q, want instant", got)
}
for _, opts := range []js.Value{smooth, instant} {
if got := opts.Get("inline").String(); got != "start" {
t.Errorf("inline = %q, want start", got)
}
if got := opts.Get("block").String(); got != "nearest" {
t.Errorf("block = %q, want nearest", got)
}
}
}
// deckCompInstant mirrors deckComp exactly, calling ScrollIntoViewInstant
// instead of ScrollIntoView — compile+call-shape coverage through the
// public method, same spirit as TestMountedScrollableConsumer.
// TestScrollOptionsBehaviorDiffers above is what proves the actual
// behavior difference.
type deckCompInstant struct {
Element
child *scrollableItem
mountedCalled bool
childFoundAtMnt bool
}
func (d *deckCompInstant) Init(ctx Ctx) {
d.child = &scrollableItem{}
}
func (d *deckCompInstant) Children() []Component {
return []Component{d.child}
}
func (d *deckCompInstant) Render() *Element {
return NewElement("div").ID("deck-scroller-instant").
StyleProp("width", "100px").
StyleProp("height", "100px").
StyleProp("overflow-x", "auto").
StyleProp("white-space", "nowrap").
Child(
NewElement("div").
StyleProp("width", "50px").
StyleProp("height", "100px").
StyleProp("display", "inline-block"),
d.child,
)
}
func (d *deckCompInstant) Mounted() {
d.mountedCalled = true
if el, ok := Get("item-to-scroll"); ok {
d.childFoundAtMnt = true
el.ScrollIntoViewInstant()
}
}
func TestMountedScrollableConsumerInstant(t *testing.T) {
c := &deckCompInstant{}
Render("app", c)
if !c.mountedCalled {
t.Error("deckCompInstant Mounted was not called")
}
if !c.childFoundAtMnt {
t.Error("item-to-scroll was not found in DOM during deckCompInstant.Mounted")
}
}