Skip to content

Commit 5c965a6

Browse files
authored
refactor all tests to use asserts (#7)
1 parent 46fee13 commit 5c965a6

4 files changed

Lines changed: 139 additions & 190 deletions

File tree

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package components_test
22

33
import (
4-
"strings"
54
"testing"
65

76
"tusshi/internal/tui/components"
87
"tusshi/internal/tui/theme"
98

109
tea "github.com/charmbracelet/bubbletea"
10+
"github.com/stretchr/testify/assert"
1111
)
1212

1313
func TestAlertComponent(t *testing.T) {
@@ -18,35 +18,30 @@ func TestAlertComponent(t *testing.T) {
1818
Theme: theme.Mock,
1919
}
2020

21-
if cmd := alert.Init(); cmd != nil {
22-
t.Errorf("expected Init to return nil, got %v", cmd)
23-
}
21+
t.Run("init", func(t *testing.T) {
22+
assert.Nil(t, alert.Init())
23+
})
2424

25-
rendered := alert.View(50)
26-
if !strings.Contains(rendered, "Backup Failed") {
27-
t.Error("expected view to contain title")
28-
}
29-
if !strings.Contains(rendered, "Could not create initial pre-tuSSHi backup") {
30-
t.Error("expected view to contain message")
31-
}
32-
if !strings.Contains(rendered, "OK") {
33-
t.Error("expected view to contain OK button")
34-
}
25+
t.Run("view", func(t *testing.T) {
26+
rendered := alert.View(50)
27+
assert.Contains(t, rendered, "Backup Failed")
28+
assert.Contains(t, rendered, "Could not create initial pre-tuSSHi backup")
29+
assert.Contains(t, rendered, "OK")
30+
})
3531

36-
_, done := alert.Update(tea.KeyMsg{Type: tea.KeyEsc})
37-
if !done {
38-
t.Error("expected Esc to dismiss alert")
39-
}
40-
_, done = alert.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
41-
if !done {
42-
t.Error("expected 'q' to dismiss alert")
43-
}
44-
_, done = alert.Update(tea.KeyMsg{Type: tea.KeyEnter})
45-
if !done {
46-
t.Error("expected Enter to dismiss alert")
47-
}
48-
_, done = alert.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x")})
49-
if done {
50-
t.Error("expected other keys to not dismiss alert")
51-
}
32+
t.Run("dismiss keys", func(t *testing.T) {
33+
_, done := alert.Update(tea.KeyMsg{Type: tea.KeyEsc})
34+
assert.True(t, done)
35+
36+
_, done = alert.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
37+
assert.True(t, done)
38+
39+
_, done = alert.Update(tea.KeyMsg{Type: tea.KeyEnter})
40+
assert.True(t, done)
41+
})
42+
43+
t.Run("non-dismiss keys", func(t *testing.T) {
44+
_, done := alert.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("x")})
45+
assert.False(t, done)
46+
})
5247
}
Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package components_test
22

33
import (
4-
"strings"
54
"testing"
65

76
"tusshi/internal/tui/components"
87
"tusshi/internal/tui/theme"
98

109
tea "github.com/charmbracelet/bubbletea"
10+
"github.com/stretchr/testify/assert"
1111
)
1212

1313
func TestConfirmComponent(t *testing.T) {
@@ -22,75 +22,59 @@ func TestConfirmComponent(t *testing.T) {
2222
},
2323
}
2424

25-
if c.Title != "Test Confirm" {
26-
t.Errorf("expected Title 'Test Confirm', got %q", c.Title)
27-
}
25+
t.Run("initial state", func(t *testing.T) {
26+
assert.Equal(t, "Test Confirm", c.Title)
27+
assert.False(t, c.YesSelected)
28+
})
2829

29-
if c.YesSelected {
30-
t.Error("expected YesSelected to be false by default")
31-
}
30+
t.Run("move left", func(t *testing.T) {
31+
_, done := c.Update(tea.KeyMsg{Type: tea.KeyLeft})
32+
assert.False(t, done)
33+
assert.True(t, c.YesSelected)
34+
})
3235

33-
// Move left
34-
_, done := c.Update(tea.KeyMsg{Type: tea.KeyLeft})
35-
if done {
36-
t.Error("expected navigation to not finalize selection")
37-
}
38-
if !c.YesSelected {
39-
t.Error("expected YesSelected to be true after left key press")
40-
}
41-
42-
// Move right
43-
_, done = c.Update(tea.KeyMsg{Type: tea.KeyRight})
44-
if done {
45-
t.Error("expected navigation to not finalize selection")
46-
}
47-
if c.YesSelected {
48-
t.Error("expected YesSelected to be false after right key press")
49-
}
36+
t.Run("move right", func(t *testing.T) {
37+
_, done := c.Update(tea.KeyMsg{Type: tea.KeyRight})
38+
assert.False(t, done)
39+
assert.False(t, c.YesSelected)
40+
})
5041

51-
// Confirm 'No'
52-
_, done = c.Update(tea.KeyMsg{Type: tea.KeyEnter})
53-
if !done {
54-
t.Error("expected done to be true after enter key press")
55-
}
56-
if confirmedCalled {
57-
t.Error("expected confirmedCalled to be false since 'No' was focused")
58-
}
42+
t.Run("confirm no", func(t *testing.T) {
43+
_, done := c.Update(tea.KeyMsg{Type: tea.KeyEnter})
44+
assert.True(t, done)
45+
assert.False(t, confirmedCalled)
46+
})
5947

60-
// Select Yes and Confirm 'Yes'
61-
c.YesSelected = true
62-
_, done = c.Update(tea.KeyMsg{Type: tea.KeyEnter})
63-
if !done {
64-
t.Error("expected done to be true after enter key press on Yes")
65-
}
66-
if !confirmedCalled {
67-
t.Error("expected confirmedCalled to be true since 'Yes' was focused")
68-
}
48+
t.Run("select yes and confirm yes", func(t *testing.T) {
49+
c.YesSelected = true
50+
_, done := c.Update(tea.KeyMsg{Type: tea.KeyEnter})
51+
assert.True(t, done)
52+
assert.True(t, confirmedCalled)
53+
})
6954

70-
// Esc test
71-
_, done = c.Update(tea.KeyMsg{Type: tea.KeyEsc})
72-
if !done {
73-
t.Error("expected done to be true after esc key press")
74-
}
55+
t.Run("esc close", func(t *testing.T) {
56+
_, done := c.Update(tea.KeyMsg{Type: tea.KeyEsc})
57+
assert.True(t, done)
58+
})
7559
}
7660

7761
func TestConfirmCustomLabels(t *testing.T) {
7862
c := &components.Confirm{
7963
Theme: theme.Mock,
8064
}
8165

82-
// Verify defaults render when fields are left empty
83-
viewEmpty := c.View(40)
84-
if !strings.Contains(viewEmpty, " Yes ") || !strings.Contains(viewEmpty, " No ") {
85-
t.Error("expected view to render default button labels when empty")
86-
}
66+
t.Run("default labels", func(t *testing.T) {
67+
viewEmpty := c.View(40)
68+
assert.Contains(t, viewEmpty, " Yes ")
69+
assert.Contains(t, viewEmpty, " No ")
70+
})
8771

88-
// Apply Option 2 (direct mutation)
89-
c.YesStr = " Delete "
90-
c.NoStr = " Cancel "
72+
t.Run("custom labels", func(t *testing.T) {
73+
c.YesStr = " Delete "
74+
c.NoStr = " Cancel "
9175

92-
viewCustom := c.View(40)
93-
if !strings.Contains(viewCustom, " Delete ") || !strings.Contains(viewCustom, " Cancel ") {
94-
t.Error("expected view to render custom button labels after mutation")
95-
}
76+
viewCustom := c.View(40)
77+
assert.Contains(t, viewCustom, " Delete ")
78+
assert.Contains(t, viewCustom, " Cancel ")
79+
})
9680
}

internal/tui/components/form_test.go

Lines changed: 48 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
tea "github.com/charmbracelet/bubbletea"
1010
"github.com/charmbracelet/huh"
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
func TestFormComponent(t *testing.T) {
@@ -26,40 +27,33 @@ func TestFormComponent(t *testing.T) {
2627
},
2728
}
2829

