| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- package issues
- import (
- "strings"
- "testing"
- tea "github.com/charmbracelet/bubbletea"
- "github.com/stretchr/testify/assert"
- )
- func Test_Model_Init(t *testing.T) {
- testModel := Model{Path: "tests/bugs/test-1"}
- testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
- assert.Equal(t, testModel.Init()(), tea.Msg(testIssue))
- testModel = Model{Path: "tests/bugs"}
- testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
- assert.Equal(t, testModel.Init()(), tea.Msg(testCollection))
- }
- func Test_Model_Update_quit_on_keymsg(t *testing.T) {
- testModel := Model{}
- testKey := tea.Key{Type: tea.KeyCtrlC}
- testMsg := tea.KeyMsg(testKey)
- model, cmd := testModel.Update(testMsg)
- assert.Equal(t, testModel, model)
- if cmd != nil {
- cmdMsg := cmd()
- assert.Equal(t, tea.QuitMsg{}, cmdMsg)
- } else {
- assert.Fail(t, "should return cmd!")
- }
- }
- func Test_Model_Update_scroll_up(t *testing.T) {
- ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
- testModel := Model{widget: ic}
- testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
- testMsg := tea.KeyMsg(testKey)
- model, cmd := testModel.Update(testMsg)
- widget, _ := model.(Model).widget.(IssueCollection)
- assert.Equal(t, ic.selection+1, widget.selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- // test select wraparound
- ic.selection = len(ic.Collection) - 1
- testModel = Model{widget: ic}
- model, cmd = testModel.Update(testMsg)
- widget, _ = model.(Model).widget.(IssueCollection)
- assert.Equal(t, 0, widget.selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- }
- func Test_Model_Update_scroll_down(t *testing.T) {
- ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
- testModel := Model{widget: ic}
- testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
- testMsg := tea.KeyMsg(testKey)
- model, cmd := testModel.Update(testMsg)
- widget, _ := model.(Model).widget.(IssueCollection)
- assert.Equal(t, len(ic.Collection)-1, widget.selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- // test select wraparound
- ic.selection = len(ic.Collection) - 1
- testModel = Model{widget: ic}
- model, _ = testModel.Update(testMsg)
- widget, _ = model.(Model).widget.(IssueCollection)
- assert.Equal(t, len(ic.Collection)-2, widget.selection)
- // if cmd == nil {
- // assert.Fail(t, "should return tea.Cmd")
- // }
- // ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
- // testModel := Model{widget: ic}
- // testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
- // testMsg := tea.KeyMsg(testKey)
- // model, cmd := testModel.Update(testMsg)
- // widget, _ := testModel.widget.(IssueCollection)
- // assert.Equal(t, len(ic.Collection)-1, widget.selection)
- // if cmd == nil {
- // assert.Fail(t, "should return tea.Cmd")
- // }
- // model, cmd = model.Update(testMsg)
- // assert.Equal(t, len(ic.Collection)-2, widget.selection)
- // if cmd == nil {
- // assert.Fail(t, "should return tea.Cmd")
- // }
- }
- func Test_Model_Update_load_on_enter(t *testing.T) {
- ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs")
- testModel := Model{widget: ic}
- testKey := tea.Key{Type: tea.KeyEnter, Runes: []rune{}}
- testMsg := tea.KeyMsg(testKey)
- testPath := ic.Collection[ic.selection].Path
- model, cmd := testModel.Update(testMsg)
- assert.Equal(t, testPath, model.(Model).Path)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- }
- func Test_Model_Update_renderIssue(t *testing.T) {
- testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
- testModel := Model{}
- model, cmd := testModel.Update(tea.Msg(testIssue))
- widget, _ := model.(Model).widget.(Issue)
- if cmd == nil {
- assert.Fail(t, "should return cmd")
- }
- assert.Equal(t, testIssue.Title, widget.Title)
- assert.Equal(t, testIssue.render(), cmd())
- }
- func Test_Model_Update_renderIssueCollection(t *testing.T) {
- testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
- testModel := Model{}
- model, cmd := testModel.Update(tea.Msg(testCollection))
- widget, _ := model.(Model).widget.(IssueCollection)
- if cmd == nil {
- assert.Fail(t, "should return cmd")
- }
- assert.Equal(t, len(testCollection.Collection), len(widget.Collection))
- assert.Equal(t, testCollection.render(), cmd())
- }
- func Test_Model_Update_updates_content(t *testing.T) {
- testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
- testModel := Model{widget: testIssue}
- testWidget := testModel.widget.(Issue)
- testModel.content = testWidget.render().(string)
- model, cmd := testModel.Update(tea.Msg(testModel.content))
- if cmd != nil {
- assert.Fail(t, "should not return cmd")
- }
- assert.Equal(t, testModel.content, model.(Model).content)
- }
- func Test_Model_Update_do_nothing(t *testing.T) {
- testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
- testModel := Model{widget: testIssue}
- var testMsg int
- model, cmd := testModel.Update(testMsg)
- assert.Equal(t, testModel, model)
- assert.Nil(t, cmd)
- }
- func Test_Model_renderIssue(t *testing.T) {
- testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
- testRender := testIssue.render()
- renderContent, _ := testRender.(string)
- assert.True(t, strings.Contains(renderContent, "test description"))
- }
- func Test_Model_View(t *testing.T) {
- testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
- model, cmd := Model{}.Update(tea.Msg(testIssue)) // gives render cmd
- model, cmd = model.Update(cmd()) // handle internal render
- if cmd != nil {
- assert.Fail(t, "should not return cmd")
- }
- testView := testIssue.render().(string) + "\nj/k: down/up\tenter: select\tq: quit"
- assert.Equal(t, testView, model.(Model).View())
- render2 := Model{}.View()
- assert.Equal(t, "loading...", render2)
- }
|