feat: implement item management handlers with corresponding tests
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/term-tier/internal/core"
|
||||
"github.com/StevanFreeborn/term-tier/internal/handlers"
|
||||
"github.com/StevanFreeborn/term-tier/internal/state"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestNewSelectItemHandler(t *testing.T) {
|
||||
h := handlers.NewSelectItemHandler()
|
||||
|
||||
if h.Description() != "Select item" {
|
||||
t.Errorf("Expected description 'Select item', got %q", h.Description())
|
||||
}
|
||||
|
||||
keys := h.KeysHandled()
|
||||
|
||||
if !slices.Contains(keys, "enter") || !slices.Contains(keys, "space") {
|
||||
t.Errorf("Expected keys ['enter', 'space'], got %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectItemHandler_Match(t *testing.T) {
|
||||
h := handlers.NewSelectItemHandler()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mode state.Mode
|
||||
key string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Matches 'enter' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "enter",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Matches 'space' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: " ",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Matches 'space' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "space",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'enter' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: "enter",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'space' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: " ",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'space' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: "space",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Ignores other keys",
|
||||
mode: state.NavigationMode,
|
||||
key: "a",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := state.New(state.WithMode(tt.mode))
|
||||
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(tt.key)}
|
||||
|
||||
switch tt.key {
|
||||
case "enter":
|
||||
msg = tea.KeyMsg{Type: tea.KeyEnter}
|
||||
case " ":
|
||||
msg = tea.KeyMsg{Type: tea.KeySpace}
|
||||
}
|
||||
|
||||
if got := h.Match(s, msg); got != tt.expected {
|
||||
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectItemHandler_Handle(t *testing.T) {
|
||||
h := handlers.NewSelectItemHandler()
|
||||
|
||||
tiers := []core.Tier{{Name: "S", Items: []core.Item{"Item A"}}}
|
||||
|
||||
s := state.New(
|
||||
state.WithTiers(tiers),
|
||||
state.WithCursor(0, 0),
|
||||
state.WithMode(state.NavigationMode),
|
||||
)
|
||||
|
||||
t.Run("Starts Dragging", func(t *testing.T) {
|
||||
msg := tea.KeyMsg{Type: tea.KeyEnter}
|
||||
cmd := h.Handle(s, msg)
|
||||
|
||||
if !s.Dragging() {
|
||||
t.Error("Expected dragging to be true after selecting item")
|
||||
}
|
||||
|
||||
if cmd != nil {
|
||||
t.Error("Expected nil command")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Stops Dragging", func(t *testing.T) {
|
||||
if !s.Dragging() {
|
||||
s.SelectItem()
|
||||
}
|
||||
|
||||
msg := tea.KeyMsg{Type: tea.KeyEnter}
|
||||
|
||||
h.Handle(s, msg)
|
||||
|
||||
if s.Dragging() {
|
||||
t.Error("Expected dragging to be false after selecting again")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user