-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGIFX-60: Increase unit test coverage
- Loading branch information
1 parent
22657e8
commit 6bb9a8a
Showing
9 changed files
with
87 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ui | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/grafviktor/goto/internal/mock" | ||
"github.com/grafviktor/goto/internal/state" | ||
"github.com/grafviktor/goto/internal/ui/message" | ||
"github.com/grafviktor/goto/internal/utils/ssh" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
model := New(context.TODO(), mock.NewMockStorage(true), MockAppState(), &mock.MockLogger{}) | ||
require.NotNil(t, model) | ||
} | ||
|
||
func TestUpdate_KeyMsg(t *testing.T) { | ||
// Random key test - make sure that the app reacts on Ctrl+C | ||
model := New(context.TODO(), mock.NewMockStorage(true), MockAppState(), &mock.MockLogger{}) | ||
_, cmd := model.Update(tea.KeyMsg{ | ||
Type: tea.KeyCtrlC, | ||
}) | ||
|
||
assert.NotNil(t, model) | ||
require.IsType(t, tea.QuitMsg{}, cmd(), "Wrong message type") | ||
} | ||
|
||
func TestUpdate_TerminalSizePolling(t *testing.T) { | ||
// Ensure that when the model receives TerminalSizePolling it autogenerates 'WindowSizeMsg' | ||
model := New(context.TODO(), mock.NewMockStorage(true), MockAppState(), &mock.MockLogger{}) | ||
assert.Equal(t, 0, model.appState.Width) | ||
assert.Equal(t, 0, model.appState.Height) | ||
|
||
_, cmds := model.Update(message.TerminalSizePolling{ | ||
Width: 10, | ||
Height: 10, | ||
}) | ||
|
||
var dst []tea.Msg | ||
cmdToMessage(cmds, &dst) | ||
|
||
require.Contains(t, dst, tea.WindowSizeMsg{ | ||
Width: 10, | ||
Height: 10, | ||
}) | ||
} | ||
|
||
func MockAppState() *state.ApplicationState { | ||
return &state.ApplicationState{ | ||
HostSSHConfig: &ssh.Config{}, | ||
} | ||
} | ||
|
||
func cmdToMessage(cmd tea.Cmd, messages *[]tea.Msg) { | ||
result := cmd() | ||
|
||
if batchMsg, ok := result.(tea.BatchMsg); ok { | ||
for _, msg := range batchMsg { | ||
cmdToMessage(msg, messages) | ||
} | ||
} else { | ||
*messages = append(*messages, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters