-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.go
More file actions
617 lines (544 loc) · 16.1 KB
/
node_test.go
File metadata and controls
617 lines (544 loc) · 16.1 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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package html
import (
"testing"
i18n "dappco.re/go/i18n"
"slices"
)
func TestRawNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := Raw("hello")
got := node.Render(ctx)
if got != "hello" {
t.Errorf("Raw(\"hello\").Render() = %q, want %q", got, "hello")
}
}
func TestElNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := El("div", Raw("content"))
got := node.Render(ctx)
want := "<div>content</div>"
if got != want {
t.Errorf("El(\"div\", Raw(\"content\")).Render() = %q, want %q", got, want)
}
}
func TestElNode_Nested_Good(t *testing.T) {
ctx := NewContext()
node := El("div", El("span", Raw("inner")))
got := node.Render(ctx)
want := "<div><span>inner</span></div>"
if got != want {
t.Errorf("nested El().Render() = %q, want %q", got, want)
}
}
func TestLayout_DirectElementBlockPath_Good(t *testing.T) {
ctx := NewContext()
got := NewLayout("C").C(El("div", Raw("content"))).Render(ctx)
if !containsText(got, `data-block="C.0"`) {
t.Fatalf("direct element inside layout should receive a block path, got:\n%s", got)
}
}
func TestLayout_EachElementBlockPaths_Good(t *testing.T) {
ctx := NewContext()
got := NewLayout("C").C(
Each([]string{"a", "b"}, func(item string) Node {
return El("span", Raw(item))
}),
).Render(ctx)
if !containsText(got, `data-block="C.0.0"`) {
t.Fatalf("first Each item should receive a block path, got:\n%s", got)
}
if !containsText(got, `data-block="C.0.1"`) {
t.Fatalf("second Each item should receive a block path, got:\n%s", got)
}
}
func TestElNode_MultipleChildren_Good(t *testing.T) {
ctx := NewContext()
node := El("div", Raw("a"), Raw("b"))
got := node.Render(ctx)
want := "<div>ab</div>"
if got != want {
t.Errorf("El with multiple children = %q, want %q", got, want)
}
}
func TestElNode_VoidElement_Good(t *testing.T) {
ctx := NewContext()
node := El("br")
got := node.Render(ctx)
want := "<br>"
if got != want {
t.Errorf("El(\"br\").Render() = %q, want %q", got, want)
}
}
func TestTextNode_Render_Good(t *testing.T) {
ctx := NewContext()
node := Text("hello")
got := node.Render(ctx)
if got != "hello" {
t.Errorf("Text(\"hello\").Render() = %q, want %q", got, "hello")
}
}
func TestTextNode_UsesContextDataForCount_Good(t *testing.T) {
svc, _ := i18n.New()
i18n.SetDefault(svc)
tests := []struct {
name string
key string
data map[string]any
want string
}{
{
name: "capitalised count",
key: "i18n.count.file",
data: map[string]any{"Count": 5},
want: "5 files",
},
{
name: "lowercase count",
key: "i18n.count.file",
data: map[string]any{"count": 1},
want: "1 file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := NewContext()
for k, v := range tt.data {
ctx.Metadata[k] = v
}
got := Text(tt.key).Render(ctx)
if got != tt.want {
t.Fatalf("Text(%q).Render() = %q, want %q", tt.key, got, tt.want)
}
})
}
}
func TestTextNode_Escapes_Good(t *testing.T) {
ctx := NewContext()
node := Text("<script>alert('xss')</script>")
got := node.Render(ctx)
if containsText(got, "<script>") {
t.Errorf("Text node must HTML-escape output, got %q", got)
}
if !containsText(got, "<script>") {
t.Errorf("Text node should contain escaped script tag, got %q", got)
}
}
func TestIfNode_True_Good(t *testing.T) {
ctx := NewContext()
node := If(func(*Context) bool { return true }, Raw("visible"))
got := node.Render(ctx)
if got != "visible" {
t.Errorf("If(true) = %q, want %q", got, "visible")
}
}
func TestIfNode_False_Good(t *testing.T) {
ctx := NewContext()
node := If(func(*Context) bool { return false }, Raw("hidden"))
got := node.Render(ctx)
if got != "" {
t.Errorf("If(false) = %q, want %q", got, "")
}
}
func TestUnlessNode_False_Good(t *testing.T) {
ctx := NewContext()
node := Unless(func(*Context) bool { return false }, Raw("visible"))
got := node.Render(ctx)
if got != "visible" {
t.Errorf("Unless(false) = %q, want %q", got, "visible")
}
}
func TestEntitledNode_Granted_Good(t *testing.T) {
ctx := NewContext()
ctx.Entitlements = func(feature string) bool { return feature == "premium" }
node := Entitled("premium", Raw("premium content"))
got := node.Render(ctx)
if got != "premium content" {
t.Errorf("Entitled(granted) = %q, want %q", got, "premium content")
}
}
func TestEntitledNode_Denied_Bad(t *testing.T) {
ctx := NewContext()
ctx.Entitlements = func(feature string) bool { return false }
node := Entitled("premium", Raw("premium content"))
got := node.Render(ctx)
if got != "" {
t.Errorf("Entitled(denied) = %q, want %q", got, "")
}
}
func TestEntitledNode_NoFunc_Bad(t *testing.T) {
ctx := NewContext()
node := Entitled("premium", Raw("premium content"))
got := node.Render(ctx)
if got != "" {
t.Errorf("Entitled(no func) = %q, want %q (deny by default)", got, "")
}
}
func TestEachNode_Render_Good(t *testing.T) {
ctx := NewContext()
items := []string{"a", "b", "c"}
node := Each(items, func(item string) Node {
return El("li", Raw(item))
})
got := node.Render(ctx)
want := "<li>a</li><li>b</li><li>c</li>"
if got != want {
t.Errorf("Each([a,b,c]) = %q, want %q", got, want)
}
}
func TestEachNode_Empty_Good(t *testing.T) {
ctx := NewContext()
node := Each([]string{}, func(item string) Node {
return El("li", Raw(item))
})
got := node.Render(ctx)
if got != "" {
t.Errorf("Each([]) = %q, want %q", got, "")
}
}
func TestEachNode_NestedLayout_PreservesBlockPath_Good(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("item"))
node := Each([]Node{inner}, func(item Node) Node {
return item
})
got := NewLayout("C").C(node).Render(ctx)
want := `<main role="main" data-block="C"><main role="main" data-block="C.0">item</main></main>`
if got != want {
t.Fatalf("Each nested layout render = %q, want %q", got, want)
}
}
func TestEachNode_MultipleLayouts_GetDistinctPaths_Good(t *testing.T) {
ctx := NewContext()
first := NewLayout("C").C(Raw("one"))
second := NewLayout("C").C(Raw("two"))
node := Each([]Node{first, second}, func(item Node) Node {
return item
})
got := NewLayout("C").C(node).Render(ctx)
if !containsText(got, `data-block="C.0.0"`) {
t.Fatalf("first layout item should receive a distinct block path, got:\n%s", got)
}
if !containsText(got, `data-block="C.0.1"`) {
t.Fatalf("second layout item should receive a distinct block path, got:\n%s", got)
}
}
func TestEachSeq_NestedLayout_PreservesBlockPath_Good(t *testing.T) {
ctx := NewContext()
inner := NewLayout("C").C(Raw("item"))
node := EachSeq(slices.Values([]Node{inner}), func(item Node) Node {
return item
})
got := NewLayout("C").C(node).Render(ctx)
want := `<main role="main" data-block="C"><main role="main" data-block="C.0">item</main></main>`
if got != want {
t.Fatalf("EachSeq nested layout render = %q, want %q", got, want)
}
}
func TestEachSeq_MultipleLayouts_GetDistinctPaths_Good(t *testing.T) {
ctx := NewContext()
first := NewLayout("C").C(Raw("one"))
second := NewLayout("C").C(Raw("two"))
node := EachSeq(slices.Values([]Node{first, second}), func(item Node) Node {
return item
})
got := NewLayout("C").C(node).Render(ctx)
if !containsText(got, `data-block="C.0.0"`) {
t.Fatalf("first layout item should receive a distinct block path, got:\n%s", got)
}
if !containsText(got, `data-block="C.0.1"`) {
t.Fatalf("second layout item should receive a distinct block path, got:\n%s", got)
}
}
func TestElNode_Attr_Good(t *testing.T) {
ctx := NewContext()
node := Attr(El("div", Raw("content")), "class", "container")
got := node.Render(ctx)
want := `<div class="container">content</div>`
if got != want {
t.Errorf("Attr() = %q, want %q", got, want)
}
}
func TestElNode_AttrRecursiveThroughEachSeq_Good(t *testing.T) {
ctx := NewContext()
node := Attr(
EachSeq(slices.Values([]string{"a", "b"}), func(item string) Node {
return El("span", Raw(item))
}),
"data-kind",
"item",
)
got := NewLayout("C").C(node).Render(ctx)
if count := countText(got, `data-kind="item"`); count != 2 {
t.Fatalf("Attr through EachSeq should apply to every item, got %d in:\n%s", count, got)
}
}
func TestElNode_AttrRecursiveThroughSwitch_Good(t *testing.T) {
ctx := NewContext()
node := Attr(
Switch(
func(*Context) string { return "match" },
map[string]Node{
"match": El("span", Raw("visible")),
"miss": El("span", Raw("hidden")),
},
),
"data-state",
"selected",
)
got := node.Render(ctx)
if !containsText(got, `data-state="selected"`) {
t.Fatalf("Attr through Switch should reach the selected case, got:\n%s", got)
}
}
func TestAccessibilityHelpers_Good(t *testing.T) {
ctx := NewContext()
button := Role(
AriaLabel(
TabIndex(
AutoFocus(El("button", Raw("save"))),
3,
),
"Save changes",
),
"button",
)
got := button.Render(ctx)
for _, want := range []string{
`aria-label="Save changes"`,
`autofocus="autofocus"`,
`role="button"`,
`tabindex="3"`,
">save</button>",
} {
if !containsText(got, want) {
t.Fatalf("accessibility helpers missing %q in:\n%s", want, got)
}
}
img := AltText(El("img"), "Profile photo")
if got := img.Render(ctx); got != `<img alt="Profile photo">` {
t.Fatalf("AltText() = %q, want %q", got, `<img alt="Profile photo">`)
}
}
func TestSwitchNode_Good(t *testing.T) {
ctx := NewContext()
ctx.Locale = "en-GB"
node := Switch(
func(ctx *Context) string { return ctx.Locale },
map[string]Node{
"en-GB": Raw("hello"),
"fr-FR": Raw("bonjour"),
},
)
if got := node.Render(ctx); got != "hello" {
t.Fatalf("Switch matched case = %q, want %q", got, "hello")
}
if got := Switch(func(*Context) string { return "de-DE" }, map[string]Node{"en-GB": Raw("hello")}).Render(ctx); got != "" {
t.Fatalf("Switch missing case = %q, want empty", got)
}
}
func TestElNode_AttrEscaping_Good(t *testing.T) {
ctx := NewContext()
node := Attr(El("img"), "alt", `he said "hello"`)
got := node.Render(ctx)
if !containsText(got, `alt="he said "hello""`) {
t.Errorf("Attr should escape attribute values, got %q", got)
}
}
func TestAriaLabel_Good(t *testing.T) {
node := AriaLabel(El("button", Raw("save")), "Save changes")
got := node.Render(NewContext())
want := `<button aria-label="Save changes">save</button>`
if got != want {
t.Errorf("AriaLabel() = %q, want %q", got, want)
}
}
func TestAltText_Good(t *testing.T) {
node := AltText(El("img"), "Profile photo")
got := node.Render(NewContext())
want := `<img alt="Profile photo">`
if got != want {
t.Errorf("AltText() = %q, want %q", got, want)
}
}
func TestTabIndex_Good(t *testing.T) {
node := TabIndex(El("button", Raw("save")), 0)
got := node.Render(NewContext())
want := `<button tabindex="0">save</button>`
if got != want {
t.Errorf("TabIndex() = %q, want %q", got, want)
}
}
func TestAutoFocus_Good(t *testing.T) {
node := AutoFocus(El("input"))
got := node.Render(NewContext())
want := `<input autofocus="autofocus">`
if got != want {
t.Errorf("AutoFocus() = %q, want %q", got, want)
}
}
func TestRole_Good(t *testing.T) {
node := Role(El("nav", Raw("links")), "navigation")
got := node.Render(NewContext())
want := `<nav role="navigation">links</nav>`
if got != want {
t.Errorf("Role() = %q, want %q", got, want)
}
}
func TestElNode_MultipleAttrs_Good(t *testing.T) {
ctx := NewContext()
node := Attr(Attr(El("a", Raw("link")), "href", "/home"), "class", "nav")
got := node.Render(ctx)
if !containsText(got, `class="nav"`) || !containsText(got, `href="/home"`) {
t.Errorf("multiple Attr() calls should stack, got %q", got)
}
}
func TestAttr_NonElement_Ugly(t *testing.T) {
node := Attr(Raw("text"), "class", "x")
got := node.Render(NewContext())
if got != "text" {
t.Errorf("Attr on non-element should return unchanged, got %q", got)
}
}
func TestAttr_TypedNilWrappers_Ugly(t *testing.T) {
tests := []struct {
name string
node Node
}{
{name: "layout", node: (*Layout)(nil)},
{name: "responsive", node: (*Responsive)(nil)},
{name: "if", node: (*ifNode)(nil)},
{name: "unless", node: (*unlessNode)(nil)},
{name: "entitled", node: (*entitledNode)(nil)},
{name: "switch", node: (*switchNode)(nil)},
{name: "each", node: (*eachNode[string])(nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Attr(tt.node, "data-test", "x"); got != nil {
t.Fatalf("Attr on typed nil %s should return nil, got %#v", tt.name, got)
}
})
}
}
func TestUnlessNode_True_Good(t *testing.T) {
ctx := NewContext()
node := Unless(func(*Context) bool { return true }, Raw("hidden"))
got := node.Render(ctx)
if got != "" {
t.Errorf("Unless(true) = %q, want %q", got, "")
}
}
func TestAttr_ThroughIfNode_Good(t *testing.T) {
ctx := NewContext()
inner := El("div", Raw("content"))
node := If(func(*Context) bool { return true }, inner)
Attr(node, "class", "wrapped")
got := node.Render(ctx)
want := `<div class="wrapped">content</div>`
if got != want {
t.Errorf("Attr through If = %q, want %q", got, want)
}
}
func TestAttr_ThroughUnlessNode_Good(t *testing.T) {
ctx := NewContext()
inner := El("div", Raw("content"))
node := Unless(func(*Context) bool { return false }, inner)
Attr(node, "id", "test")
got := node.Render(ctx)
want := `<div id="test">content</div>`
if got != want {
t.Errorf("Attr through Unless = %q, want %q", got, want)
}
}
func TestAttr_ThroughEntitledNode_Good(t *testing.T) {
ctx := NewContext()
ctx.Entitlements = func(string) bool { return true }
inner := El("div", Raw("content"))
node := Entitled("feature", inner)
Attr(node, "data-feat", "on")
got := node.Render(ctx)
want := `<div data-feat="on">content</div>`
if got != want {
t.Errorf("Attr through Entitled = %q, want %q", got, want)
}
}
func TestAttr_ThroughSwitchNode_Good(t *testing.T) {
ctx := NewContext()
inner := El("div", Raw("content"))
node := Switch(func(*Context) string { return "match" }, map[string]Node{
"match": inner,
"miss": El("span", Raw("unused")),
})
Attr(node, "data-state", "active")
got := node.Render(ctx)
want := `<div data-state="active">content</div>`
if got != want {
t.Errorf("Attr through Switch = %q, want %q", got, want)
}
}
func TestAttr_ThroughLayout_Good(t *testing.T) {
ctx := NewContext()
layout := NewLayout("C").C(El("div", Raw("content")))
Attr(layout, "class", "page")
got := layout.Render(ctx)
want := `<main role="main" data-block="C"><div class="page" data-block="C.0">content</div></main>`
if got != want {
t.Errorf("Attr through Layout = %q, want %q", got, want)
}
}
func TestAttr_ThroughResponsive_Good(t *testing.T) {
ctx := NewContext()
resp := NewResponsive().Variant("mobile", NewLayout("C").C(El("div", Raw("content"))))
Attr(resp, "data-kind", "page")
got := resp.Render(ctx)
want := `<div data-variant="mobile"><main role="main" data-block="C"><div data-block="C.0" data-kind="page">content</div></main></div>`
if got != want {
t.Errorf("Attr through Responsive = %q, want %q", got, want)
}
}
func TestAttr_ThroughEachNode_Good(t *testing.T) {
ctx := NewContext()
node := Each([]string{"a", "b"}, func(item string) Node {
return El("span", Raw(item))
})
Attr(node, "class", "item")
got := node.Render(ctx)
want := `<span class="item">a</span><span class="item">b</span>`
if got != want {
t.Errorf("Attr through Each = %q, want %q", got, want)
}
}
func TestAttr_ThroughEachSeqNode_Good(t *testing.T) {
ctx := NewContext()
node := EachSeq(slices.Values([]string{"a", "b"}), func(item string) Node {
return El("span", Raw(item))
})
Attr(node, "data-kind", "item")
got := node.Render(ctx)
want := `<span data-kind="item">a</span><span data-kind="item">b</span>`
if got != want {
t.Errorf("Attr through EachSeq = %q, want %q", got, want)
}
}
func TestTextNode_WithService_Good(t *testing.T) {
svc, _ := i18n.New()
ctx := NewContextWithService(svc)
node := Text("hello")
got := node.Render(ctx)
if got != "hello" {
t.Errorf("Text with service context = %q, want %q", got, "hello")
}
}
func TestSwitchNode_SelectsMatch_Good(t *testing.T) {
ctx := NewContext()
cases := map[string]Node{
"dark": Raw("dark theme"),
"light": Raw("light theme"),
}
node := Switch(func(*Context) string { return "dark" }, cases)
got := node.Render(ctx)
want := "dark theme"
if got != want {
t.Errorf("Switch(\"dark\") = %q, want %q", got, want)
}
}