11package app_test
22
33import (
4+ "os"
5+ "strings"
46 "testing"
7+ "unicode/utf8"
58
69 tea "github.com/charmbracelet/bubbletea"
710 "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
812
913 "github.com/hedhyw/json-log-viewer/assets"
1014 "github.com/hedhyw/json-log-viewer/internal/app"
@@ -119,26 +123,9 @@ func TestAppQuit(t *testing.T) {
119123 })
120124}
121125
122- func newTestModel (tb testing.TB , content []byte ) app.Model {
123- tb .Helper ()
124-
125- testFile := tests .RequireCreateFile (tb , content )
126-
127- appModel := app .NewModel (testFile )
128- cmd := appModel .Init ()
129-
130- appModel , _ = toAppModel (appModel .Update (cmd ()))
131-
132- return appModel
133- }
134-
135- func toAppModel (teaModel tea.Model , cmd tea.Cmd ) (app.Model , tea.Cmd ) {
136- appModel , _ := teaModel .(app.Model )
137-
138- return appModel , cmd
139- }
140-
141126func TestAppViewFiltereRunes (t * testing.T ) {
127+ t .Parallel ()
128+
142129 appModel := newTestModel (t , assets .ExampleJSONLog ())
143130
144131 appModel , _ = toAppModel (appModel .Update (tea.KeyMsg {
@@ -160,3 +147,73 @@ func TestAppViewFiltereRunes(t *testing.T) {
160147 assert .NotEqual (t , tea .Quit (), cmd ())
161148 assert .True (t , appModel .IsFilterShown (), appModel .View ())
162149}
150+
151+ func TestAppViewReload (t * testing.T ) {
152+ t .Parallel ()
153+
154+ const expected = "included"
155+
156+ const (
157+ jsonFile = `
158+ {"time":"1970-01-01T00:00:00.00","level":"INFO","message": "test2"}
159+ {"time":"1970-01-01T00:00:00.00","level":"INFO","message": "test1"}
160+ `
161+
162+ jsonFileUpdated = `
163+ {"time":"1970-01-01T00:00:00.00","level":"INFO","message": "` + expected + `"}
164+ ` + jsonFile
165+ )
166+
167+ appModel := newTestModel (t , []byte (jsonFile ))
168+
169+ rendered := appModel .View ()
170+ assert .NotContains (t , rendered , expected )
171+
172+ err := os .WriteFile (appModel .File (), []byte (jsonFileUpdated ), os .ModePerm )
173+ require .NoError (t , err )
174+
175+ appModel , _ = toAppModel (appModel .Update (tea.KeyMsg {
176+ Type : tea .KeyUp ,
177+ }))
178+
179+ rendered = appModel .View ()
180+ assert .NotContains (t , rendered , expected )
181+ }
182+
183+ func TestAppViewResized (t * testing.T ) {
184+ t .Parallel ()
185+
186+ appModel := newTestModel (t , assets .ExampleJSONLog ())
187+
188+ windowSize := tea.WindowSizeMsg {
189+ Width : 60 ,
190+ Height : 10 ,
191+ }
192+
193+ appModel , _ = toAppModel (appModel .Update (windowSize ))
194+
195+ rendered := appModel .View ()
196+ lines := strings .Split (rendered , "\n " )
197+ if assert .NotEmpty (t , lines , rendered ) {
198+ assert .Less (t , utf8 .RuneCountInString (lines [0 ]), windowSize .Width , rendered )
199+ }
200+ }
201+
202+ func newTestModel (tb testing.TB , content []byte ) app.Model {
203+ tb .Helper ()
204+
205+ testFile := tests .RequireCreateFile (tb , content )
206+
207+ appModel := app .NewModel (testFile )
208+ cmd := appModel .Init ()
209+
210+ appModel , _ = toAppModel (appModel .Update (cmd ()))
211+
212+ return appModel
213+ }
214+
215+ func toAppModel (teaModel tea.Model , cmd tea.Cmd ) (app.Model , tea.Cmd ) {
216+ appModel , _ := teaModel .(app.Model )
217+
218+ return appModel , cmd
219+ }
0 commit comments