-
Notifications
You must be signed in to change notification settings - Fork 9
/
reactssr_test.go
73 lines (67 loc) · 1.62 KB
/
reactssr_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
package reactssr_test
import (
"fmt"
"regexp"
"testing"
"github.com/tmc/reactssr"
)
func Example_newServerSideRenderer() {
r, err := reactssr.NewServerSideRenderer("./testdata/test-app-1/build/out.js")
if err != nil {
panic(err)
}
output, err := r.Render()
if err != nil {
panic(err)
}
untilFirstImage := regexp.MustCompile(`^(.*)<img`).FindAllString(output, -1)
fmt.Println(untilFirstImage[0])
// This is the expected output from a vanilla create-react-app app:
// output: <div class="App"><header class="App-header"><img
}
func TestRenderer(t *testing.T) {
r, err := reactssr.NewServerSideRenderer("./testdata/test-app-1/build/out.js")
if err != nil {
panic(err)
}
output, err := r.Render()
if err != nil {
panic(err)
}
untilFirstImage := regexp.MustCompile(`^(.*)<img`).FindAllString(output, -1)
// This is the expected output from a vanilla create-react-app app:
expected := `<div class="App"><header class="App-header"><img`
if untilFirstImage[0] != expected {
t.Errorf("Expected %s, got %s", expected, untilFirstImage[0])
}
_, err = r.Render()
if err != nil {
t.Fatal(err)
}
}
func BenchmarkAWarmup(b *testing.B) {
r, err := reactssr.NewServerSideRenderer("./testdata/test-app-1/build/out.js")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := r.Render()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkRender(b *testing.B) {
r, err := reactssr.NewServerSideRenderer("./testdata/test-app-1/build/out.js")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := r.Render()
if err != nil {
b.Fatal(err)
}
}
}