feat: implement item management handlers with corresponding tests
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
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 TestNewUpdateItemHandler(t *testing.T) {
|
||||
h := handlers.NewUpdateItemHandler()
|
||||
|
||||
if h.Description() != "Update item" {
|
||||
t.Errorf("Expected description 'Update item', got %q", h.Description())
|
||||
}
|
||||
|
||||
keys := h.KeysHandled()
|
||||
|
||||
if !slices.Contains(keys, "r") {
|
||||
t.Errorf("Expected keys ['r'], got %v", keys)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateItemHandler_Match(t *testing.T) {
|
||||
h := handlers.NewUpdateItemHandler()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mode state.Mode
|
||||
key string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Matches 'r' in Navigation Mode",
|
||||
mode: state.NavigationMode,
|
||||
key: "r",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Ignores 'r' in Input Mode",
|
||||
mode: state.InputMode,
|
||||
key: "r",
|
||||
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)}
|
||||
|
||||
if got := h.Match(s, msg); got != tt.expected {
|
||||
t.Errorf("Match() = %v, want %v", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateItemHandler_Handle(t *testing.T) {
|
||||
h := handlers.NewUpdateItemHandler()
|
||||
|
||||
tiers := []core.Tier{{Name: "S", Items: []core.Item{"EditMe"}}}
|
||||
|
||||
s := state.New(
|
||||
state.WithTiers(tiers),
|
||||
state.WithCursor(0, 0),
|
||||
state.WithMode(state.NavigationMode),
|
||||
)
|
||||
|
||||
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("r")}
|
||||
cmd := h.Handle(s, msg)
|
||||
|
||||
if s.Mode() != state.InputMode {
|
||||
t.Errorf("Expected state to switch to InputMode, got %v", s.Mode())
|
||||
}
|
||||
|
||||
if s.Input().Value() != "EditMe" {
|
||||
t.Errorf("Expected input to be pre-filled with 'EditMe', got %q", s.Input().Value())
|
||||
}
|
||||
|
||||
if cmd == nil {
|
||||
t.Error("Expected Handle to return a command, got nil")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user