Skip to content

Commit 02eeb55

Browse files
authored
test: assert output for collaborator list command (#16)
* test: assert output for collaborator list command * test: not match against 10 collaborators
1 parent 8b0cf18 commit 02eeb55

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

cmd/collaborators/collaborators_test.go

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,45 @@ import (
2929

3030
func TestCollaboratorsCommand(t *testing.T) {
3131
tests := map[string]struct {
32-
App types.App
33-
Collaborators []types.SlackUser
32+
app types.App
33+
collaborators []types.SlackUser
34+
expectedOutputs []string
3435
}{
3536
"lists no collaborators if none exist": {
36-
App: types.App{
37+
app: types.App{
3738
AppID: "A001",
3839
},
39-
Collaborators: []types.SlackUser{},
40+
collaborators: []types.SlackUser{},
41+
expectedOutputs: []string{
42+
" 0 collaborators", // Include space to not match "10 collaborators"
43+
},
4044
},
4145
"lists the collaborator if one exists": {
42-
App: types.App{
46+
app: types.App{
4347
AppID: "A002",
4448
},
45-
Collaborators: []types.SlackUser{
49+
collaborators: []types.SlackUser{
4650
{
4751
ID: "USLACKBOT",
4852
UserName: "slackbot",
4953
5054
PermissionType: types.OWNER,
5155
},
5256
},
57+
expectedOutputs: []string{
58+
"1 collaborator",
59+
// User info: slackbot
60+
"USLACKBOT",
61+
"slackbot",
62+
63+
string(types.OWNER),
64+
},
5365
},
5466
"lists all collaborators if many exist": {
55-
App: types.App{
67+
app: types.App{
5668
AppID: "A002",
5769
},
58-
Collaborators: []types.SlackUser{
70+
collaborators: []types.SlackUser{
5971
{
6072
ID: "USLACKBOT",
6173
UserName: "slackbot",
@@ -69,37 +81,54 @@ func TestCollaboratorsCommand(t *testing.T) {
6981
PermissionType: types.READER,
7082
},
7183
},
84+
expectedOutputs: []string{
85+
"2 collaborators",
86+
// User info: slackbot
87+
"USLACKBOT",
88+
"slackbot",
89+
90+
string(types.OWNER),
91+
// User info: bookworm
92+
"U00READER",
93+
"bookworm",
94+
95+
string(types.READER),
96+
},
7297
},
7398
}
7499

75100
for name, tt := range tests {
76101
t.Run(name, func(t *testing.T) {
77102
appSelectMock := prompts.NewAppSelectMock()
78103
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt
79-
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.App, Auth: types.SlackAuth{}}, nil)
104+
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.app, Auth: types.SlackAuth{}}, nil)
80105
clientsMock := shared.NewClientsMock()
81106
clientsMock.AddDefaultMocks()
82107
clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything).
83-
Return(tt.Collaborators, nil)
108+
Return(tt.collaborators, nil)
84109
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
85110
clients.SDKConfig = hooks.NewSDKConfigMock()
86111
})
87112

88113
err := NewCommand(clients).Execute()
89114
require.NoError(t, err)
90-
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.App.AppID)
115+
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.app.AppID)
91116
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListSuccess, mock.Anything)
92117
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCount, []string{
93-
fmt.Sprintf("%d", len(tt.Collaborators)),
118+
fmt.Sprintf("%d", len(tt.collaborators)),
94119
})
95-
for _, collaborator := range tt.Collaborators {
120+
for _, collaborator := range tt.collaborators {
96121
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCollaborator, []string{
97122
collaborator.ID,
98123
collaborator.UserName,
99124
collaborator.Email,
100125
string(collaborator.PermissionType),
101126
})
102127
}
128+
output := clientsMock.GetCombinedOutput()
129+
for _, expectedOutput := range tt.expectedOutputs {
130+
require.Contains(t, output, expectedOutput)
131+
}
103132
})
104133
}
105134
}

