-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathmain_test.go
119 lines (94 loc) · 2.76 KB
/
main_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
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
package main
import (
"bytes"
"io"
"os"
"testing"
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/teatest"
"github.com/muesli/termenv"
"github.com/stretchr/testify/require"
"github.com/antonmedv/fx/internal/jsonx"
"github.com/antonmedv/fx/internal/theme"
)
func init() {
lipgloss.SetColorProfile(termenv.ANSI)
}
func prepare(t *testing.T) *teatest.TestModel {
file, err := os.Open("testdata/example.json")
require.NoError(t, err)
json, err := io.ReadAll(file)
require.NoError(t, err)
head, err := jsonx.Parse(json)
require.NoError(t, err)
m := &model{
top: head,
head: head,
wrap: true,
showCursor: true,
digInput: textinput.New(),
searchInput: textinput.New(),
search: newSearch(),
}
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(80, 40),
)
return tm
}
func read(t *testing.T, tm *teatest.TestModel) []byte {
var out []byte
teatest.WaitFor(t,
tm.Output(),
func(b []byte) bool {
out = b
return bytes.Contains(b, []byte("{"))
},
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second),
)
return out
}
func TestOutput(t *testing.T) {
tm := prepare(t)
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestNavigation(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestDig(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(".")})
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("year")})
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursive(t *testing.T) {
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyShiftLeft})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursiveWithSizes(t *testing.T) {
theme.ShowSizes = true
defer func() { theme.ShowSizes = true }()
tm := prepare(t)
tm.Send(tea.KeyMsg{Type: tea.KeyShiftLeft})
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}