Skip to content

Commit cb79f99

Browse files
committed
chore: test for check box
1 parent 26361d7 commit cb79f99

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

utils/checkbox_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
tea "github.com/charmbracelet/bubbletea"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCheckBoxNavigationAndSelection(t *testing.T) {
11+
options := []string{"Option 1", "Option 2", "Option 3"}
12+
cb := NewCheckBox(options)
13+
14+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
15+
assert.Equal(t, cb.Cursor, 1)
16+
17+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")})
18+
assert.Equal(t, cb.Cursor, 0)
19+
20+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
21+
assert.Equal(t, cb.GetSelected(), []string{"Option 1"})
22+
23+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
24+
assert.Equal(t, cb.GetSelected(), []string{})
25+
26+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
27+
28+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
29+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
30+
assert.Equal(t, cb.GetSelected(), []string{"Option 1", "Option 2"})
31+
32+
_, _, entered := cb.Select(tea.KeyMsg{Type: tea.KeyEnter})
33+
assert.True(t, entered)
34+
}
35+
36+
func TestCheckBoxQuit(t *testing.T) {
37+
options := []string{"Option 1", "Option 2", "Option 3"}
38+
cb := NewCheckBox(options)
39+
40+
_, cmd, _ := cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
41+
assert.NotNil(t, cmd)
42+
}
43+
44+
func TestCheckBoxNavigationWrapping(t *testing.T) {
45+
options := []string{"Option 1", "Option 2", "Option 3"}
46+
cb := NewCheckBox(options)
47+
48+
for i := 0; i < len(options)+1; i++ {
49+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
50+
}
51+
assert.Equal(t, 1, cb.Cursor)
52+
53+
cb.Cursor = 0
54+
for i := 0; i < len(options)+1; i++ {
55+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")})
56+
}
57+
assert.Equal(t, len(options)-1, cb.Cursor)
58+
}
59+
60+
func TestCheckBoxSimultaneousSelectionsAndDeselections(t *testing.T) {
61+
options := []string{"Option 1", "Option 2", "Option 3"}
62+
cb := NewCheckBox(options)
63+
64+
for i := 0; i < len(options); i++ {
65+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
66+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
67+
}
68+
69+
expectedAllSelected := []string{"Option 1", "Option 2", "Option 3"}
70+
assert.ElementsMatch(t, expectedAllSelected, cb.GetSelected())
71+
72+
for i := 0; i < len(options); i++ {
73+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")})
74+
cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace})
75+
}
76+
77+
expectedNoneSelected := []string{}
78+
assert.ElementsMatch(t, expectedNoneSelected, cb.GetSelected())
79+
}

0 commit comments

Comments
 (0)