Repository navigation
Expand file tree
/
Copy pathscripttemplate_test.go
More file actions
189 lines (179 loc) · 5.82 KB
/
Copy pathscripttemplate_test.go
File metadata and controls
189 lines (179 loc) · 5.82 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
package templ_test
import (
"bytes"
"context"
"io"
"testing"
"github.com/a-h/templ"
"github.com/google/go-cmp/cmp"
)
func TestRenderScriptItems(t *testing.T) {
s1 := templ.ComponentScript{
Name: "s1",
Function: "function s1() { return 'hello1'; }",
}
s2 := templ.ComponentScript{
Name: "s2",
Function: "function s2() { return 'hello2'; }",
}
tests := []struct {
name string
toIgnore []templ.ComponentScript
toRender []templ.ComponentScript
expected string
}{
{
name: "if none are ignored, everything is rendered",
toIgnore: nil,
toRender: []templ.ComponentScript{s1, s2},
expected: `<script>` + s1.Function + s2.Function + `</script>`,
},
{
name: "if something outside the expected is ignored, if has no effect",
toIgnore: []templ.ComponentScript{
{
Name: "s3",
Function: "function s3() { return 'hello3'; }",
},
},
toRender: []templ.ComponentScript{s1, s2},
expected: `<script>` + s1.Function + s2.Function + `</script>`,
},
{
name: "if one is ignored, it's not rendered",
toIgnore: []templ.ComponentScript{s1},
toRender: []templ.ComponentScript{s1, s2},
expected: `<script>` + s2.Function + `</script>`,
},
{
name: "if all are ignored, not even style tags are rendered",
toIgnore: []templ.ComponentScript{
s1,
s2,
{
Name: "s3",
Function: "function s3() { return 'hello3'; }",
},
},
toRender: []templ.ComponentScript{s1, s2},
expected: ``,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
b := new(bytes.Buffer)
// Render twice, reusing the same context so that there's a memory of which classes have been rendered.
ctx = templ.InitializeContext(ctx)
err := templ.RenderScriptItems(ctx, b, tt.toIgnore...)
if err != nil {
t.Fatalf("failed to render initial scripts: %v", err)
}
// Now render again to check that only the expected classes were rendered.
b.Reset()
err = templ.RenderScriptItems(ctx, b, tt.toRender...)
if err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
if diff := cmp.Diff(tt.expected, b.String()); diff != "" {
t.Error(diff)
}
})
}
}
func TestRenderScriptItemsWithNonce(t *testing.T) {
ctx := templ.WithNonce(context.Background(), `test"nonce`)
b := new(bytes.Buffer)
s := templ.ComponentScript{
Name: "s1",
Function: "function s1() { return 'hello1'; }",
}
err := templ.RenderScriptItems(ctx, b, s)
if err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
// Should include the escaped nonce attribute on the <script> tag.
expected := `<script nonce="test"nonce">` + s.Function + `</script>`
if diff := cmp.Diff(expected, b.String()); diff != "" {
t.Error(diff)
}
t.Run("rendering a script call with a nonce requires one allocation", func(t *testing.T) {
// Nonces are base64, so they don't need escaping, which would allocate.
ctx := templ.WithNonce(context.Background(), "dGVzdG5vbmNl")
call := templ.JSFuncCall("alert", 1)
actualAllocs := testing.AllocsPerRun(4, func() {
if err := call.Render(ctx, io.Discard); err != nil {
t.Fatalf("failed to render script call: %v", err)
}
})
if actualAllocs > 1 {
t.Errorf("expected 1 alloc, got %v", actualAllocs)
}
})
}
func TestRenderScriptItemsDeduplication(t *testing.T) {
s1 := templ.ComponentScript{
Name: "s1",
Function: "function s1() { return 'hello1'; }",
}
empty := templ.ComponentScript{
Name: "empty",
}
t.Run("scripts without a function don't render a script element", func(t *testing.T) {
ctx := templ.InitializeContext(context.Background())
b := new(bytes.Buffer)
if err := templ.RenderScriptItems(ctx, b, empty); err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
if b.Len() != 0 {
t.Errorf("expected no output, got %q", b.String())
}
})
t.Run("scripts without a function are skipped", func(t *testing.T) {
ctx := templ.InitializeContext(context.Background())
b := new(bytes.Buffer)
if err := templ.RenderScriptItems(ctx, b, empty, s1, empty); err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
if diff := cmp.Diff(`<script>`+s1.Function+`</script>`, b.String()); diff != "" {
t.Error(diff)
}
})
t.Run("a CSS class and a script with the same name are both rendered", func(t *testing.T) {
ctx := templ.InitializeContext(context.Background())
b := new(bytes.Buffer)
class := templ.ComponentCSSClass{ID: "same", Class: ".same{color:red}"}
script := templ.ComponentScript{Name: "same", Function: "function same() {}"}
if err := templ.RenderCSSItems(ctx, b, class); err != nil {
t.Fatalf("failed to render CSS: %v", err)
}
if err := templ.RenderScriptItems(ctx, b, script); err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
expected := `<style type="text/css">` + string(class.Class) + `</style><script>` + script.Function + `</script>`
if diff := cmp.Diff(expected, b.String()); diff != "" {
t.Error(diff)
}
})
t.Run("scripts that have already been rendered do not require allocations", func(t *testing.T) {
ctx := templ.InitializeContext(context.Background())
if err := templ.RenderScriptItems(ctx, io.Discard, s1, empty); err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
actualAllocs := testing.AllocsPerRun(4, func() {
if err := templ.RenderScriptItems(ctx, io.Discard, s1, empty); err != nil {
t.Fatalf("failed to render scripts: %v", err)
}
})
if actualAllocs > 0 {
t.Errorf("expected no allocs, got %v", actualAllocs)
}
})
}
func TestJSExpression(t *testing.T) {
expected := "myJSFunction(\"StringValue\",123,event,1 + 2)"
actual := templ.SafeScriptInline("myJSFunction", "StringValue", 123, templ.JSExpression("event"), templ.JSExpression("1 + 2"))
if actual != expected {
t.Fatalf("TestJSExpression: expected %q, got %q", expected, actual)
}
}