cmd/collaborators/list_test.go

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,45 @@ import (
2929

3030
func TestListCommand(t *testing.T) {
3131
tests := map[string]struct {
32-
App types.App
33-
Collaborators []types.SlackUser
32+
app types.App
33+
collaborators []types.SlackUser
34+
expectedOutputs []string
3435
}{
3536
"lists no collaborators if none exist": {
36-
App: types.App{
37+
app: types.App{
3738
AppID: "A001",
3839
},
39-
Collaborators: []types.SlackUser{},
40+
collaborators: []types.SlackUser{},
41+
expectedOutputs: []string{
42+
"0 collaborators",
43+
},
4044
},
4145
"lists the collaborator if one exists": {
42-
App: types.App{
46+
app: types.App{
4347
AppID: "A002",
4448
},
45-
Collaborators: []types.SlackUser{
49+
collaborators: []types.SlackUser{
4650
{
4751
ID: "USLACKBOT",
4852
UserName: "slackbot",
4953
5054
PermissionType: types.OWNER,
5155
},
5256
},
57+
expectedOutputs: []string{
58+
"1 collaborator",
59+
// User info: slackbot
60+
"USLACKBOT",
61+
"slackbot",
62+
63+
string(types.OWNER),
64+
},
5365
},
5466
"lists all collaborators if many exist": {
55-
App: types.App{
67+
app: types.App{
5668
AppID: "A002",
5769
},
58-
Collaborators: []types.SlackUser{
70+
collaborators: []types.SlackUser{
5971
{
6072
ID: "USLACKBOT",
6173
UserName: "slackbot",
@@ -69,37 +81,54 @@ func TestListCommand(t *testing.T) {
6981
PermissionType: types.READER,
7082
},
7183
},
84+
expectedOutputs: []string{
85+
"2 collaborators",
86+
// User info: slackbot
87+
"USLACKBOT",
88+
"slackbot",
89+
90+
string(types.OWNER),
91+
// User info: bookworm
92+
"U00READER",
93+
"bookworm",
94+
95+
string(types.READER),
96+
},
7297
},
7398
}
7499

75100
for name, tt := range tests {
76101
t.Run(name, func(t *testing.T) {
77102
appSelectMock := prompts.NewAppSelectMock()
78103
teamAppSelectPromptFunc = appSelectMock.TeamAppSelectPrompt
79-
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.App, Auth: types.SlackAuth{}}, nil)
104+
appSelectMock.On("TeamAppSelectPrompt").Return(prompts.SelectedApp{App: tt.app, Auth: types.SlackAuth{}}, nil)
80105
clientsMock := shared.NewClientsMock()
81106
clientsMock.AddDefaultMocks()
82107
clientsMock.ApiInterface.On("ListCollaborators", mock.Anything, mock.Anything, mock.Anything).
83-
Return(tt.Collaborators, nil)
108+
Return(tt.collaborators, nil)
84109
clients := shared.NewClientFactory(clientsMock.MockClientFactory(), func(clients *shared.ClientFactory) {
85110
clients.SDKConfig = hooks.NewSDKConfigMock()
86111
})
87112

88113
err := NewListCommand(clients).Execute()
89114
require.NoError(t, err)
90-
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.App.AppID)
115+
clientsMock.ApiInterface.AssertCalled(t, "ListCollaborators", mock.Anything, mock.Anything, tt.app.AppID)
91116
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListSuccess, mock.Anything)
92117
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCount, []string{
93-
fmt.Sprintf("%d", len(tt.Collaborators)),
118+
fmt.Sprintf("%d", len(tt.collaborators)),
94119
})
95-
for _, collaborator := range tt.Collaborators {
120+
for _, collaborator := range tt.collaborators {
96121
clientsMock.IO.AssertCalled(t, "PrintTrace", mock.Anything, slacktrace.CollaboratorListCollaborator, []string{
97122
collaborator.ID,
98123
collaborator.UserName,
99124
collaborator.Email,
100125
string(collaborator.PermissionType),
101126
})
102127
}
128+
output := clientsMock.GetCombinedOutput()
129+
for _, expectedOutput := range tt.expectedOutputs {
130+
require.Contains(t, output, expectedOutput)
131+
}
103132
})
104133
}
105134
}

0 commit comments

Comments
 (0)