浏览代码

Added tests for some of tui

arianagiroux 1 周之前
父节点
当前提交
213fce408d
共有 1 个文件被更改,包括 67 次插入0 次删除
  1. 67 0
      tui_test.go

+ 67 - 0
tui_test.go

@@ -0,0 +1,67 @@
+package pingo
+
+import (
+	"testing"
+	"time"
+
+	tea "github.com/charmbracelet/bubbletea"
+	"github.com/stretchr/testify/assert"
+)
+
+func Test_InitialModel(t *testing.T) {
+	model := InitialModel([]string{"test"}, time.Duration(time.Second*1), 10)
+
+	assert.True(t, len(model.Addresses) == 1)
+	assert.True(t, model.viewport.Width == 0)
+	assert.Equal(t, model.UpdateSpeed, time.Duration(time.Second*1))
+	assert.Equal(t, model.ChartHeight, 10)
+}
+
+func Test_Model_Init(t *testing.T) {
+	testModel := Model{}
+	result := testModel.Init()
+	assert.IsType(t, new(tea.Cmd), &result)
+
+	result2 := result()
+	assert.IsType(t, new(tea.Msg), &result2)
+
+	_, ok := result2.(tickMsg)
+	assert.True(t, ok)
+}
+
+func Test_Model_Update_window_resize(t *testing.T) {
+	testMsg := tea.WindowSizeMsg{Width: 10, Height: 10}
+	model := tea.Model(InitialModel([]string{"test"}, time.Duration(time.Second*1), 10))
+	assert.Equal(t, 0, model.(Model).width)
+	assert.Equal(t, 0, model.(Model).viewport.YPosition)
+	assert.Equal(t, 1, len(model.(Model).Addresses))
+	assert.Equal(t, 80, model.(Model).Addresses[0].max_results)
+
+	model, _ = model.Update(testMsg)
+	assert.Equal(t, 10, model.(Model).width)
+	assert.Equal(t, 10, model.(Model).viewport.Width)
+	assert.Equal(t, 6, model.(Model).viewport.Height)
+	assert.Equal(t, 1, model.(Model).viewport.YPosition)
+	assert.Equal(t, 10, model.(Model).Addresses[0].max_results)
+}
+
+func Test_Model_Update_keymsg_j(t *testing.T) {
+	t.Skip()
+	testKey := tea.Key{Type: tea.KeyRunes, Runes: []rune{'j'}}
+	testMsg := tea.KeyMsg(testKey)
+	model := tea.Model(InitialModel([]string{"127.0.0.1", "127.0.0.1"}, time.Duration(time.Second*1), 0))
+	scrollPercent := model.(Model).viewport.ScrollPercent()
+	var msgs []tea.Msg
+	var cmd tea.Cmd
+	model, cmd = model.Update(tea.WindowSizeMsg{Width: 100, Height: 2000})
+	if cmd != nil {
+		msgs = append(msgs, cmd())
+	}
+	msgs = append(msgs, testMsg)
+	model, _ = model.Update(msgs)
+	assert.NotEqual(t, scrollPercent, model.(Model).viewport.ScrollPercent())
+}
+
+func Test_Model_View(t *testing.T)    {}
+func Test_Model_content(t *testing.T) {}
+func Test_Model_Poll(t *testing.T)    {}