29-
// Test Init
30-
cmd := f.Init()
31-
if cmd == nil {
32-
t.Error("expected Init to return a non-nil command for huh form initialization")
33-
}
34-
35-
// Test View rendering
36-
rendered := f.View(40)
37-
if rendered == "" {
38-
t.Error("expected View to return a non-empty string representation of the form")
39-
}
40-
41-
// Test cancellation key: Esc
42-
_, done := f.Update(tea.KeyMsg{Type: tea.KeyEsc})
43-
if !done {
44-
t.Error("expected Esc to finish/cancel the form component")
45-
}
46-
47-
// Test manual completed state transition to trigger callback
48-
f.Form.State = huh.StateCompleted
49-
_, done = f.Update(nil)
50-
if !done {
51-
t.Error("expected completed form state to return done = true")
52-
}
53-
if !submitted {
54-
t.Error("expected OnSubmit callback to be executed on form completion")
55-
}
56-
57-
// Test manual aborted state transition
58-
f.Form.State = huh.StateAborted
59-
_, done = f.Update(nil)
60-
if !done {
61-
t.Error("expected aborted form state to return done = true")
62-
}
30+
t.Run("init", func(t *testing.T) {
31+
cmd := f.Init()
32+
assert.NotNil(t, cmd)
33+
})
34+
35+
t.Run("view rendering", func(t *testing.T) {
36+
rendered := f.View(40)
37+
assert.NotEmpty(t, rendered)
38+
})
39+
40+
t.Run("cancellation", func(t *testing.T) {
41+
_, done := f.Update(tea.KeyMsg{Type: tea.KeyEsc})
42+
assert.True(t, done)
43+
})
44+
45+
t.Run("completed state transition", func(t *testing.T) {
46+
f.Form.State = huh.StateCompleted
47+
_, done := f.Update(nil)
48+
assert.True(t, done)
49+
assert.True(t, submitted)
50+
})
51+
52+
t.Run("aborted state transition", func(t *testing.T) {
53+
f.Form.State = huh.StateAborted
54+
_, done := f.Update(nil)
55+
assert.True(t, done)
56+
})
6357
}
6458

6559
func TestFormValidationAndSubmission(t *testing.T) {
@@ -85,36 +79,24 @@ func TestFormValidationAndSubmission(t *testing.T) {
8579
},
8680
}
8781

88-
// Part 1: Validation fails
89-
f.Form.State = huh.StateNormal
90-
validationErr = errors.New("invalid field")
91-
_, done := f.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
92-
if done {
93-
t.Error("expected form to not finalize when validation fails")
94-
}
95-
if !validated {
96-
t.Error("expected validation function to be called")
97-
}
98-
if submitted {
99-
t.Error("expected form not to submit when validation fails")
100-
}
101-
102-
// Part 2: validation succeeds
103-
validated = false
104-
submitted = false
105-
106-
validationErr = nil
107-
_, done = f.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
108-
if !done {
109-
t.Error("expected form to finalize when validation succeeds")
110-
}
111-
if !validated {
112-
t.Error("expected validation function to be called")
113-
}
114-
if !submitted {
115-
t.Error("expected form to submit when validation succeeds")
116-
}
117-
if f.Form.State != huh.StateCompleted {
118-
t.Errorf("expected form state to be Completed, got %v", f.Form.State)
119-
}
82+
t.Run("validation fails", func(t *testing.T) {
83+
f.Form.State = huh.StateNormal
84+
validationErr = errors.New("invalid field")
85+
_, done := f.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
86+
assert.False(t, done)
87+
assert.True(t, validated)
88+
assert.False(t, submitted)
89+
})
90+
91+
t.Run("validation succeeds", func(t *testing.T) {
92+
validated = false
93+
submitted = false
94+
validationErr = nil
95+
96+
_, done := f.Update(tea.KeyMsg{Type: tea.KeyCtrlS})
97+
assert.True(t, done)
98+
assert.True(t, validated)
99+
assert.True(t, submitted)
100+
assert.Equal(t, huh.StateCompleted, f.Form.State)
101+
})
120102
}

0 commit comments

Comments
 (0)