From 59e23d87a229135e96609a40bea1b9650a667a4a Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Wed, 12 Apr 2023 11:07:41 +0200 Subject: [PATCH] Improve log stream display tests --- internal/display/display_test.go | 52 ++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/internal/display/display_test.go b/internal/display/display_test.go index 8f48743dc..ca8c47a84 100644 --- a/internal/display/display_test.go +++ b/internal/display/display_test.go @@ -8,6 +8,8 @@ import ( "github.com/auth0/go-auth0/management" "github.com/stretchr/testify/assert" + + "github.com/auth0/auth0-cli/internal/auth0" ) func TestTimeAgo(t *testing.T) { @@ -42,26 +44,58 @@ func TestTimeAgo(t *testing.T) { } func TestStream(t *testing.T) { - results := []View{} - stdout := &bytes.Buffer{} + var stdout bytes.Buffer mockRender := &Renderer{ MessageWriter: io.Discard, - ResultWriter: stdout, + ResultWriter: &stdout, + } + + results := []View{ + &logView{ + Log: &management.Log{ + LogID: auth0.String("354234"), + Type: auth0.String("sapi"), + Description: auth0.String("Update branding settings"), + }, + }, + &logView{ + Log: &management.Log{ + LogID: auth0.String("354235"), + Type: auth0.String("sapi"), + Description: auth0.String("Set custom text for a specific prompt"), + }, + }, } t.Run("Stream correctly handles nil channel", func(t *testing.T) { mockRender.Stream(results, nil) - assert.Len(t, stdout.Bytes(), 0) + expectedResult := `API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A +API Operation Set custom text for a specific prompt Jan 01 00:00:00.000 N/A N/A +` + assert.Equal(t, expectedResult, stdout.String()) }) t.Run("Stream successfully", func(t *testing.T) { viewChan := make(chan View) - go mockRender.Stream(results, viewChan) - mockLogID := "log1" - mockLog := management.Log{LogID: &mockLogID} - viewChan <- &logView{Log: &mockLog} - close(viewChan) + go func() { + mockRender.Stream(results, viewChan) + close(viewChan) + }() + + viewChan <- &logView{ + Log: &management.Log{ + LogID: auth0.String("354236"), + Type: auth0.String("sapi"), + Description: auth0.String("Update tenant settings"), + }, + } + + expectedResult := `API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A +API Operation Set custom text for a specific prompt Jan 01 00:00:00.000 N/A N/A +API Operation Update tenant settings Jan 01 00:00:00.000 N/A N/A +` + assert.Equal(t, expectedResult, stdout.String()) }) }