frontend_test.go
61 lines1
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
package frontend
import (
"testing"
"time"
)
func TestNewDefaults(t *testing.T) {
t.Setenv("ENV", "development")
f := New()
if f.SourceDir != "components" {
t.Errorf("SourceDir = %q, want components", f.SourceDir)
}
if f.OutputDir != "views/static/scripts/gen" {
t.Errorf("OutputDir = %q, want views/static/scripts/gen", f.OutputDir)
}
if !f.DevMode {
t.Error("DevMode should be true when ENV != production")
}
close(f.hmrDone) // stop the hub goroutine
}
func TestNewProductionDisablesHMR(t *testing.T) {
t.Setenv("ENV", "production")
f := New()
if f.DevMode {
t.Error("DevMode should be false in production")
}
if f.hmrReload != nil {
t.Error("HMR channels should be nil in production")
}
}
func TestHMRHubBroadcastsReload(t *testing.T) {
t.Setenv("ENV", "development")
f := New()
defer close(f.hmrDone)
ch := make(chan struct{}, 1)
f.hmrJoin <- ch // subscribe (unbuffered — returns once the hub has us)
f.notifyClients() // trigger a reload
select {
case <-ch:
case <-time.After(time.Second):
t.Fatal("subscribed client did not receive reload signal")
}
f.hmrLeave <- ch // unsubscribe (hub processes this before the next reload)
f.notifyClients() // reload again — nobody should be listening
select {
case <-ch:
t.Fatal("unsubscribed client still received a reload signal")
case <-time.After(100 * time.Millisecond):
}
}
func TestNotifyClientsProductionNoop(t *testing.T) {
f := &Frontend{} // no HMR channels
f.notifyClients() // must not panic
}