github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/shared/survey_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ungtb10d/cli/v2/api"
     7  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     8  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
     9  	"github.com/ungtb10d/cli/v2/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  	ios, _, 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  	//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
    47  	as, restoreAsk := prompt.InitAskStubber()
    48  	defer restoreAsk()
    49  
    50  	//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
    51  	as.Stub([]*prompt.QuestionStub{
    52  		{
    53  			Name:  "metadata",
    54  			Value: []string{"Labels", "Projects", "Assignees", "Reviewers", "Milestone"},
    55  		},
    56  	})
    57  	//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
    58  	as.Stub([]*prompt.QuestionStub{
    59  		{
    60  			Name:  "reviewers",
    61  			Value: []string{"monalisa"},
    62  		},
    63  		{
    64  			Name:  "assignees",
    65  			Value: []string{"hubot"},
    66  		},
    67  		{
    68  			Name:  "labels",
    69  			Value: []string{"good first issue"},
    70  		},
    71  		{
    72  			Name:  "projects",
    73  			Value: []string{"The road to 1.0"},
    74  		},
    75  		{
    76  			Name:  "milestone",
    77  			Value: "(none)",
    78  		},
    79  	})
    80  
    81  	state := &IssueMetadataState{
    82  		Assignees: []string{"hubot"},
    83  	}
    84  	err := MetadataSurvey(ios, repo, fetcher, state)
    85  	assert.NoError(t, err)
    86  
    87  	assert.Equal(t, "", stdout.String())
    88  	assert.Equal(t, "", stderr.String())
    89  
    90  	assert.Equal(t, []string{"hubot"}, state.Assignees)
    91  	assert.Equal(t, []string{"monalisa"}, state.Reviewers)
    92  	assert.Equal(t, []string{"good first issue"}, state.Labels)
    93  	assert.Equal(t, []string{"The road to 1.0"}, state.Projects)
    94  	assert.Equal(t, []string{}, state.Milestones)
    95  }
    96  
    97  func TestMetadataSurvey_keepExisting(t *testing.T) {
    98  	ios, _, stdout, stderr := iostreams.Test()
    99  
   100  	repo := ghrepo.New("OWNER", "REPO")
   101  
   102  	fetcher := &metadataFetcher{
   103  		metadataResult: &api.RepoMetadataResult{
   104  			Labels: []api.RepoLabel{
   105  				{Name: "help wanted"},
   106  				{Name: "good first issue"},
   107  			},
   108  			Projects: []api.RepoProject{
   109  				{Name: "Huge Refactoring"},
   110  				{Name: "The road to 1.0"},
   111  			},
   112  		},
   113  	}
   114  
   115  	//nolint:staticcheck // SA1019: prompt.InitAskStubber is deprecated: use NewAskStubber
   116  	as, restoreAsk := prompt.InitAskStubber()
   117  	defer restoreAsk()
   118  
   119  	//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
   120  	as.Stub([]*prompt.QuestionStub{
   121  		{
   122  			Name:  "metadata",
   123  			Value: []string{"Labels", "Projects"},
   124  		},
   125  	})
   126  	//nolint:staticcheck // SA1019: as.Stub is deprecated: use StubPrompt
   127  	as.Stub([]*prompt.QuestionStub{
   128  		{
   129  			Name:  "labels",
   130  			Value: []string{"good first issue"},
   131  		},
   132  		{
   133  			Name:  "projects",
   134  			Value: []string{"The road to 1.0"},
   135  		},
   136  	})
   137  
   138  	state := &IssueMetadataState{
   139  		Assignees: []string{"hubot"},
   140  	}
   141  	err := MetadataSurvey(ios, repo, fetcher, state)
   142  	assert.NoError(t, err)
   143  
   144  	assert.Equal(t, "", stdout.String())
   145  	assert.Equal(t, "", stderr.String())
   146  
   147  	assert.Equal(t, []string{"hubot"}, state.Assignees)
   148  	assert.Equal(t, []string{"good first issue"}, state.Labels)
   149  	assert.Equal(t, []string{"The road to 1.0"}, state.Projects)
   150  }