Explorar el Código

Re-wrote tui tests, ensures 93% test coverage

arianagiroux hace 3 semanas
padre
commit
f3a74602ce
Se han modificado 3 ficheros con 127 adiciones y 22 borrados
  1. 2 3
      Readme.md
  2. 0 9
      cmd/issues.go
  3. 125 10
      tui_test.go

+ 2 - 3
Readme.md

@@ -14,9 +14,8 @@ go install cmd/issues.go
 
 ## TODO
 
-- `cmd/issues.go:// TODO implement interface for browse bugs in folder`
-- `io.go:func WriteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement`
-- `io.go:func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement`
+io.go:func WriteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement
+io.go:func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement
 
 ## See also
 

+ 0 - 9
cmd/issues.go

@@ -1,12 +1,3 @@
-// TODO implement interface for browse bugs in folder
-//
-//	For example:
-//		If the user provides the path of a folder that matches spec for a bug,
-//		just display that bug. Otherwise treat the specified path as a collection
-//		of bugs.
-//
-//		See Also:
-//			- charmbracelet/bubbles directory and tree explorer
 package main
 
 import (

+ 125 - 10
tui_test.go

@@ -9,21 +9,130 @@ import (
 )
 
 func Test_Model_Init(t *testing.T) {
-	t.Skip()
-	assert.Nil(t, Model{}.Init())
+	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) {
-	testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
-	testModel := Model{issue: testIssue}
-	testMsg := tea.KeyMsg{}
+	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)
 
-	cmdValue := cmd()
-	assert.IsType(t, tea.QuitMsg{}, cmdValue)
+	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}
@@ -44,9 +153,15 @@ func Test_Model_renderIssue(t *testing.T) {
 }
 
 func Test_Model_View(t *testing.T) {
-	t.Skip("skip until view lifecycle complete")
 	testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
-	testRender := Model{issue: testIssue}.View()
+	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())
 
-	assert.True(t, strings.Contains(testRender, "test description"))
+	render2 := Model{}.View()
+	assert.Equal(t, "loading...", render2)
 }