1
+ import * as apputils from '@jupyterlab/apputils' ;
1
2
import {
2
3
BranchHeader ,
3
4
IBranchHeaderProps
@@ -22,7 +23,7 @@ describe('BranchHeader', () => {
22
23
upstreamBranch : 'origin/master' ,
23
24
stagedFiles : [ 'test-1' , 'test-2' ] ,
24
25
data : [ 'master' , 'feature-1' , 'feature-2' , 'patch-007' ] ,
25
- refresh : 'update all content' ,
26
+ refresh : function ( ) { } ,
26
27
disabled : false ,
27
28
toggleSidebar : function ( ) {
28
29
return true ;
@@ -32,9 +33,11 @@ describe('BranchHeader', () => {
32
33
33
34
describe ( '#constructor()' , ( ) => {
34
35
const branchHeader = new BranchHeader ( props ) ;
36
+
35
37
it ( 'should construct a new branch header' , ( ) => {
36
38
expect ( branchHeader ) . toBeInstanceOf ( BranchHeader ) ;
37
39
} ) ;
40
+
38
41
it ( 'should set default values correctly' , ( ) => {
39
42
expect ( branchHeader . state . dropdownOpen ) . toEqual ( false ) ;
40
43
expect ( branchHeader . state . showCommitBox ) . toEqual ( true ) ;
@@ -43,26 +46,181 @@ describe('BranchHeader', () => {
43
46
} ) ;
44
47
45
48
describe ( '#commitAllStagedFiles()' , ( ) => {
46
- const branchHeader = new BranchHeader ( props ) ;
47
- it ( 'should commit when commit message is provided' , ( ) => {
49
+ let branchHeader = null ;
50
+
51
+ beforeEach ( ( ) => {
52
+ branchHeader = new BranchHeader ( props ) ;
53
+ } ) ;
54
+
55
+ it ( 'should commit when commit message is provided' , async ( ) => {
48
56
const spy = jest . spyOn ( Git . prototype , 'commit' ) ;
49
- branchHeader . commitAllStagedFiles (
57
+ // Mock identity look up
58
+ const identity = jest . spyOn ( Git . prototype , 'config' ) . mockImplementation (
59
+ ( ) =>
60
+ new Response (
61
+ JSON . stringify ( {
62
+ options : {
63
+ 'user.name' : 'John Snow' ,
64
+
65
+ }
66
+ } ) ,
67
+ { status : 201 }
68
+ )
69
+ ) ;
70
+ await branchHeader . commitAllStagedFiles (
50
71
'Initial commit' ,
51
72
'/absolute/path/to/git/repo'
52
73
) ;
74
+ expect ( identity ) . toHaveBeenCalledTimes ( 1 ) ;
53
75
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
54
76
expect ( spy ) . toHaveBeenCalledWith (
55
77
'Initial commit' ,
56
78
'/absolute/path/to/git/repo'
57
79
) ;
58
- spy . mockRestore ( ) ;
80
+ jest . restoreAllMocks ( ) ;
59
81
} ) ;
60
- it ( 'should NOT commit when commit message is empty' , ( ) => {
82
+
83
+ it ( 'should NOT commit when commit message is empty' , async ( ) => {
61
84
const spy = jest . spyOn ( Git . prototype , 'commit' ) ;
62
- branchHeader . commitAllStagedFiles ( '' , props . topRepoPath ) ;
85
+ await branchHeader . commitAllStagedFiles ( '' , props . topRepoPath ) ;
63
86
expect ( spy ) . not . toHaveBeenCalled ( ) ;
64
87
spy . mockRestore ( ) ;
65
88
} ) ;
89
+
90
+ it ( 'should prompt for user identity if user.name is unset' , async ( ) => {
91
+ const spy = jest . spyOn ( Git . prototype , 'commit' ) ;
92
+ // Mock identity look up
93
+ const identity = jest
94
+ . spyOn ( Git . prototype , 'config' )
95
+ . mockImplementation ( ( path , options ) => {
96
+ if ( options === undefined ) {
97
+ return new Response (
98
+ JSON . stringify ( {
99
+ options : {
100
+
101
+ }
102
+ } ) ,
103
+ { status : 201 }
104
+ ) ;
105
+ } else {
106
+ return new Response ( '' , { status : 201 } ) ;
107
+ }
108
+ } ) ;
109
+ jest . spyOn ( apputils , 'showDialog' ) . mockReturnValue (
110
+ Promise . resolve ( {
111
+ button : {
112
+ accept : true
113
+ } ,
114
+ value : {
115
+ name : 'John Snow' ,
116
+
117
+ }
118
+ } )
119
+ ) ;
120
+
121
+ await branchHeader . commitAllStagedFiles (
122
+ 'Initial commit' ,
123
+ '/absolute/path/to/git/repo'
124
+ ) ;
125
+ expect ( identity ) . toHaveBeenCalledTimes ( 2 ) ;
126
+ expect ( identity . mock . calls [ 0 ] ) . toEqual ( [ '/absolute/path/to/git/repo' ] ) ;
127
+ expect ( identity . mock . calls [ 1 ] ) . toEqual ( [
128
+ '/absolute/path/to/git/repo' ,
129
+ {
130
+ 'user.name' : 'John Snow' ,
131
+
132
+ }
133
+ ] ) ;
134
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
135
+ expect ( spy ) . toHaveBeenCalledWith (
136
+ 'Initial commit' ,
137
+ '/absolute/path/to/git/repo'
138
+ ) ;
139
+ jest . restoreAllMocks ( ) ;
140
+ } ) ;
141
+
142
+ it ( 'should prompt for user identity if user.email is unset' , async ( ) => {
143
+ const spy = jest . spyOn ( Git . prototype , 'commit' ) ;
144
+ // Mock identity look up
145
+ const identity = jest
146
+ . spyOn ( Git . prototype , 'config' )
147
+ . mockImplementation ( ( path , options ) => {
148
+ if ( options === undefined ) {
149
+ return new Response (
150
+ JSON . stringify ( {
151
+ options : {
152
+ 'user.name' : 'John Snow'
153
+ }
154
+ } ) ,
155
+ { status : 201 }
156
+ ) ;
157
+ } else {
158
+ return new Response ( '' , { status : 201 } ) ;
159
+ }
160
+ } ) ;
161
+ jest . spyOn ( apputils , 'showDialog' ) . mockReturnValue ( {
162
+ button : {
163
+ accept : true
164
+ } ,
165
+ value : {
166
+ name : 'John Snow' ,
167
+
168
+ }
169
+ } ) ;
170
+
171
+ await branchHeader . commitAllStagedFiles (
172
+ 'Initial commit' ,
173
+ '/absolute/path/to/git/repo'
174
+ ) ;
175
+ expect ( identity ) . toHaveBeenCalledTimes ( 2 ) ;
176
+ expect ( identity . mock . calls [ 0 ] ) . toEqual ( [ '/absolute/path/to/git/repo' ] ) ;
177
+ expect ( identity . mock . calls [ 1 ] ) . toEqual ( [
178
+ '/absolute/path/to/git/repo' ,
179
+ {
180
+ 'user.name' : 'John Snow' ,
181
+
182
+ }
183
+ ] ) ;
184
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
185
+ expect ( spy ) . toHaveBeenCalledWith (
186
+ 'Initial commit' ,
187
+ '/absolute/path/to/git/repo'
188
+ ) ;
189
+ jest . restoreAllMocks ( ) ;
190
+ } ) ;
191
+
192
+ it ( 'should NOT commit if no user identity is set and the user reject the dialog' , async ( ) => {
193
+ const spy = jest . spyOn ( Git . prototype , 'commit' ) ;
194
+ // Mock identity look up
195
+ const identity = jest
196
+ . spyOn ( Git . prototype , 'config' )
197
+ . mockImplementation ( ( path , options ) => {
198
+ if ( options === undefined ) {
199
+ return new Response (
200
+ JSON . stringify ( {
201
+ options : { }
202
+ } ) ,
203
+ { status : 201 }
204
+ ) ;
205
+ } else {
206
+ return new Response ( '' , { status : 201 } ) ;
207
+ }
208
+ } ) ;
209
+ jest . spyOn ( apputils , 'showDialog' ) . mockReturnValue ( {
210
+ button : {
211
+ accept : false
212
+ }
213
+ } ) ;
214
+
215
+ await branchHeader . commitAllStagedFiles (
216
+ 'Initial commit' ,
217
+ '/absolute/path/to/git/repo'
218
+ ) ;
219
+ expect ( identity ) . toHaveBeenCalledTimes ( 1 ) ;
220
+ expect ( identity ) . toHaveBeenCalledWith ( '/absolute/path/to/git/repo' ) ;
221
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
222
+ jest . restoreAllMocks ( ) ;
223
+ } ) ;
66
224
} ) ;
67
225
68
226
describe ( '#updateCommitBoxState()' , ( ) => {
0 commit comments