| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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.KeyRunes, Runes: []rune{'q'}}
- 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_on_k(t *testing.T) {
- ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
- testModel := Model{collection: ic}
- testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
- testMsg := tea.KeyMsg(testKey)
- model, cmd := testModel.Update(testMsg)
- assert.Equal(t, testModel.selection+1, model.(Model).selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- // test select wraparound
- testModel.selection = len(testModel.collection) - 1
- model, cmd = testModel.Update(testMsg)
- assert.Equal(t, 0, model.(Model).selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- }
- func Test_Model_Update_scroll_on_j(t *testing.T) {
- ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
- testModel := Model{collection: ic}
- testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
- testMsg := tea.KeyMsg(testKey)
- model, cmd := testModel.Update(testMsg)
- assert.Equal(t, len(testModel.collection)-1, model.(Model).selection)
- if cmd == nil {
- assert.Fail(t, "should return tea.Cmd")
- }
- model, cmd = model.(Model).Update(testMsg)
- assert.Equal(t, len(testModel.collection)-2, model.(Model).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{collection: ic}
- testKey := tea.Key{Type: tea.KeyEnter, Runes: []rune{}}
- testMsg := tea.KeyMsg(testKey)
- testPath := testModel.collection[testModel.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))
- if cmd == nil {
- assert.Fail(t, "should return cmd")
- }
- assert.Equal(t, model.(Model).issue.Title, testIssue.Title)
- assert.Equal(t, Model{issue: testIssue}.renderIssue(), cmd())
- }
- func Test_Model_Update_renderIssueCollection(t *testing.T) {
- testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
- testModel := Model{}
- model, cmd := testModel.Update(tea.Msg(testCollection))
- if cmd == nil {
- assert.Fail(t, "should return cmd")
- }
- assert.Equal(t, len(testCollection), len(model.(Model).collection))
- assert.Equal(t, Model{collection: testCollection}.renderIssueCollection(), cmd())
- }
- func Test_Model_Update_updates_content(t *testing.T) {
- testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
- testModel := Model{issue: testIssue}
- testModel.content = testModel.renderIssue().(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{issue: 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 := Model{issue: testIssue}.renderIssue()
- 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")
- }
- assert.Equal(t, Model{issue: testIssue}.renderIssue().(string), model.(Model).View())
- render2 := Model{}.View()
- assert.Equal(t, "loading...", render2)
- }
|