1+ import test from 'ava' ;
2+ import makeStatusRequest , { CoreActionsForTesting , CommitState , ERR_INVALID_OWNER , ERR_INVALID_STATE } from '../src/makeStatusRequest'
3+ import inputNames from '../src/inputNames'
4+
5+ const INPUT_CONTEXT = "test context" ;
6+ const INPUT_OWNER = "TestOwner" ;
7+ const INPUT_OWNER_INVALID = "-TestOwner" ;
8+ const INPUT_REPOSITORY = "Test.Repository-1" ;
9+ const INPUT_REPOSITORY_WITHOWNER = INPUT_OWNER + "/Test.Repository-1" ;
10+ const INPUT_STATE : CommitState = "success" ;
11+ const INPUT_STATE_INVALID : CommitState = "failed" as CommitState ;
12+ const INPUT_DESC = "Test Description" ;
13+ const INPUT_SHA = "TestSHA" ;
14+ const INPUT_TARGETURL = "test/uri" ;
15+
16+ const actionsCore : CoreActionsForTesting = {
17+ getInput : ( arg :string ) => {
18+ switch ( arg ) {
19+ case inputNames . context :
20+ return INPUT_CONTEXT ;
21+ case inputNames . owner :
22+ return INPUT_OWNER ;
23+ case inputNames . repo :
24+ return INPUT_REPOSITORY ;
25+ case inputNames . state :
26+ return INPUT_STATE ;
27+ case inputNames . desc :
28+ return INPUT_DESC ;
29+ case inputNames . sha :
30+ return INPUT_SHA ;
31+ case inputNames . target_url :
32+ return INPUT_TARGETURL ;
33+ default :
34+ return "input not in test mock" ;
35+ }
36+ }
37+ }
38+ const actionsCoreAlt1 : CoreActionsForTesting = {
39+ getInput : ( arg :string ) => {
40+ switch ( arg ) {
41+ case inputNames . repo :
42+ return INPUT_REPOSITORY_WITHOWNER ;
43+ default :
44+ return actionsCore . getInput ( arg ) ;
45+ }
46+ }
47+ }
48+
49+ const actionsCoreAlt2 : CoreActionsForTesting = {
50+ getInput : ( arg :string ) => {
51+ switch ( arg ) {
52+ case inputNames . owner :
53+ return INPUT_OWNER_INVALID ;
54+ default :
55+ return actionsCore . getInput ( arg ) ;
56+ }
57+ }
58+ }
59+
60+ const actionsCoreAlt3 : CoreActionsForTesting = {
61+ getInput : ( arg :string ) => {
62+ switch ( arg ) {
63+ case inputNames . state :
64+ return INPUT_STATE_INVALID ;
65+ default :
66+ return actionsCore . getInput ( arg ) ;
67+ }
68+ }
69+ }
70+
71+
72+ test ( "should getInput context" , t => {
73+ t . is ( makeStatusRequest ( actionsCore ) . context , INPUT_CONTEXT ) ;
74+ } ) ;
75+ test ( "should getInput owner" , t => {
76+ t . is ( makeStatusRequest ( actionsCore ) . owner , INPUT_OWNER ) ;
77+ } ) ;
78+ test ( "should getInput repo" , t => {
79+ t . is ( makeStatusRequest ( actionsCore ) . repo , INPUT_REPOSITORY ) ;
80+ } ) ;
81+ test ( "should getInput state" , t => {
82+ t . is ( makeStatusRequest ( actionsCore ) . state , INPUT_STATE ) ;
83+ } ) ;
84+ test ( "should getInput description" , t => {
85+ t . is ( makeStatusRequest ( actionsCore ) . description , INPUT_DESC ) ;
86+ } ) ;
87+ test ( "should getInput sha" , t => {
88+ t . is ( makeStatusRequest ( actionsCore ) . sha , INPUT_SHA ) ;
89+ } ) ;
90+ test ( "should getInput target_url" , t => {
91+ t . is ( makeStatusRequest ( actionsCore ) . target_url , INPUT_TARGETURL ) ;
92+ } ) ;
93+
94+ test ( "should getInput repo and remove leading owner name" , t => {
95+ t . is ( makeStatusRequest ( actionsCoreAlt1 ) . repo , INPUT_REPOSITORY ) ;
96+ } ) ;
97+
98+ test ( "when owner is not a valid GitHub username, should throw" , t => {
99+ let err = t . throws ( ( ) => makeStatusRequest ( actionsCoreAlt2 ) ) ;
100+ t . is ( err . message , ERR_INVALID_OWNER )
101+ } ) ;
102+
103+ test ( "should validate state" , t => {
104+ let err = t . throws ( ( ) => makeStatusRequest ( actionsCoreAlt3 ) ) ;
105+ t . is ( err . message , ERR_INVALID_STATE )
106+ } ) ;
0 commit comments