github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/pr/shared/survey_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cli/cli/api"
     7  	"github.com/cli/cli/internal/ghrepo"
     8  	"github.com/cli/cli/pkg/iostreams"
     9  	"github.com/cli/cli/pkg/prompt"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type metadataFetcher struct {
    14  	metadataResult *api.RepoMetadataResult
    15  }
    16  
    17  func (mf *metadataFetcher) RepoMetadataFetch(input api.RepoMetadataInput) (*api.RepoMetadataResult, error) {
    18  	return mf.metadataResult, nil
    19  }
    20  
    21  func TestMetadataSurvey_selectAll(t *testing.T) {
    22  	io, _, stdout, stderr := iostreams.Test()
    23  
    24  	repo := ghrepo.New("OWNER", "REPO")
    25  
    26  	fetcher := &metadataFetcher{
    27  		metadataResult: &api.RepoMetadataResult{
    28  			AssignableUsers: []api.RepoAssignee{
    29  				{Login: "hubot"},
    30  				{Login: "monalisa"},
    31  			},
    32  			Labels: []api.RepoLabel{
    33  				{Name: "help wanted"},
    34  				{Name: "good first issue"},
    35  			},
    36  			Projects: []api.RepoProject{
    37  				{Name: "Huge Refactoring"},
    38  				{Name: "The road to 1.0"},
    39  			},
    40  			Milestones: []api.RepoMilestone{
    41  				{Title: "1.2 patch release"},
    42  			},
    43  		},
    44  	}
    45  
    46  	as, restoreAsk := prompt.InitAskStubber()
    47  	defer restoreAsk()
    48  
    49  	as.Stub([]*prompt.QuestionStub{
    50  		{
    51  			Name:  "metadata",
    52  			Value: []string{"Labels", "Projects", "Assignees", "Reviewers", "Milestone"},
    53  		},
    54  	})
    55  	as.Stub([]*prompt.QuestionStub{
    56  		{
    57  			Name:  "reviewers",
    58  			Value: []string{"monalisa"},
    59  		},
    60  		{
    61  			Name:  "assignees",
    62  			Value: []string{"hubot"},
    63  		},
    64  		{
    65  			Name:  "labels",
    66  			Value: []string{"good first issue"},
    67  		},
    68  		{
    69  			Name:  "projects",
    70  			Value: []string{"The road to 1.0"},
    71  		},
    72  		{
    73  			Name:  "milestone",
    74  			Value: []string{"(none)"},
    75  		},
    76  	})
    77  
    78  	state := &IssueMetadataState{
    79  		Assignees: []string{"hubot"},
    80  	}
    81  	err := MetadataSurvey(io, repo, fetcher, state)
    82  	assert.NoError(t, err)
    83  
    84  	assert.Equal(t, "", stdout.String())
    85  	assert.Equal(t, "", stderr.String())
    86  
    87  	assert.Equal(t, []string{"hubot"}, state.Assignees)
    88  	assert.Equal(t, []string{"monalisa"}, state.Reviewers)
    89  	assert.Equal(t, []string{"good first issue"}, state.Labels)
    90  	assert.Equal(t, []string{"The road to 1.0"}, state.Projects)
    91  	assert.Equal(t, []string{}, state.Milestones)
    92  }
    93  
    94  func TestMetadataSurvey_keepExisting(t *testing.T) {
    95  	io, _, stdout, stderr := iostreams.Test()
    96  
    97  	repo := ghrepo.New("OWNER", "REPO")
    98  
    99  	fetcher := &metadataFetcher{
   100  		metadataResult: &api.RepoMetadataResult{
   101  			Labels: []api.RepoLabel{
   102  				{Name: "help wanted"},
   103  				{Name: "good first issue"},
   104  			},
   105  			Projects: []api.RepoProject{
   106  				{Name: "Huge Refactoring"},
   107  				{Name: "The road to 1.0"},
   108  			},
   109  		},
   110  	}
   111  
   112  	as, restoreAsk := prompt.InitAskStubber()
   113  	defer restoreAsk()
   114  
   115  	as.Stub([]*prompt.QuestionStub{
   116  		{
   117  			Name:  "metadata",
   118  			Value: []string{"Labels", "Projects"},
   119  		},
   120  	})
   121  	as.Stub([]*prompt.QuestionStub{
   122  		{
   123  			Name:  "labels",
   124  			Value: []string{"good first issue"},
   125  		},
   126  		{
   127  			Name:  "projects",
   128  			Value: []string{"The road to 1.0"},
   129  		},
   130  	})
   131  
   132  	state := &IssueMetadataState{
   133  		Assignees: []string{"hubot"},
   134  	}
   135  	err := MetadataSurvey(io, repo, fetcher, state)
   136  	assert.NoError(t, err)
   137  
   138  	assert.Equal(t, "", stdout.String())
   139  	assert.Equal(t, "", stderr.String())
   140  
   141  	assert.Equal(t, []string{"hubot"}, state.Assignees)
   142  	assert.Equal(t, []string{"good first issue"}, state.Labels)
   143  	assert.Equal(t, []string{"The road to 1.0"}, state.Projects)
   144  }