@@ -12,6 +12,7 @@ import (
1212
1313 "github.com/cli/cli/internal/run"
1414 "github.com/cli/cli/test"
15+ "github.com/google/go-cmp/cmp"
1516)
1617
1718func TestIssueStatus (t * testing.T ) {
@@ -284,81 +285,77 @@ func TestIssueView_web_numberArgWithHash(t *testing.T) {
284285 eq (t , url , "https://github.com/OWNER/REPO/issues/123" )
285286}
286287
287- func TestIssueView (t * testing.T ) {
288- initBlankContext ("OWNER/REPO" , "master" )
289- http := initFakeHTTP ()
290- http .StubRepoResponse ("OWNER" , "REPO" )
291-
292- http .StubResponse (200 , bytes .NewBufferString (`
293- { "data": { "repository": { "hasIssuesEnabled": true, "issue": {
294- "number": 123,
295- "body": "**bold story**",
296- "title": "ix of coins",
297- "author": {
298- "login": "marseilles"
288+ func TestIssueView_Preview (t * testing.T ) {
289+ tests := map [string ]struct {
290+ ownerRepo string
291+ command string
292+ fixture string
293+ expectedOutputs []string
294+ }{
295+ "Open issue" : {
296+ ownerRepo : "master" ,
297+ command : "issue view 123" ,
298+ fixture : "../test/fixtures/issueView_preview.json" ,
299+ expectedOutputs : []string {
300+ "ix of coins" ,
301+ "Open • marseilles opened about 292 years ago • 9 comments" ,
302+ "bold story" ,
303+ "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" ,
304+ },
299305 },
300- "labels": {
301- "nodes": [
302- {"name": "tarot"}
303- ]
306+ "Open issue with no label" : {
307+ ownerRepo : "master" ,
308+ command : "issue view 123" ,
309+ fixture : "../test/fixtures/issueView_previewNoLabel.json" ,
310+ expectedOutputs : []string {
311+ "ix of coins" ,
312+ "Open • marseilles opened about 292 years ago • 9 comments" ,
313+ "bold story" ,
314+ "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" ,
315+ },
304316 },
305- "comments": {
306- "totalCount": 9
317+ "Open issue with empty body" : {
318+ ownerRepo : "master" ,
319+ command : "issue view 123" ,
320+ fixture : "../test/fixtures/issueView_previewWithEmptyBody.json" ,
321+ expectedOutputs : []string {
322+ "ix of coins" ,
323+ "Open • marseilles opened about 292 years ago • 9 comments" ,
324+ "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" ,
325+ },
326+ },
327+ "Closed issue" : {
328+ ownerRepo : "master" ,
329+ command : "issue view 123" ,
330+ fixture : "../test/fixtures/issueView_previewClosedState.json" ,
331+ expectedOutputs : []string {
332+ "ix of coins" ,
333+ "Closed • marseilles opened about 292 years ago • 9 comments" ,
334+ "bold story" ,
335+ "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" ,
336+ },
307337 },
308- "url": "https://github.com/OWNER/REPO/issues/123"
309- } } } }
310- ` ))
311-
312- output , err := RunCommand (issueViewCmd , "issue view 123" )
313- if err != nil {
314- t .Errorf ("error running command `issue view`: %v" , err )
315338 }
339+ for name , tc := range tests {
340+ t .Run (name , func (t * testing.T ) {
341+ initBlankContext ("OWNER/REPO" , tc .ownerRepo )
342+ http := initFakeHTTP ()
343+ http .StubRepoResponse ("OWNER" , "REPO" )
316344
317- eq (t , output .Stderr (), "" )
318-
319- test .ExpectLines (t , output .String (),
320- "ix of coins" ,
321- `opened by marseilles. 9 comments. \(tarot\)` ,
322- "bold story" ,
323- "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" )
324- }
345+ jsonFile , _ := os .Open (tc .fixture )
346+ defer jsonFile .Close ()
347+ http .StubResponse (200 , jsonFile )
325348
326- func TestIssueView_WithEmptyBody ( t * testing. T ) {
327- initBlankContext ( "OWNER/REPO" , "master" )
328- http := initFakeHTTP ( )
329- http . StubRepoResponse ( "OWNER" , "REPO" )
349+ output , err := RunCommand ( issueViewCmd , tc . command )
350+ if err != nil {
351+ t . Errorf ( "error running command `%v`: %v" , tc . command , err )
352+ }
330353
331- http .StubResponse (200 , bytes .NewBufferString (`
332- { "data": { "repository": { "hasIssuesEnabled": true, "issue": {
333- "number": 123,
334- "body": "",
335- "title": "ix of coins",
336- "author": {
337- "login": "marseilles"
338- },
339- "labels": {
340- "nodes": [
341- {"name": "tarot"}
342- ]
343- },
344- "comments": {
345- "totalCount": 9
346- },
347- "url": "https://github.com/OWNER/REPO/issues/123"
348- } } } }
349- ` ))
354+ eq (t , output .Stderr (), "" )
350355
351- output , err := RunCommand (issueViewCmd , "issue view 123" )
352- if err != nil {
353- t .Errorf ("error running command `issue view`: %v" , err )
356+ test .ExpectLines (t , output .String (), tc .expectedOutputs ... )
357+ })
354358 }
355-
356- eq (t , output .Stderr (), "" )
357-
358- test .ExpectLines (t , output .String (),
359- "ix of coins" ,
360- `opened by marseilles. 9 comments. \(tarot\)` ,
361- "View this issue on GitHub: https://github.com/OWNER/REPO/issues/123" )
362359}
363360
364361func TestIssueView_web_notFound (t * testing.T ) {
@@ -659,3 +656,23 @@ func Test_listHeader(t *testing.T) {
659656 })
660657 }
661658}
659+
660+ func TestIssueStateTitleWithColor (t * testing.T ) {
661+ tests := map [string ]struct {
662+ state string
663+ want string
664+ }{
665+ "Open state" : {state : "OPEN" , want : "Open" },
666+ "Closed state" : {state : "CLOSED" , want : "Closed" },
667+ }
668+
669+ for name , tc := range tests {
670+ t .Run (name , func (t * testing.T ) {
671+ got := issueStateTitleWithColor (tc .state )
672+ diff := cmp .Diff (tc .want , got )
673+ if diff != "" {
674+ t .Fatalf (diff )
675+ }
676+ })
677+ }
678+ }
0 commit comments