github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/issue/list/http_test.go (about) 1 package list 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/cli/cli/api" 11 "github.com/cli/cli/internal/ghrepo" 12 prShared "github.com/cli/cli/pkg/cmd/pr/shared" 13 "github.com/cli/cli/pkg/httpmock" 14 ) 15 16 func TestIssueList(t *testing.T) { 17 http := &httpmock.Registry{} 18 client := api.NewClient(api.ReplaceTripper(http)) 19 20 http.Register( 21 httpmock.GraphQL(`query IssueList\b`), 22 httpmock.StringResponse(` 23 { "data": { "repository": { 24 "hasIssuesEnabled": true, 25 "issues": { 26 "nodes": [], 27 "pageInfo": { 28 "hasNextPage": true, 29 "endCursor": "ENDCURSOR" 30 } 31 } 32 } } } 33 `), 34 ) 35 http.Register( 36 httpmock.GraphQL(`query IssueList\b`), 37 httpmock.StringResponse(` 38 { "data": { "repository": { 39 "hasIssuesEnabled": true, 40 "issues": { 41 "nodes": [], 42 "pageInfo": { 43 "hasNextPage": false, 44 "endCursor": "ENDCURSOR" 45 } 46 } 47 } } } 48 `), 49 ) 50 51 repo, _ := ghrepo.FromFullName("OWNER/REPO") 52 filters := prShared.FilterOptions{ 53 Entity: "issue", 54 State: "open", 55 } 56 _, err := listIssues(client, repo, filters, 251) 57 if err != nil { 58 t.Fatalf("unexpected error: %v", err) 59 } 60 61 if len(http.Requests) != 2 { 62 t.Fatalf("expected 2 HTTP requests, seen %d", len(http.Requests)) 63 } 64 var reqBody struct { 65 Query string 66 Variables map[string]interface{} 67 } 68 69 bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body) 70 _ = json.Unmarshal(bodyBytes, &reqBody) 71 if reqLimit := reqBody.Variables["limit"].(float64); reqLimit != 100 { 72 t.Errorf("expected 100, got %v", reqLimit) 73 } 74 if _, cursorPresent := reqBody.Variables["endCursor"]; cursorPresent { 75 t.Error("did not expect first request to pass 'endCursor'") 76 } 77 78 bodyBytes, _ = ioutil.ReadAll(http.Requests[1].Body) 79 _ = json.Unmarshal(bodyBytes, &reqBody) 80 if endCursor := reqBody.Variables["endCursor"].(string); endCursor != "ENDCURSOR" { 81 t.Errorf("expected %q, got %q", "ENDCURSOR", endCursor) 82 } 83 } 84 85 func TestIssueList_pagination(t *testing.T) { 86 http := &httpmock.Registry{} 87 client := api.NewClient(api.ReplaceTripper(http)) 88 89 http.Register( 90 httpmock.GraphQL(`query IssueList\b`), 91 httpmock.StringResponse(` 92 { "data": { "repository": { 93 "hasIssuesEnabled": true, 94 "issues": { 95 "nodes": [ 96 { 97 "title": "issue1", 98 "labels": { "nodes": [ { "name": "bug" } ], "totalCount": 1 }, 99 "assignees": { "nodes": [ { "login": "user1" } ], "totalCount": 1 } 100 } 101 ], 102 "pageInfo": { 103 "hasNextPage": true, 104 "endCursor": "ENDCURSOR" 105 }, 106 "totalCount": 2 107 } 108 } } } 109 `), 110 ) 111 112 http.Register( 113 httpmock.GraphQL(`query IssueList\b`), 114 httpmock.StringResponse(` 115 { "data": { "repository": { 116 "hasIssuesEnabled": true, 117 "issues": { 118 "nodes": [ 119 { 120 "title": "issue2", 121 "labels": { "nodes": [ { "name": "enhancement" } ], "totalCount": 1 }, 122 "assignees": { "nodes": [ { "login": "user2" } ], "totalCount": 1 } 123 } 124 ], 125 "pageInfo": { 126 "hasNextPage": false, 127 "endCursor": "ENDCURSOR" 128 }, 129 "totalCount": 2 130 } 131 } } } 132 `), 133 ) 134 135 repo := ghrepo.New("OWNER", "REPO") 136 res, err := listIssues(client, repo, prShared.FilterOptions{}, 0) 137 if err != nil { 138 t.Fatalf("IssueList() error = %v", err) 139 } 140 141 assert.Equal(t, 2, res.TotalCount) 142 assert.Equal(t, 2, len(res.Issues)) 143 144 getLabels := func(i api.Issue) []string { 145 var labels []string 146 for _, l := range i.Labels.Nodes { 147 labels = append(labels, l.Name) 148 } 149 return labels 150 } 151 getAssignees := func(i api.Issue) []string { 152 var logins []string 153 for _, u := range i.Assignees.Nodes { 154 logins = append(logins, u.Login) 155 } 156 return logins 157 } 158 159 assert.Equal(t, []string{"bug"}, getLabels(res.Issues[0])) 160 assert.Equal(t, []string{"user1"}, getAssignees(res.Issues[0])) 161 assert.Equal(t, []string{"enhancement"}, getLabels(res.Issues[1])) 162 assert.Equal(t, []string{"user2"}, getAssignees(res.Issues[1])) 163 }