Преглед на файлове

Major refactoring of tui tests to implement widget interface

arianagiroux преди 2 седмици
родител
ревизия
aaf2ea5cf0
променени са 1 файла, в които са добавени 140 реда и са изтрити 97 реда
  1. 140 97
      tui_test.go

+ 140 - 97
tui_test.go

@@ -6,21 +6,31 @@ import (
 	"testing"
 
 	tea "github.com/charmbracelet/bubbletea"
-	"github.com/charmbracelet/lipgloss"
 	"github.com/stretchr/testify/assert"
 )
 
-func Test_Model_Init(t *testing.T) {
+// Model tests  ---------------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+func Test_Model_load_cmd_loads_issue(t *testing.T)      {}
+func Test_Model_load_cmd_loads_collection(t *testing.T) {}
+func Test_Model_load_cmd_loads_edit(t *testing.T)       {}
+
+func Test_Model_Init_throws_load(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))
+	payloadFunc := testModel.Init()
+	assert.IsType(t, tea.Cmd(testModel.load), payloadFunc, tea.Cmd(testModel.load), payloadFunc)
 
 	testModel = Model{Path: "tests/bugs"}
-	testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
-	assert.Equal(t, testModel.Init()(), tea.Msg(testCollection))
+	payloadFunc = testModel.Init()
+	assert.IsType(t, tea.Cmd(testModel.load), payloadFunc, tea.Cmd(testModel.load), payloadFunc)
+
+	testModel = Model{Path: "tests/bugs/doesntexistyet"}
+	payloadFunc = testModel.Init()
+	assert.IsType(t, tea.Cmd(testModel.load), payloadFunc, tea.Cmd(testModel.load), payloadFunc)
 }
 
-func Test_Model_Update_quit_on_keymsg(t *testing.T) {
+func Test_Model_Update_quit_on_ctrl_c(t *testing.T) {
 	testModel := Model{}
 	testKey := tea.Key{Type: tea.KeyCtrlC}
 	testMsg := tea.KeyMsg(testKey)
@@ -36,7 +46,88 @@ func Test_Model_Update_quit_on_keymsg(t *testing.T) {
 	}
 }
 
-func Test_Model_Update_scroll_up(t *testing.T) {
+func Test_Model_Update_renderIssue(t *testing.T) {
+	testIssue, _ := Issue{}.NewFromPath("tests/bugs/test-1")
+	model := tea.Model(Model{widget: testIssue})
+	testModel := Model{widget: testIssue}
+
+	model, cmd := model.Update(tea.Msg(testIssue))
+	if cmd == nil {
+		assert.Fail(t, "should return cmd")
+	}
+	widget, ok := model.(Model).widget.(Issue)
+	assert.True(t, ok, "widget should be type Issue")
+
+	assert.Equal(t, testIssue.Title, widget.Title)
+	assert.Equal(t, testModel.View(), model.View())
+}
+
+func Test_Model_Update_renderIssueCollection(t *testing.T) {
+	testCollection, _ := IssueCollection{}.NewFromPath("tests/bugs")
+	model := tea.Model(Model{widget: testCollection})
+	testModel := Model{widget: testCollection}
+
+	var cmd tea.Cmd
+	model, cmd = testModel.Update(tea.Msg(testCollection))
+	if cmd == nil {
+		assert.Fail(t, "should return cmd")
+	}
+	widget, ok := model.(Model).widget.(IssueCollection)
+	assert.True(t, ok, "widget should be type Issue")
+
+	assert.Equal(t, len(testCollection.Collection), len(widget.Collection))
+	assert.Equal(t, testModel.View(), model.View())
+}
+
+func Test_Model_Update_updates_content_on_render(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 cmds")
+	}
+
+	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}
+
+	// create a type wrapper for switch/case channel routing
+	type testMsg int
+	var tm testMsg
+
+	model, cmd := testModel.Update(tm)
+	assert.Equal(t, testModel, model)
+
+	assert.NotNil(t, cmd)
+}
+
+// widget tests  --------------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+// -------- Issue widget definitions ------------------------------------------
+// ----------------------------------------------------------------------------
+
+func Test_Issue_render(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_Issue_keyhelp(t *testing.T) {}
+
+// -------- IssueCollection widget tests --------------------------------------
+// ----------------------------------------------------------------------------
+
+func Test_IssueCollection_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'}}
@@ -62,7 +153,7 @@ func Test_Model_Update_scroll_up(t *testing.T) {
 	}
 }
 
