-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactea_test.go
More file actions
103 lines (78 loc) · 2.07 KB
/
Copy pathreactea_test.go
File metadata and controls
103 lines (78 loc) · 2.07 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
package reactea
import (
"bytes"
"strings"
"testing"
"time"
tea "github.com/charmbracelet/bubbletea"
)
func TestComponent(t *testing.T) {
var in, out bytes.Buffer
in.WriteString("~~~")
type testState struct {
echoKey string
lastWidth, lastHeight int
}
root := &mockComponent[testState]{
updateFunc: func(c Component, s *testState, msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.String() == "x" {
return Destroy
}
s.echoKey = msg.String()
}
SetRoute("/test/test/test")
return nil
},
renderFunc: func(c Component, s *testState, width, height int) string {
s.lastWidth, s.lastHeight = width, height
return s.echoKey
},
}
program := NewProgram(root, tea.WithInput(&in), tea.WithOutput(&out))
// Test for window size
go func() {
// Simulate initial window size
program.Send(tea.WindowSizeMsg{Width: 1, Height: 1})
// Give time to catch up
time.Sleep(50 * time.Millisecond)
// Simulate pressing X
program.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}, Alt: false})
}()
if _, err := program.Run(); err != nil {
t.Fatal(err)
}
if strings.Contains(out.String(), "default") {
t.Errorf("did not echo")
}
if !strings.Contains(out.String(), "~") {
t.Errorf("invalid echo")
}
if WasRouteChanged() {
t.Errorf("current route was changed")
}
if CurrentRoute() != "/test/test/test" {
t.Errorf("current route is wrong, expected \"/test/test/test\", got \"%s\"", CurrentRoute())
}
if root.state.lastWidth != 1 {
t.Errorf("expected lastWidth 1, but got %d", root.state.lastWidth)
}
if root.state.lastHeight != 1 {
t.Errorf("expected lastHeigth 1, but got %d", root.state.lastWidth)
}
}
func TestNew(t *testing.T) {
t.Run("NewProgram", func(t *testing.T) {
root := &mockComponent[struct{}]{
renderFunc: func(c Component, s *struct{}, width, height int) string {
return "test passed"
},
}
program := NewProgram(root, WithoutInput(), tea.WithoutRenderer())
go program.Quit()
if _, err := program.Run(); err != nil {
t.Fatal(err)
}
})
}