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

100 lines
2.1 KiB
Go

package handlers_test
import (
"slices"
"testing"
"github.com/StevanFreeborn/term-tier/internal/handlers"
"github.com/StevanFreeborn/term-tier/internal/state"
tea "github.com/charmbracelet/bubbletea"
)
func TestNewQuitHandler(t *testing.T) {
h := handlers.NewQuitHandler()
if h.Description() != "Quit" {
t.Errorf("Expected description 'Quit', got %q", h.Description())
}
keys := h.KeysHandled()
if !slices.Contains(keys, "q") || !slices.Contains(keys, "ctrl+c") {
t.Errorf("Expected keys ['q', 'ctrl+c'], got %v", keys)
}
}
func TestQuitHandler_Match(t *testing.T) {
h := handlers.NewQuitHandler()
tests := []struct {
name string
mode state.Mode
key string
expected bool
}{
{
name: "Matches 'q' in Navigation Mode",
mode: state.NavigationMode,
key: "q",
expected: true,
},
{
name: "Matches 'ctrl+c' in Navigation Mode",
mode: state.NavigationMode,
key: "ctrl+c",
expected: true,
},
{
name: "Ignores 'q' in Input Mode",
mode: state.InputMode,
key: "q",
expected: false,
},
{
name: "Ignores 'ctrl+c' in Input Mode",
mode: state.InputMode,
key: "ctrl+c",
expected: false,
},
{
name: "Ignores other keys in Navigation Mode",
mode: state.NavigationMode,
key: "x",
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 tt.key == "ctrl+c" {
msg = tea.KeyMsg{Type: tea.KeyCtrlC}
}
if got := h.Match(s, msg); got != tt.expected {
t.Errorf("Match() = %v, want %v", got, tt.expected)
}
})
}
}
func TestQuitHandler_Handle(t *testing.T) {
h := handlers.NewQuitHandler()
s := state.New(state.WithMode(state.NavigationMode))
msg := tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")}
cmd := h.Handle(s, msg)
if cmd == nil {
t.Fatal("Expected Handle to return a command, got nil")
}
cmdMsg := cmd()
if _, ok := cmdMsg.(tea.QuitMsg); !ok {
t.Errorf("Expected command to produce tea.QuitMsg, got %T", cmdMsg)
}
}