From cb79f99131cdd9fdf2a7491eaf9b852d7807b2b9 Mon Sep 17 00:00:00 2001 From: "beeb@neb.one" Date: Tue, 10 Sep 2024 13:32:40 +0700 Subject: [PATCH] chore: test for check box --- utils/checkbox_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 utils/checkbox_test.go diff --git a/utils/checkbox_test.go b/utils/checkbox_test.go new file mode 100644 index 00000000..ff285fc0 --- /dev/null +++ b/utils/checkbox_test.go @@ -0,0 +1,79 @@ +package utils + +import ( + "testing" + + tea "github.com/charmbracelet/bubbletea" + "github.com/stretchr/testify/assert" +) + +func TestCheckBoxNavigationAndSelection(t *testing.T) { + options := []string{"Option 1", "Option 2", "Option 3"} + cb := NewCheckBox(options) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")}) + assert.Equal(t, cb.Cursor, 1) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")}) + assert.Equal(t, cb.Cursor, 0) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + assert.Equal(t, cb.GetSelected(), []string{"Option 1"}) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + assert.Equal(t, cb.GetSelected(), []string{}) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")}) + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + assert.Equal(t, cb.GetSelected(), []string{"Option 1", "Option 2"}) + + _, _, entered := cb.Select(tea.KeyMsg{Type: tea.KeyEnter}) + assert.True(t, entered) +} + +func TestCheckBoxQuit(t *testing.T) { + options := []string{"Option 1", "Option 2", "Option 3"} + cb := NewCheckBox(options) + + _, cmd, _ := cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")}) + assert.NotNil(t, cmd) +} + +func TestCheckBoxNavigationWrapping(t *testing.T) { + options := []string{"Option 1", "Option 2", "Option 3"} + cb := NewCheckBox(options) + + for i := 0; i < len(options)+1; i++ { + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")}) + } + assert.Equal(t, 1, cb.Cursor) + + cb.Cursor = 0 + for i := 0; i < len(options)+1; i++ { + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")}) + } + assert.Equal(t, len(options)-1, cb.Cursor) +} + +func TestCheckBoxSimultaneousSelectionsAndDeselections(t *testing.T) { + options := []string{"Option 1", "Option 2", "Option 3"} + cb := NewCheckBox(options) + + for i := 0; i < len(options); i++ { + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")}) + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + } + + expectedAllSelected := []string{"Option 1", "Option 2", "Option 3"} + assert.ElementsMatch(t, expectedAllSelected, cb.GetSelected()) + + for i := 0; i < len(options); i++ { + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("k")}) + cb, _, _ = cb.Select(tea.KeyMsg{Type: tea.KeySpace}) + } + + expectedNoneSelected := []string{} + assert.ElementsMatch(t, expectedNoneSelected, cb.GetSelected()) +}