1
1
import * as apputils from '@jupyterlab/apputils' ;
2
2
import 'jest' ;
3
- import { FileList , IFileListProps } from '../../src/components/FileList' ;
4
- import { GitExtension } from '../../src/model' ;
3
+ import { GitExtension as GitModel } from '../../src/model' ;
5
4
import * as git from '../../src/git' ;
5
+ import { GitPanel , IGitSessionNodeProps } from '../../src/components/GitPanel' ;
6
6
7
7
jest . mock ( '../../src/git' ) ;
8
-
9
- // Mock jupyterlab package
10
8
jest . mock ( '@jupyterlab/apputils' ) ;
11
9
12
- describe ( 'FileList' , ( ) => {
13
- const props : IFileListProps = {
14
- model : null ,
15
- renderMime : null ,
16
- stagedFiles : [ ] ,
17
- unstagedFiles : [ ] ,
18
- untrackedFiles : [ ] ,
19
- settings : null
10
+ function MockRequest ( url : string , method : string , request : any ) {
11
+ let response : Response ;
12
+ let obj : any ;
13
+ switch ( url ) {
14
+ case '/git/commit' :
15
+ response = new Response ( ) ;
16
+ break ;
17
+ case '/git/show_top_level' :
18
+ obj = {
19
+ code : 0 ,
20
+ top_repo_path : ( request as any ) [ 'current_path' ]
21
+ } ;
22
+ response = new Response ( JSON . stringify ( obj ) ) ;
23
+ break ;
24
+ case '/git/server_root' :
25
+ obj = {
26
+ server_root : '/foo'
27
+ } ;
28
+ response = new Response ( JSON . stringify ( obj ) ) ;
29
+ break ;
30
+ default :
31
+ obj = {
32
+ message : `No mock implementation for ${ url } .`
33
+ } ;
34
+ response = new Response ( JSON . stringify ( obj ) , { status : 404 } ) ;
35
+ }
36
+ return Promise . resolve ( response ) ;
37
+ }
38
+
39
+ /**
40
+ * Returns a bare minimum "settings" object for use within the Git panel.
41
+ *
42
+ * @private
43
+ * @returns mock settings
44
+ */
45
+ function MockSettings ( ) {
46
+ return {
47
+ changed : {
48
+ connect : ( ) => true ,
49
+ disconnect : ( ) => true
50
+ } ,
51
+ composite : { }
20
52
} ;
53
+ }
21
54
22
- describe ( '#commitAllStagedFiles()' , ( ) => {
23
- let fileList : FileList = null ;
55
+ describe ( 'GitPanel' , ( ) => {
56
+ describe ( '#commitStagedFiles()' , ( ) => {
57
+ const props : IGitSessionNodeProps = {
58
+ model : null ,
59
+ renderMime : null ,
60
+ settings : null
61
+ } ;
24
62
25
63
beforeEach ( async ( ) => {
26
64
jest . restoreAllMocks ( ) ;
27
65
28
- const fakePath = '/path/to/repo' ;
29
- const fakeRoot = '/foo' ;
30
- const mockGit = git as jest . Mocked < typeof git > ;
31
- mockGit . httpGitRequest . mockImplementation ( ( url , method , request ) => {
32
- let response : Response ;
33
- switch ( url ) {
34
- case '/git/commit' :
35
- response = new Response ( ) ;
36
- break ;
37
- case '/git/show_top_level' :
38
- response = new Response (
39
- JSON . stringify ( {
40
- code : 0 ,
41
- top_repo_path : ( request as any ) [ 'current_path' ]
42
- } )
43
- ) ;
44
- break ;
45
- case '/git/server_root' :
46
- response = new Response (
47
- JSON . stringify ( {
48
- server_root : fakeRoot
49
- } )
50
- ) ;
51
- break ;
52
- default :
53
- response = new Response (
54
- `{"message": "No mock implementation for ${ url } ."}` ,
55
- { status : 404 }
56
- ) ;
57
- }
58
- return Promise . resolve ( response ) ;
59
- } ) ;
66
+ const mock = git as jest . Mocked < typeof git > ;
67
+ mock . httpGitRequest . mockImplementation ( MockRequest ) ;
68
+
60
69
const app = {
61
70
commands : {
62
71
hasCommand : jest . fn ( ) . mockReturnValue ( true )
63
72
}
64
73
} ;
65
- props . model = new GitExtension ( app as any ) ;
66
- props . model . pathRepository = fakePath ;
74
+ props . model = new GitModel ( app as any ) ;
75
+ props . model . pathRepository = '/path/to/repo' ;
76
+
77
+ // @ts -ignore
78
+ props . settings = MockSettings ( ) ;
79
+
67
80
await props . model . ready ;
68
- fileList = new FileList ( props ) ;
69
81
} ) ;
70
82
71
83
it ( 'should commit when commit message is provided' , async ( ) => {
72
- const spy = jest . spyOn ( GitExtension . prototype , 'commit' ) ;
84
+ const spy = jest . spyOn ( GitModel . prototype , 'commit' ) ;
85
+
73
86
// Mock identity look up
74
87
const identity = jest
75
- . spyOn ( GitExtension . prototype , 'config' )
88
+ . spyOn ( GitModel . prototype , 'config' )
76
89
. mockResolvedValue (
77
90
new Response (
78
91
JSON . stringify ( {
@@ -84,24 +97,28 @@ describe('FileList', () => {
84
97
{ status : 201 }
85
98
)
86
99
) ;
87
- await fileList . commitAllStagedFiles ( 'Initial commit' ) ;
100
+
101
+ const panel = new GitPanel ( props ) ;
102
+ await panel . commitStagedFiles ( 'Initial commit' ) ;
88
103
expect ( identity ) . toHaveBeenCalledTimes ( 1 ) ;
89
104
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
90
105
expect ( spy ) . toHaveBeenCalledWith ( 'Initial commit' ) ;
91
106
} ) ;
92
107
93
- it ( 'should NOT commit when commit message is empty' , async ( ) => {
94
- const spy = jest . spyOn ( GitExtension . prototype , 'commit' ) ;
95
- await fileList . commitAllStagedFiles ( '' ) ;
108
+ it ( 'should not commit without a commit message' , async ( ) => {
109
+ const spy = jest . spyOn ( GitModel . prototype , 'commit' ) ;
110
+ const panel = new GitPanel ( props ) ;
111
+ await panel . commitStagedFiles ( '' ) ;
96
112
expect ( spy ) . not . toHaveBeenCalled ( ) ;
97
113
spy . mockRestore ( ) ;
98
114
} ) ;
99
115
100
- it ( 'should prompt for user identity if user.name is unset' , async ( ) => {
101
- const spy = jest . spyOn ( GitExtension . prototype , 'commit' ) ;
116
+ it ( 'should prompt for user identity if user.name is not set' , async ( ) => {
117
+ const spy = jest . spyOn ( GitModel . prototype , 'commit' ) ;
118
+
102
119
// Mock identity look up
103
120
const identity = jest
104
- . spyOn ( GitExtension . prototype , 'config' )
121
+ . spyOn ( GitModel . prototype , 'config' )
105
122
. mockImplementation ( options => {
106
123
let response : Response = null ;
107
124
if ( options === undefined ) {
@@ -118,8 +135,8 @@ describe('FileList', () => {
118
135
}
119
136
return Promise . resolve ( response ) ;
120
137
} ) ;
121
- const mockApputils = apputils as jest . Mocked < typeof apputils > ;
122
- mockApputils . showDialog . mockResolvedValue ( {
138
+ const mock = apputils as jest . Mocked < typeof apputils > ;
139
+ mock . showDialog . mockResolvedValue ( {
123
140
button : {
124
141
accept : true ,
125
142
caption : '' ,
@@ -135,7 +152,8 @@ describe('FileList', () => {
135
152
}
136
153
} ) ;
137
154
138
- await fileList . commitAllStagedFiles ( 'Initial commit' ) ;
155
+ const panel = new GitPanel ( props ) ;
156
+ await panel . commitStagedFiles ( 'Initial commit' ) ;
139
157
expect ( identity ) . toHaveBeenCalledTimes ( 2 ) ;
140
158
expect ( identity . mock . calls [ 0 ] ) . toHaveLength ( 0 ) ;
141
159
expect ( identity . mock . calls [ 1 ] ) . toEqual ( [
@@ -148,11 +166,12 @@ describe('FileList', () => {
148
166
expect ( spy ) . toHaveBeenCalledWith ( 'Initial commit' ) ;
149
167
} ) ;
150
168
151
- it ( 'should prompt for user identity if user.email is unset' , async ( ) => {
152
- const spy = jest . spyOn ( GitExtension . prototype , 'commit' ) ;
169
+ it ( 'should prompt for user identity if user.email is not set' , async ( ) => {
170
+ const spy = jest . spyOn ( GitModel . prototype , 'commit' ) ;
171
+
153
172
// Mock identity look up
154
173
const identity = jest
155
- . spyOn ( GitExtension . prototype , 'config' )
174
+ . spyOn ( GitModel . prototype , 'config' )
156
175
. mockImplementation ( options => {
157
176
let response : Response = null ;
158
177
if ( options === undefined ) {
@@ -169,8 +188,8 @@ describe('FileList', () => {
169
188
}
170
189
return Promise . resolve ( response ) ;
171
190
} ) ;
172
- const mockApputils = apputils as jest . Mocked < typeof apputils > ;
173
- mockApputils . showDialog . mockResolvedValue ( {
191
+ const mock = apputils as jest . Mocked < typeof apputils > ;
192
+ mock . showDialog . mockResolvedValue ( {
174
193
button : {
175
194
accept : true ,
176
195
caption : '' ,
@@ -186,7 +205,8 @@ describe('FileList', () => {
186
205
}
187
206
} ) ;
188
207
189
- await fileList . commitAllStagedFiles ( 'Initial commit' ) ;
208
+ const panel = new GitPanel ( props ) ;
209
+ await panel . commitStagedFiles ( 'Initial commit' ) ;
190
210
expect ( identity ) . toHaveBeenCalledTimes ( 2 ) ;
191
211
expect ( identity . mock . calls [ 0 ] ) . toHaveLength ( 0 ) ;
192
212
expect ( identity . mock . calls [ 1 ] ) . toEqual ( [
@@ -199,11 +219,12 @@ describe('FileList', () => {
199
219
expect ( spy ) . toHaveBeenCalledWith ( 'Initial commit' ) ;
200
220
} ) ;
201
221
202
- it ( 'should NOT commit if no user identity is set and the user reject the dialog' , async ( ) => {
203
- const spy = jest . spyOn ( GitExtension . prototype , 'commit' ) ;
222
+ it ( 'should not commit if no user identity is set and the user rejects the dialog' , async ( ) => {
223
+ const spy = jest . spyOn ( GitModel . prototype , 'commit' ) ;
224
+
204
225
// Mock identity look up
205
226
const identity = jest
206
- . spyOn ( GitExtension . prototype , 'config' )
227
+ . spyOn ( GitModel . prototype , 'config' )
207
228
. mockImplementation ( options => {
208
229
let response : Response = null ;
209
230
if ( options === undefined ) {
@@ -218,8 +239,8 @@ describe('FileList', () => {
218
239
}
219
240
return Promise . resolve ( response ) ;
220
241
} ) ;
221
- const mockApputils = apputils as jest . Mocked < typeof apputils > ;
222
- mockApputils . showDialog . mockResolvedValue ( {
242
+ const mock = apputils as jest . Mocked < typeof apputils > ;
243
+ mock . showDialog . mockResolvedValue ( {
223
244
button : {
224
245
accept : false ,
225
246
caption : '' ,
@@ -232,7 +253,8 @@ describe('FileList', () => {
232
253
value : null
233
254
} ) ;
234
255
235
- await fileList . commitAllStagedFiles ( 'Initial commit' ) ;
256
+ const panel = new GitPanel ( props ) ;
257
+ await panel . commitStagedFiles ( 'Initial commit' ) ;
236
258
expect ( identity ) . toHaveBeenCalledTimes ( 1 ) ;
237
259
expect ( identity ) . toHaveBeenCalledWith ( ) ;
238
260
expect ( spy ) . not . toHaveBeenCalled ( ) ;
0 commit comments