-func Test_Model_Update_scroll_down(t *testing.T) {
+func Test_IssueCollection_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'}}
@@ -107,109 +198,39 @@ func Test_Model_Update_scroll_down(t *testing.T) {
 	// }
 }
 
-func Test_Model_Update_load_on_enter(t *testing.T) {
-	ic, _ := IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs")
-	testModel := Model{widget: ic}
+func Test_IssueCollection_update_load_on_enter(t *testing.T) {
+	var ic widget
+	ic, _ = IssueCollection.NewFromPath(IssueCollection{}, "tests/bugs")
 	testKey := tea.Key{Type: tea.KeyEnter, Runes: []rune{}}
 	testMsg := tea.KeyMsg(testKey)
-	testPath := ic.Collection[ic.selection].Path
+	testPath := ic.(IssueCollection).Collection[ic.(IssueCollection).selection].Path
 
-	model, cmd := testModel.Update(testMsg)
-	assert.Equal(t, testPath, model.(Model).Path)
+	var cmd tea.Cmd
+	ic, cmd = ic.update(testMsg)
+	assert.Equal(t, testPath, ic.(IssueCollection).Path)
+	// assert.IsType(t, Issue{}, model.widget)
 
 	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)
-	testView = testView + lipgloss.NewStyle().Faint(true).Margin(1).Render(testIssue.keyhelp())
-	assert.Equal(t, testView, model.(Model).View())
+func Test_IssueCollection_render(t *testing.T)  {}
+func Test_IssueCollection_keyhelp(t *testing.T) {}
 
-	render2 := Model{}.View()
-	assert.Equal(t, "loading...", render2)
-}
+// -------- edit widget tests  ------------------------------------------------
+// ----------------------------------------------------------------------------
 
-func Test_create_create_cmd(t *testing.T) {
+func Test_edit_create_cmd(t *testing.T) {
 	// test data init
 	testData := make(map[string]string)
 
 	// because the resulting test data never gets written from memory to disk,
 	// we can set just about any test path
-	testC := initialCreateWidget("tests/bugs/test-create-in-memory", "lorem ipsum")
-	for i := range testC.inputFields {
-		testData[testC.inputFields[i].title] = fmt.Sprintf("test%d", i)
-		testC.inputFields[i].input.SetValue(fmt.Sprintf("test%d", i))
+	testC := newEditBlank("tests/bugs/test-create-in-memory", "lorem ipsum")
+	for i := range testC.(edit).inputFields {
+		testData[testC.(edit).inputFields[i].title] = fmt.Sprintf("test%d", i)
+		testC.(edit).inputFields[i].input.SetValue(fmt.Sprintf("test%d", i))
 	}
 
 	commaSplit := func(t string) []string {
@@ -250,7 +271,29 @@ func Test_create_create_cmd(t *testing.T) {
 		}
 	}
 
-	result := testC.create()
+	result := testC.(edit).createIssueObject()
 	assert.IsType(t, createResult(testIssue), result)
 	assert.Equal(t, createResult(testIssue).Path, result.(createResult).Path)
 }
+
+func Test_edit_render(t *testing.T)  {}
+func Test_edit_keyhelp(t *testing.T) {}
+
+// misc...---------------------------------------------------------------------
+// ----------------------------------------------------------------------------
+
+// duplicated by Test_Model_update_render*
+func Test_Model_View(t *testing.T) { // TODO DEPRECATE
+	testIssue, _ := Issue.NewFromPath(Issue{}, "tests/bugs/test-1")
+	testModel := Model{widget: testIssue}
+	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 return cmds")
+	}
+
+	assert.Equal(t, testModel.View(), model.(Model).View())
+
+	render2 := Model{}.View()
+	assert.Equal(t, "loading...", render2)
+}