tui_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_on_k(t *testing.T) {
  30. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
  31. testModel := Model{collection: ic}
  32. testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'k'}}
  33. testMsg := tea.KeyMsg(testKey)
  34. model, cmd := testModel.Update(testMsg)
  35. assert.Equal(t, testModel.selection+1, model.(Model).selection)
  36. if cmd == nil {
  37. assert.Fail(t, "should return tea.Cmd")
  38. }
  39. // test select wraparound
  40. testModel.selection = len(testModel.collection) - 1
  41. model, cmd = testModel.Update(testMsg)
  42. assert.Equal(t, 0, model.(Model).selection)
  43. if cmd == nil {
  44. assert.Fail(t, "should return tea.Cmd")
  45. }
  46. }
  47. func Test_Model_Update_scroll_on_j(t *testing.T) {
  48. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs/")
  49. testModel := Model{collection: ic}
  50. testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
  51. testMsg := tea.KeyMsg(testKey)
  52. model, cmd := testModel.Update(testMsg)
  53. assert.Equal(t, len(testModel.collection)-1, model.(Model).selection)
  54. if cmd == nil {
  55. assert.Fail(t, "should return tea.Cmd")
  56. }
  57. model, cmd = model.(Model).Update(testMsg)
  58. assert.Equal(t, len(testModel.collection)-2, model.(Model).selection)
  59. if cmd == nil {
  60. assert.Fail(t, "should return tea.Cmd")
  61. }
  62. }
  63. func Test_Model_Update_load_on_enter(t *testing.T) {
  64. ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs")
  65. testModel := Model{collection: ic}
  66. testKey := tea.Key{Type: tea.KeyEnter, Runes: []rune{}}
  67. testMsg := tea.KeyMsg(testKey)
  68. testPath := testModel.collection[testModel.selection].Path
  69. model, cmd := testModel.Update(testMsg)
  70. assert.Equal(t, testPath, model.(Model).Path)
  71. if cmd == nil {
  72. assert.Fail(t, "should return tea.Cmd")
  73. }
  74. }
  75. func Test_Model_Update_renderIssue(t *testing.T) {
  76. testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
  77. testModel := Model{}
  78. model, cmd := testModel.Update(tea.Msg(testIssue))
  79. if cmd == nil {
  80. assert.Fail(t, "should return cmd")
  81. }
  82. assert.Equal(t, model.(Model).issue.Title, testIssue.Title)
  83. assert.Equal(t, Model{issue: testIssue}.renderIssue(), cmd())
  84. }
  85. func Test_Model_Update_renderIssueCollection(t *testing.T) {
  86. testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
  87. testModel := Model{}
  88. model, cmd := testModel.Update(tea.Msg(testCollection))
  89. if cmd == nil {
  90. assert.Fail(t, "should return cmd")
  91. }
  92. assert.Equal(t, len(testCollection), len(model.(Model).collection))
  93. assert.Equal(t, Model{collection: testCollection}.renderIssueCollection(), cmd())
  94. }
  95. func Test_Model_Update_updates_content(t *testing.T) {
  96. testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
  97. testModel := Model{issue: testIssue}
  98. testModel.content = testModel.renderIssue().(string)
  99. model, cmd := testModel.Update(tea.Msg(testModel.content))
  100. if cmd != nil {
  101. assert.Fail(t, "should not return cmd")
  102. }
  103. assert.Equal(t, testModel.content, model.(Model).content)
  104. }
  105. func Test_Model_Update_do_nothing(t *testing.T) {
  106. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  107. testModel := Model{issue: testIssue}
  108. var testMsg int
  109. model, cmd := testModel.Update(testMsg)
  110. assert.Equal(t, testModel, model)
  111. assert.Nil(t, cmd)
  112. }
  113. func Test_Model_renderIssue(t *testing.T) {
  114. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  115. testRender := Model{issue: testIssue}.renderIssue()
  116. renderContent, _ := testRender.(string)
  117. assert.True(t, strings.Contains(renderContent, "test description"))
  118. }
  119. func Test_Model_View(t *testing.T) {
  120. testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
  121. model, cmd := Model{}.Update(tea.Msg(testIssue)) // gives render cmd
  122. model, cmd = model.Update(cmd()) // handle internal render
  123. if cmd != nil {
  124. assert.Fail(t, "should not return cmd")
  125. }
  126. assert.Equal(t, Model{issue: testIssue}.renderIssue().(string), model.(Model).View())
  127. render2 := Model{}.View()
  128. assert.Equal(t, "loading...", render2)
  129. }