1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
package dialog
import (
"testing"
"glint/internal/theme"
tea "github.com/charmbracelet/bubbletea"
)
func newModel() *Model { return New(theme.FlexokiDark()) }
func typeRunes(m *Model, s string) {
for _, r := range s {
m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
}
}
func TestOpenSetsTitleAndPrefill(t *testing.T) {
m := newModel()
m.Open("Rename", "~/Notes/", "my-note")
if m.Title() != "Rename" {
t.Errorf("Title() = %q, want Rename", m.Title())
}
if m.Value() != "my-note" {
t.Errorf("Value() = %q, want my-note", m.Value())
}
}
func TestTypingAppendsAfterPrefill(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "draft")
typeRunes(m, "-2")
if m.Value() != "draft-2" {
t.Errorf("Value() = %q, want draft-2 (cursor must start at end of prefill)", m.Value())
}
}
func TestEnterConfirms(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
typeRunes(m, "hello")
res, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
if res != ResultConfirm {
t.Errorf("Enter gave %v, want ResultConfirm", res)
}
if m.Value() != "hello" {
t.Errorf("Value() = %q, want hello", m.Value())
}
}
func TestEscCancels(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
res, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
if res != ResultCancel {
t.Errorf("Esc gave %v, want ResultCancel", res)
}
}
func TestOrdinaryKeysReturnNone(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
res, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}})
if res != ResultNone {
t.Errorf("rune key gave %v, want ResultNone", res)
}
}
func TestValueTrimsSurroundingSpace(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
typeRunes(m, " spaced ")
if m.Value() != "spaced" {
t.Errorf("Value() = %q, want spaced", m.Value())
}
}
func TestOpenClearsPreviousError(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
m.SetError("Name taken")
m.Open("New note", "~/Notes/", "")
if m.err != "" {
t.Errorf("err = %q after reopen, want empty", m.err)
}
}
func TestTypingClearsError(t *testing.T) {
m := newModel()
m.Open("New note", "~/Notes/", "")
m.SetError("Type a name first")
typeRunes(m, "a")
if m.err != "" {
t.Errorf("err = %q after typing, want empty", m.err)
}
}
|