github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/shared/lookup_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/andrewhsu/cli/v2/api"
     8  	"github.com/andrewhsu/cli/v2/internal/ghrepo"
     9  	"github.com/andrewhsu/cli/v2/pkg/httpmock"
    10  )
    11  
    12  func TestIssueFromArg(t *testing.T) {
    13  	type args struct {
    14  		baseRepoFn func() (ghrepo.Interface, error)
    15  		selector   string
    16  	}
    17  	tests := []struct {
    18  		name      string
    19  		args      args
    20  		httpStub  func(*httpmock.Registry)
    21  		wantIssue int
    22  		wantRepo  string
    23  		wantErr   bool
    24  	}{
    25  		{
    26  			name: "number argument",
    27  			args: args{
    28  				selector: "13",
    29  				baseRepoFn: func() (ghrepo.Interface, error) {
    30  					return ghrepo.FromFullName("OWNER/REPO")
    31  				},
    32  			},
    33  			httpStub: func(r *httpmock.Registry) {
    34  				r.Register(
    35  					httpmock.GraphQL(`query IssueByNumber\b`),
    36  					httpmock.StringResponse(`{"data":{"repository":{
    37  						"hasIssuesEnabled": true,
    38  						"issue":{"number":13}
    39  					}}}`))
    40  			},
    41  			wantIssue: 13,
    42  			wantRepo:  "https://github.com/OWNER/REPO",
    43  		},
    44  		{
    45  			name: "number with hash argument",
    46  			args: args{
    47  				selector: "#13",
    48  				baseRepoFn: func() (ghrepo.Interface, error) {
    49  					return ghrepo.FromFullName("OWNER/REPO")
    50  				},
    51  			},
    52  			httpStub: func(r *httpmock.Registry) {
    53  				r.Register(
    54  					httpmock.GraphQL(`query IssueByNumber\b`),
    55  					httpmock.StringResponse(`{"data":{"repository":{
    56  						"hasIssuesEnabled": true,
    57  						"issue":{"number":13}
    58  					}}}`))
    59  			},
    60  			wantIssue: 13,
    61  			wantRepo:  "https://github.com/OWNER/REPO",
    62  		},
    63  		{
    64  			name: "URL argument",
    65  			args: args{
    66  				selector:   "https://example.org/OWNER/REPO/issues/13#comment-123",
    67  				baseRepoFn: nil,
    68  			},
    69  			httpStub: func(r *httpmock.Registry) {
    70  				r.Register(
    71  					httpmock.GraphQL(`query IssueByNumber\b`),
    72  					httpmock.StringResponse(`{"data":{"repository":{
    73  						"hasIssuesEnabled": true,
    74  						"issue":{"number":13}
    75  					}}}`))
    76  			},
    77  			wantIssue: 13,
    78  			wantRepo:  "https://example.org/OWNER/REPO",
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			reg := &httpmock.Registry{}
    84  			if tt.httpStub != nil {
    85  				tt.httpStub(reg)
    86  			}
    87  			httpClient := &http.Client{Transport: reg}
    88  			issue, repo, err := IssueFromArg(api.NewClientFromHTTP(httpClient), tt.args.baseRepoFn, tt.args.selector)
    89  			if (err != nil) != tt.wantErr {
    90  				t.Errorf("IssueFromArg() error = %v, wantErr %v", err, tt.wantErr)
    91  				return
    92  			}
    93  			if issue.Number != tt.wantIssue {
    94  				t.Errorf("want issue #%d, got #%d", tt.wantIssue, issue.Number)
    95  			}
    96  			repoURL := ghrepo.GenerateRepoURL(repo, "")
    97  			if repoURL != tt.wantRepo {
    98  				t.Errorf("want repo %s, got %s", tt.wantRepo, repoURL)
    99  			}
   100  		})
   101  	}
   102  }