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

     1  package shared
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/cli/cli/internal/ghrepo"
    11  	"github.com/cli/cli/pkg/httpmock"
    12  	"github.com/cli/cli/pkg/prompt"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestTemplateManager_hasAPI(t *testing.T) {
    17  	rootDir := t.TempDir()
    18  	legacyTemplateFile := filepath.Join(rootDir, ".github", "ISSUE_TEMPLATE.md")
    19  	_ = os.MkdirAll(filepath.Dir(legacyTemplateFile), 0755)
    20  	_ = ioutil.WriteFile(legacyTemplateFile, []byte("LEGACY"), 0644)
    21  
    22  	tr := httpmock.Registry{}
    23  	httpClient := &http.Client{Transport: &tr}
    24  	defer tr.Verify(t)
    25  
    26  	tr.Register(
    27  		httpmock.GraphQL(`query IssueTemplates_fields\b`),
    28  		httpmock.StringResponse(`{"data":{
    29  			"Repository": {
    30  				"fields": [
    31  					{"name": "foo"},
    32  					{"name": "issueTemplates"}
    33  				]
    34  			},
    35  			"CreateIssueInput": {
    36  				"inputFields": [
    37  					{"name": "foo"},
    38  					{"name": "issueTemplate"}
    39  				]
    40  			}
    41  		}}`))
    42  	tr.Register(
    43  		httpmock.GraphQL(`query IssueTemplates\b`),
    44  		httpmock.StringResponse(`{"data":{"repository":{
    45  			"issueTemplates": [
    46  				{"name": "Bug report", "body": "I found a problem"},
    47  				{"name": "Feature request", "body": "I need a feature"}
    48  			]
    49  		}}}`))
    50  
    51  	m := templateManager{
    52  		repo:         ghrepo.NewWithHost("OWNER", "REPO", "example.com"),
    53  		rootDir:      rootDir,
    54  		allowFS:      true,
    55  		isPR:         false,
    56  		httpClient:   httpClient,
    57  		cachedClient: httpClient,
    58  	}
    59  
    60  	hasTemplates, err := m.HasTemplates()
    61  	assert.NoError(t, err)
    62  	assert.True(t, hasTemplates)
    63  
    64  	assert.Equal(t, "LEGACY", string(m.LegacyBody()))
    65  
    66  	as, askRestore := prompt.InitAskStubber()
    67  	defer askRestore()
    68  
    69  	as.StubOne(1) // choose "Feature Request"
    70  	tpl, err := m.Choose()
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, "Feature request", tpl.NameForSubmit())
    73  	assert.Equal(t, "I need a feature", string(tpl.Body()))
    74  }