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

     1  package unpin
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/ungtb10d/cli/v2/internal/config"
     9  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    10  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    11  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    12  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    13  	"github.com/google/shlex"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestNewCmdPin(t *testing.T) {
    18  	tests := []struct {
    19  		name    string
    20  		input   string
    21  		output  UnpinOptions
    22  		wantErr bool
    23  		errMsg  string
    24  	}{
    25  		{
    26  			name:    "no argument",
    27  			input:   "",
    28  			wantErr: true,
    29  			errMsg:  "accepts 1 arg(s), received 0",
    30  		},
    31  		{
    32  			name:  "issue number",
    33  			input: "6",
    34  			output: UnpinOptions{
    35  				SelectorArg: "6",
    36  			},
    37  		},
    38  		{
    39  			name:  "issue url",
    40  			input: "https://github.com/ungtb10d/cli/6",
    41  			output: UnpinOptions{
    42  				SelectorArg: "https://github.com/ungtb10d/cli/6",
    43  			},
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			ios, _, _, _ := iostreams.Test()
    49  			ios.SetStdinTTY(true)
    50  			ios.SetStdoutTTY(true)
    51  			f := &cmdutil.Factory{
    52  				IOStreams: ios,
    53  			}
    54  			argv, err := shlex.Split(tt.input)
    55  			assert.NoError(t, err)
    56  			var gotOpts *UnpinOptions
    57  			cmd := NewCmdUnpin(f, func(opts *UnpinOptions) error {
    58  				gotOpts = opts
    59  				return nil
    60  			})
    61  			cmd.SetArgs(argv)
    62  			cmd.SetIn(&bytes.Buffer{})
    63  			cmd.SetOut(&bytes.Buffer{})
    64  			cmd.SetErr(&bytes.Buffer{})
    65  
    66  			_, err = cmd.ExecuteC()
    67  			if tt.wantErr {
    68  				assert.Error(t, err)
    69  				assert.Equal(t, tt.errMsg, err.Error())
    70  				return
    71  			}
    72  
    73  			assert.NoError(t, err)
    74  			assert.Equal(t, tt.output.SelectorArg, gotOpts.SelectorArg)
    75  		})
    76  	}
    77  }
    78  
    79  func TestUnpinRun(t *testing.T) {
    80  	tests := []struct {
    81  		name       string
    82  		tty        bool
    83  		opts       *UnpinOptions
    84  		httpStubs  func(*httpmock.Registry)
    85  		wantStdout string
    86  		wantStderr string
    87  	}{
    88  		{
    89  			name: "unpin issue",
    90  			tty:  true,
    91  			opts: &UnpinOptions{SelectorArg: "20"},
    92  			httpStubs: func(reg *httpmock.Registry) {
    93  				reg.Register(
    94  					httpmock.GraphQL(`query IssueByNumber\b`),
    95  					httpmock.StringResponse(`
    96              { "data": { "repository": {
    97                "issue": { "id": "ISSUE-ID", "number": 20, "title": "Issue Title", "isPinned": true}
    98              } } }`),
    99  				)
   100  				reg.Register(
   101  					httpmock.GraphQL(`mutation IssueUnpin\b`),
   102  					httpmock.GraphQLMutation(`{"id": "ISSUE-ID"}`,
   103  						func(inputs map[string]interface{}) {
   104  							assert.Equal(t, inputs["issueId"], "ISSUE-ID")
   105  						},
   106  					),
   107  				)
   108  			},
   109  			wantStdout: "",
   110  			wantStderr: "✓ Unpinned issue #20 (Issue Title) from OWNER/REPO\n",
   111  		},
   112  		{
   113  			name: "issue not pinned",
   114  			tty:  true,
   115  			opts: &UnpinOptions{SelectorArg: "20"},
   116  			httpStubs: func(reg *httpmock.Registry) {
   117  				reg.Register(
   118  					httpmock.GraphQL(`query IssueByNumber\b`),
   119  					httpmock.StringResponse(`
   120              { "data": { "repository": {
   121                "issue": { "id": "ISSUE-ID", "number": 20, "title": "Issue Title", "isPinned": false}
   122              } } }`),
   123  				)
   124  			},
   125  			wantStderr: "! Issue #20 (Issue Title) is not pinned to OWNER/REPO\n",
   126  		},
   127  	}
   128  	for _, tt := range tests {
   129  		reg := &httpmock.Registry{}
   130  		if tt.httpStubs != nil {
   131  			tt.httpStubs(reg)
   132  		}
   133  		tt.opts.HttpClient = func() (*http.Client, error) {
   134  			return &http.Client{Transport: reg}, nil
   135  		}
   136  
   137  		ios, _, stdout, stderr := iostreams.Test()
   138  		ios.SetStdinTTY(tt.tty)
   139  		ios.SetStdoutTTY(tt.tty)
   140  		tt.opts.IO = ios
   141  
   142  		tt.opts.Config = func() (config.Config, error) {
   143  			return config.NewBlankConfig(), nil
   144  		}
   145  
   146  		tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
   147  			return ghrepo.New("OWNER", "REPO"), nil
   148  		}
   149  
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			defer reg.Verify(t)
   152  			err := unpinRun(tt.opts)
   153  			assert.NoError(t, err)
   154  			assert.Equal(t, tt.wantStdout, stdout.String())
   155  			assert.Equal(t, tt.wantStderr, stderr.String())
   156  		})
   157  	}
   158  }