tui_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package issues
  2. import (
  3. "strings"
  4. "testing"
  5. tea "github.com/charmbracelet/bubbletea"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_Model_Init(t *testing.T) {
  9. testModel := Model{Path: "tests/bugs/test-1"}
  10. testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
  11. assert.Equal(t, testModel.Init()(), tea.Msg(testIssue))
  12. testModel = Model{Path: "tests/bugs"}
  13. testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
  14. assert.Equal(t, testModel.Init()(), tea.Msg(testCollection))
  15. }
  16. func Test_Model_Update_quit_on_keymsg(t *testing.T) {
  17. testModel := Model{}
  18. testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'q'}}
  19. testMsg := tea.KeyMsg(testKey)
  20. model, cmd := testModel.Update(testMsg)
  21. assert.Equal(t, testModel, model)
  22. if cmd != nil {
  23. cmdMsg := cmd()
  24. assert.Equal(t, tea.QuitMsg{}, cmdMsg)
  25. } else {
  26. assert.Fail(t, "should return cmd!")
  27. }
  28. }
  29. func Test_Model_Update_scroll_up(t *testing.T) {
  30. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
  31. testModel := Model{widget: ic}
  32. testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
  33. testMsg := tea.KeyMsg(testKey)
  34. model, cmd := testModel.Update(testMsg)
  35. widget, _ := model.(Model).widget.(IssueCollection)
  36. assert.Equal(t, ic.selection+1, widget.selection)
  37. if cmd == nil {
  38. assert.Fail(t, "should return tea.Cmd")
  39. }
  40. // test select wraparound
  41. ic.selection = len(ic.Collection) - 1
  42. testModel = Model{widget: ic}
  43. model, cmd = testModel.Update(testMsg)
  44. widget, _ = model.(Model).widget.(IssueCollection)
  45. assert.Equal(t, 0, widget.selection)
  46. if cmd == nil {
  47. assert.Fail(t, "should return tea.Cmd")
  48. }
  49. }
  50. func Test_Model_Update_scroll_down(t *testing.T) {
  51. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
  52. testModel := Model{widget: ic}
  53. testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
  54. testMsg := tea.KeyMsg(testKey)
  55. model, cmd := testModel.Update(testMsg)
  56. widget, _ := model.(Model).widget.(IssueCollection)
  57. assert.Equal(t, len(ic.Collection)-1, widget.selection)
  58. if cmd == nil {
  59. assert.Fail(t, "should return tea.Cmd")
  60. }
  61. // test select wraparound
  62. ic.selection = len(ic.Collection) - 1
  63. testModel = Model{widget: ic}
  64. model, _ = testModel.Update(testMsg)
  65. widget, _ = model.(Model).widget.(IssueCollection)
  66. assert.Equal(t, len(ic.Collection)-2, widget.selection)
  67. // if cmd == nil {
  68. // assert.Fail(t, "should return tea.Cmd")
  69. // }
  70. // ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
  71. // testModel := Model{widget: ic}
  72. // testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
  73. // testMsg := tea.KeyMsg(testKey)
  74. // model, cmd := testModel.Update(testMsg)
  75. // widget, _ := testModel.widget.(IssueCollection)
  76. // assert.Equal(t, len(ic.Collection)-1, widget.selection)
  77. // if cmd == nil {
  78. // assert.Fail(t, "should return tea.Cmd")
  79. // }
  80. // model, cmd = model.Update(testMsg)
  81. // assert.Equal(t, len(ic.Collection)-2, widget.selection)
  82. // if cmd == nil {
  83. // assert.Fail(t, "should return tea.Cmd")
  84. // }
  85. }
  86. func Test_Model_Update_load_on_enter(t *testing.T) {
  87. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs")
  88. testModel := Model{widget: ic}
  89. testKey := tea.Key{Type: tea.KeyEnter, Runes: []rune{}}
  90. testMsg := tea.KeyMsg(testKey)
  91. testPath := ic.Collection[ic.selection].Path
  92. model, cmd := testModel.Update(testMsg)
  93. assert.Equal(t, testPath, model.(Model).Path)
  94. if cmd == nil {
  95. assert.Fail(t, "should return tea.Cmd")
  96. }
  97. }
  98. func Test_Model_Update_renderIssue(t *testing.T) {
  99. testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
  100. testModel := Model{}
  101. model, cmd := testModel.Update(tea.Msg(testIssue))
  102. widget, _ := model.(Model).widget.(Issue)
  103. if cmd == nil {
  104. assert.Fail(t, "should return cmd")
  105. }
  106. assert.Equal(t, testIssue.Title, widget.Title)
  107. assert.Equal(t, testIssue.view(), cmd())
  108. }
  109. func Test_Model_Update_renderIssueCollection(t *testing.T) {
  110. testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
  111. testModel := Model{}
  112. model, cmd := testModel.Update(tea.Msg(testCollection))
  113. widget, _ := model.(Model).widget.(IssueCollection)
  114. if cmd == nil {
  115. assert.Fail(t, "should return cmd")
  116. }
  117. assert.Equal(t, len(testCollection.Collection), len(widget.Collection))
  118. assert.Equal(t, testCollection.view(), cmd())
  119. }
  120. func Test_Model_Update_updates_content(t *testing.T) {
  121. testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
  122. testModel := Model{widget: testIssue}
  123. testWidget := testModel.widget.(Issue)
  124. testModel.content = testWidget.view().(string)
  125. model, cmd := testModel.Update(tea.Msg(testModel.content))
  126. if cmd != nil {
  127. assert.Fail(t, "should not return cmd")
  128. }
  129. assert.Equal(t, testModel.content, model.(Model).content)
  130. }
  131. func Test_Model_Update_do_nothing(t *testing.T) {
  132. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  133. testModel := Model{widget: testIssue}
  134. var testMsg int
  135. model, cmd := testModel.Update(testMsg)
  136. assert.Equal(t, testModel, model)
  137. assert.Nil(t, cmd)
  138. }
  139. func Test_Model_renderIssue(t *testing.T) {
  140. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  141. testRender := testIssue.view()
  142. renderContent, _ := testRender.(string)
  143. assert.True(t, strings.Contains(renderContent, "test description"))
  144. }
  145. func Test_Model_View(t *testing.T) {
  146. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  147. model, cmd := Model{}.Update(tea.Msg(testIssue)) // gives render cmd
  148. model, cmd = model.Update(cmd()) // handle internal render
  149. if cmd != nil {
  150. assert.Fail(t, "should not return cmd")
  151. }
  152. testView := testIssue.view().(string) + "\nj/k: down/up\tenter: select\tq: quit"
  153. assert.Equal(t, testView, model.(Model).View())
  154. render2 := Model{}.View()
  155. assert.Equal(t, "loading...", render2)
  156. }