package list import ( "net/http" "reflect" "testing" "github.com/cli/cli/v2/pkg/httpmock" ) func Test_listOrgs(t *testing.T) { type args struct { limit int } tests := []struct { name string args args httpStub func(*testing.T, *httpmock.Registry) wantErr bool }{ { name: "default", args: args{ limit: 30, }, httpStub: func(t *testing.T, reg *httpmock.Registry) { reg.Register( httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data": {"viewer": {"login": "octocat"}}}`)) reg.Register( httpmock.GraphQL(`query OrganizationList\b`), httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { want := map[string]interface{}{ "user": "octocat", "limit": float64(30), } if !reflect.DeepEqual(vars, want) { t.Errorf("got GraphQL variables %#v, want %#v", vars, want) } })) }, }, { name: "with limit", args: args{ limit: 1, }, httpStub: func(t *testing.T, r *httpmock.Registry) { r.Register( httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data": {"viewer": {"login": "octocat"}}}`)) r.Register( httpmock.GraphQL(`query OrganizationList\b`), httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { want := map[string]interface{}{ "user": "octocat", "limit": float64(1), } if !reflect.DeepEqual(vars, want) { t.Errorf("got GraphQL variables %#v, want %#v", vars, want) } })) }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { reg := &httpmock.Registry{} if tt.httpStub != nil { tt.httpStub(t, reg) } httpClient := &http.Client{Transport: reg} _, err := listOrgs(httpClient, "octocat", tt.args.limit) if (err != nil) != tt.wantErr { t.Errorf("listOrgs() error = %v, wantErr %v", err, tt.wantErr) return } }) } }