github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/list/http_test.go (about) 1 package list 2 3 import ( 4 "net/http" 5 "reflect" 6 "testing" 7 8 "github.com/ungtb10d/cli/v2/internal/ghrepo" 9 prShared "github.com/ungtb10d/cli/v2/pkg/cmd/pr/shared" 10 "github.com/ungtb10d/cli/v2/pkg/httpmock" 11 ) 12 13 func Test_listPullRequests(t *testing.T) { 14 type args struct { 15 repo ghrepo.Interface 16 filters prShared.FilterOptions 17 limit int 18 } 19 tests := []struct { 20 name string 21 args args 22 httpStub func(*testing.T, *httpmock.Registry) 23 wantErr bool 24 }{ 25 { 26 name: "default", 27 args: args{ 28 repo: ghrepo.New("OWNER", "REPO"), 29 limit: 30, 30 filters: prShared.FilterOptions{ 31 State: "open", 32 }, 33 }, 34 httpStub: func(t *testing.T, r *httpmock.Registry) { 35 r.Register( 36 httpmock.GraphQL(`query PullRequestList\b`), 37 httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { 38 want := map[string]interface{}{ 39 "owner": "OWNER", 40 "repo": "REPO", 41 "state": []interface{}{"OPEN"}, 42 "limit": float64(30), 43 } 44 if !reflect.DeepEqual(vars, want) { 45 t.Errorf("got GraphQL variables %#v, want %#v", vars, want) 46 } 47 })) 48 }, 49 }, 50 { 51 name: "closed", 52 args: args{ 53 repo: ghrepo.New("OWNER", "REPO"), 54 limit: 30, 55 filters: prShared.FilterOptions{ 56 State: "closed", 57 }, 58 }, 59 httpStub: func(t *testing.T, r *httpmock.Registry) { 60 r.Register( 61 httpmock.GraphQL(`query PullRequestList\b`), 62 httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { 63 want := map[string]interface{}{ 64 "owner": "OWNER", 65 "repo": "REPO", 66 "state": []interface{}{"CLOSED", "MERGED"}, 67 "limit": float64(30), 68 } 69 if !reflect.DeepEqual(vars, want) { 70 t.Errorf("got GraphQL variables %#v, want %#v", vars, want) 71 } 72 })) 73 }, 74 }, 75 { 76 name: "with labels", 77 args: args{ 78 repo: ghrepo.New("OWNER", "REPO"), 79 limit: 30, 80 filters: prShared.FilterOptions{ 81 State: "open", 82 Labels: []string{"hello", "one world"}, 83 }, 84 }, 85 httpStub: func(t *testing.T, r *httpmock.Registry) { 86 r.Register( 87 httpmock.GraphQL(`query PullRequestSearch\b`), 88 httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { 89 want := map[string]interface{}{ 90 "q": `label:"one world" label:hello repo:OWNER/REPO state:open type:pr`, 91 "limit": float64(30), 92 } 93 if !reflect.DeepEqual(vars, want) { 94 t.Errorf("got GraphQL variables %#v, want %#v", vars, want) 95 } 96 })) 97 }, 98 }, 99 { 100 name: "with author", 101 args: args{ 102 repo: ghrepo.New("OWNER", "REPO"), 103 limit: 30, 104 filters: prShared.FilterOptions{ 105 State: "open", 106 Author: "monalisa", 107 }, 108 }, 109 httpStub: func(t *testing.T, r *httpmock.Registry) { 110 r.Register( 111 httpmock.GraphQL(`query PullRequestSearch\b`), 112 httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { 113 want := map[string]interface{}{ 114 "q": "author:monalisa repo:OWNER/REPO state:open type:pr", 115 "limit": float64(30), 116 } 117 if !reflect.DeepEqual(vars, want) { 118 t.Errorf("got GraphQL variables %#v, want %#v", vars, want) 119 } 120 })) 121 }, 122 }, 123 { 124 name: "with search", 125 args: args{ 126 repo: ghrepo.New("OWNER", "REPO"), 127 limit: 30, 128 filters: prShared.FilterOptions{ 129 State: "open", 130 Search: "one world in:title", 131 }, 132 }, 133 httpStub: func(t *testing.T, r *httpmock.Registry) { 134 r.Register( 135 httpmock.GraphQL(`query PullRequestSearch\b`), 136 httpmock.GraphQLQuery(`{"data":{}}`, func(query string, vars map[string]interface{}) { 137 want := map[string]interface{}{ 138 "q": "one world in:title repo:OWNER/REPO state:open type:pr", 139 "limit": float64(30), 140 } 141 if !reflect.DeepEqual(vars, want) { 142 t.Errorf("got GraphQL variables %#v, want %#v", vars, want) 143 } 144 })) 145 }, 146 }, 147 } 148 for _, tt := range tests { 149 t.Run(tt.name, func(t *testing.T) { 150 reg := &httpmock.Registry{} 151 if tt.httpStub != nil { 152 tt.httpStub(t, reg) 153 } 154 httpClient := &http.Client{Transport: reg} 155 156 _, err := listPullRequests(httpClient, tt.args.repo, tt.args.filters, tt.args.limit) 157 if (err != nil) != tt.wantErr { 158 t.Errorf("listPullRequests() error = %v, wantErr %v", err, tt.wantErr) 159 return 160 } 161 }) 162 } 163 }