Skip to content

Commit

Permalink
Improve log stream display tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Apr 12, 2023
1 parent e9e5be1 commit 59e23d8
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions internal/display/display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
})
}

Expand Down

0 comments on commit 59e23d8

Please sign in to comment.