github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/issue/list/http_test.go (about)

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