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