-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents_test.go
More file actions
113 lines (106 loc) · 3.35 KB
/
components_test.go
File metadata and controls
113 lines (106 loc) · 3.35 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
package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
)
func TestShortSummary(t *testing.T) {
cases := []struct{ in, want string }{
{"Single sentence.", "Single sentence."},
{"First sentence. Second sentence.", "First sentence."},
{"No period yet", "No period yet"},
{"Multi-line\nsecond line should not appear.", "Multi-line"},
{"Bang! Yes.", "Bang!"},
{"Question? Followup.", "Question?"},
}
for _, c := range cases {
if got := shortSummary(c.in); got != c.want {
t.Errorf("shortSummary(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestPrintComponentList(t *testing.T) {
items := []libraryItem{
{Name: "Callout", Summary: "Coloured aside.", Category: "emphasis"},
{Name: "TLDR", Summary: "Summary box.", Category: "layout"},
{Name: "KPI", Summary: "Big number.", Category: "data"},
}
var buf bytes.Buffer
printComponentList(&buf, items)
out := buf.String()
// Headers ALL CAPS
for _, cat := range []string{"LAYOUT", "EMPHASIS", "DATA"} {
if !strings.Contains(out, cat) {
t.Errorf("missing category header %q in:\n%s", cat, out)
}
}
// Items present
for _, name := range []string{"Callout", "TLDR", "KPI"} {
if !strings.Contains(out, name) {
t.Errorf("missing component name %q in:\n%s", name, out)
}
}
// Order: layout before emphasis (per categoryOrder)
if i, j := strings.Index(out, "LAYOUT"), strings.Index(out, "EMPHASIS"); i == -1 || j == -1 || i > j {
t.Errorf("category order wrong: layout=%d, emphasis=%d\n%s", i, j, out)
}
// Footer count
if !strings.Contains(out, "3 components") {
t.Errorf("missing count footer in:\n%s", out)
}
}
func TestPrintComponentDetail(t *testing.T) {
spec, _ := json.Marshal(componentSpec{
Props: []componentProp{
{Name: "type", Type: `"info" | "warning"`, Default: `"info"`, Description: "Visual tone."},
{Name: "title", Type: "string", Optional: true, Description: "Bold title above body."},
{Name: "children", Type: "ReactNode"}, // required, no default, no description
},
})
item := libraryItem{
Name: "Callout",
Summary: "Coloured aside.",
Category: "emphasis",
Spec: spec,
Examples: []string{`<Callout type="warning">Don't merge Fridays.</Callout>`},
}
var buf bytes.Buffer
printComponentDetail(&buf, item)
out := buf.String()
// Header
if !strings.Contains(out, "Callout · emphasis") {
t.Errorf("missing header:\n%s", out)
}
// Summary
if !strings.Contains(out, "Coloured aside.") {
t.Errorf("missing summary:\n%s", out)
}
// Section labels
if !strings.Contains(out, "PROPS") || !strings.Contains(out, "EXAMPLE") {
t.Errorf("missing section labels:\n%s", out)
}
// Default tagged
if !strings.Contains(out, `default "info"`) {
t.Errorf("missing default annotation:\n%s", out)
}
// Required tagged
if !strings.Contains(out, "(required)") {
t.Errorf("missing required annotation:\n%s", out)
}
// Example body shows up indented
if !strings.Contains(out, ` <Callout type="warning">`) {
t.Errorf("example not indented:\n%s", out)
}
}
func TestPrintComponentList_unknownCategoryStillShows(t *testing.T) {
items := []libraryItem{
{Name: "Wonky", Summary: "Future stuff.", Category: "future-things"},
}
var buf bytes.Buffer
printComponentList(&buf, items)
out := buf.String()
if !strings.Contains(out, "FUTURE-THINGS") || !strings.Contains(out, "Wonky") {
t.Errorf("unknown category should still render:\n%s", out)
}
}