Files
term-tier/internal/handlers/addItemHandler_test.go
T

88 lines
1.9 KiB
Go

package handlers_test
import (
"testing"
"github.com/StevanFreeborn/term-tier/internal/handlers"
"github.com/StevanFreeborn/term-tier/internal/state"
tea "github.com/charmbracelet/bubbletea"
)
func TestNewAddItemHandler(t *testing.T) {
h := handlers.NewAddItemHandler()
if h.Description() != "Add new item" {
t.Errorf("Expected description 'Add new item', got %q", h.Description())
}
keys := h.KeysHandled()
if len(keys) != 1 || keys[0] != "n" {
t.Errorf("Expected keys ['n'], got %v", keys)
}
}
func TestAddItemHandler_Match(t *testing.T) {
h := handlers.NewAddItemHandler()
tests := []struct {
name string
mode state.Mode
key string
expected bool
}{
{
name: "Matches 'n' in Navigation Mode",
mode: state.NavigationMode,
key: "n",
expected: true,
},
{
name: "Ignores 'n' in Input Mode",
mode: state.InputMode,
key: "n",
expected: false,
},
{
name: "Ignores 'q' in Navigation Mode",
mode: state.NavigationMode,
key: "q",
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 TestAddItemHandler_Handle(t *testing.T) {
h := handlers.NewAddItemHandler()
s := state.New(state.WithMode(state.NavigationMode))
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("n")}
cmd := h.Handle(s, msg)
if s.Mode() != state.InputMode {
t.Errorf("Expected state to switch to InputMode, got %v", s.Mode())
}
expectedTitle := "Add new item"
if s.InputTitle() != expectedTitle {
t.Errorf("Expected input title %q, got %q", expectedTitle, s.InputTitle())
}
if cmd == nil {
t.Error("Expected Handle to return a tea.Cmd (cursor blink), got nil")
}
}