Skip to content

Commit

Permalink
chore: add test for RenderScriptItems
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Oct 3, 2023
1 parent 8543fac commit a66be15
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ go test -cover ./... -args -test.gocoverdir="$PWD/coverage/unit"
go tool covdata percent -i=./coverage/generate,./coverage/unit
# Generate a text coverage profile for tooling to use.
go tool covdata textfmt -i=./coverage/generate,./coverage/unit -o coverage.out
# Print total
go tool cover -func coverage.out | grep total
```

### benchmark
Expand Down
79 changes: 79 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,82 @@ func TestHandler(t *testing.T) {
})
}
}

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 type="text/javascript">` + 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 type="text/javascript">` + 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 type="text/javascript">` + 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 {
tt := tt
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)
}
})
}
}

0 comments on commit a66be15

Please sign in to comment.