-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
join_test.go
81 lines (75 loc) · 1.94 KB
/
join_test.go
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
package templ_test
import (
"bytes"
"context"
"errors"
"io"
"testing"
"github.com/a-h/templ"
"github.com/google/go-cmp/cmp"
)
func TestJoin(t *testing.T) {
compErr := errors.New("component error")
hello := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
if _, err := io.WriteString(w, "Hello"); err != nil {
t.Fatalf("failed to write string: %v", err)
}
return nil
})
world := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
if _, err := io.WriteString(w, "World"); err != nil {
t.Fatalf("failed to write string: %v", err)
}
return nil
})
err := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
return compErr
})
tests := []struct {
name string
input []templ.Component
expectedOutput string
expectedErr error
}{
{
name: "a nil slice of components produces no output",
input: nil,
expectedOutput: "",
},
{
name: "an empty list of components produces no output",
input: []templ.Component{},
expectedOutput: "",
},
{
name: "components are rendered in order",
input: []templ.Component{hello, world},
expectedOutput: "HelloWorld",
},
{
name: "components are rendered in order, and errors returned",
input: []templ.Component{hello, err},
expectedOutput: "Hello",
expectedErr: compErr,
},
{
name: "no further components are rendered after an error",
input: []templ.Component{err, hello},
expectedOutput: "",
expectedErr: compErr,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := templ.Join(tt.input...)
b := new(bytes.Buffer)
err := got.Render(context.Background(), b)
if err != tt.expectedErr {
t.Fatalf("failed to render component: %v", err)
}
if diff := cmp.Diff(tt.expectedOutput, b.String()); diff != "" {
t.Error(diff)
}
})
}
}