1
1
import { nullTranslator } from '@jupyterlab/translation' ;
2
2
import '@testing-library/jest-dom' ;
3
- import { render , screen } from '@testing-library/react' ;
3
+ import { render , screen , waitFor } from '@testing-library/react' ;
4
4
import userEvent from '@testing-library/user-event' ;
5
5
import 'jest' ;
6
6
import * as React from 'react' ;
7
7
import { IToolbarProps , Toolbar } from '../../components/Toolbar' ;
8
8
import * as git from '../../git' ;
9
9
import { GitExtension } from '../../model' ;
10
10
import { badgeClass } from '../../style/Toolbar' ;
11
- import { DEFAULT_REPOSITORY_PATH , mockedRequestAPI } from '../utils' ;
12
11
import { CommandIDs } from '../../tokens' ;
12
+ import {
13
+ DEFAULT_REPOSITORY_PATH ,
14
+ defaultMockedResponses ,
15
+ mockedRequestAPI
16
+ } from '../utils' ;
13
17
14
18
jest . mock ( '../../git' ) ;
15
19
20
+ const REMOTES = [
21
+ {
22
+ name : 'test' ,
23
+ url : 'https://test.com'
24
+ } ,
25
+ {
26
+ name : 'origin' ,
27
+ url : 'https://origin.com'
28
+ }
29
+ ] ;
30
+
16
31
async function createModel ( ) {
17
32
const model = new GitExtension ( ) ;
18
33
model . pathRepository = DEFAULT_REPOSITORY_PATH ;
@@ -65,7 +80,18 @@ describe('Toolbar', () => {
65
80
jest . restoreAllMocks ( ) ;
66
81
67
82
const mock = git as jest . Mocked < typeof git > ;
68
- mock . requestAPI . mockImplementation ( mockedRequestAPI ( ) as any ) ;
83
+ mock . requestAPI . mockImplementation (
84
+ mockedRequestAPI ( {
85
+ responses : {
86
+ ...defaultMockedResponses ,
87
+ 'remote/show' : {
88
+ body : ( ) => {
89
+ return { code : 0 , remotes : REMOTES } ;
90
+ }
91
+ }
92
+ }
93
+ } ) as any
94
+ ) ;
69
95
70
96
model = await createModel ( ) ;
71
97
} ) ;
@@ -79,12 +105,14 @@ describe('Toolbar', () => {
79
105
} ) ;
80
106
81
107
describe ( 'render' , ( ) => {
82
- it ( 'should display a button to pull the latest changes' , ( ) => {
108
+ it ( 'should display a button to pull the latest changes' , async ( ) => {
83
109
render ( < Toolbar { ...createProps ( ) } /> ) ;
84
110
85
- expect (
86
- screen . getAllByRole ( 'button' , { name : 'Pull latest changes' } )
87
- ) . toBeDefined ( ) ;
111
+ await waitFor ( ( ) => {
112
+ expect (
113
+ screen . getAllByRole ( 'button' , { name : 'Pull latest changes' } )
114
+ ) . toBeDefined ( ) ;
115
+ } ) ;
88
116
89
117
expect (
90
118
screen
@@ -93,22 +121,26 @@ describe('Toolbar', () => {
93
121
) . toHaveClass ( 'MuiBadge-invisible' ) ;
94
122
} ) ;
95
123
96
- it ( 'should display a badge on pull icon if behind' , ( ) => {
124
+ it ( 'should display a badge on pull icon if behind' , async ( ) => {
97
125
render ( < Toolbar { ...createProps ( { nCommitsBehind : 1 } ) } /> ) ;
98
126
99
- expect (
100
- screen
101
- . getByRole ( 'button' , { name : / ^ P u l l l a t e s t c h a n g e s / } )
102
- . parentElement ?. querySelector ( `.${ badgeClass } > .MuiBadge-badge` )
103
- ) . not . toHaveClass ( 'MuiBadge-invisible' ) ;
127
+ await waitFor ( ( ) => {
128
+ expect (
129
+ screen
130
+ . getByRole ( 'button' , { name : / ^ P u l l l a t e s t c h a n g e s / } )
131
+ . parentElement ?. querySelector ( `.${ badgeClass } > .MuiBadge-badge` )
132
+ ) . not . toHaveClass ( 'MuiBadge-invisible' ) ;
133
+ } ) ;
104
134
} ) ;
105
135
106
- it ( 'should display a button to push the latest changes' , ( ) => {
136
+ it ( 'should display a button to push the latest changes' , async ( ) => {
107
137
render ( < Toolbar { ...createProps ( ) } /> ) ;
108
138
109
- expect (
110
- screen . getAllByRole ( 'button' , { name : 'Push committed changes' } )
111
- ) . toBeDefined ( ) ;
139
+ await waitFor ( ( ) => {
140
+ expect (
141
+ screen . getAllByRole ( 'button' , { name : 'Push committed changes' } )
142
+ ) . toBeDefined ( ) ;
143
+ } ) ;
112
144
113
145
expect (
114
146
screen
@@ -117,14 +149,16 @@ describe('Toolbar', () => {
117
149
) . toHaveClass ( 'MuiBadge-invisible' ) ;
118
150
} ) ;
119
151
120
- it ( 'should display a badge on push icon if behind' , ( ) => {
152
+ it ( 'should display a badge on push icon if behind' , async ( ) => {
121
153
render ( < Toolbar { ...createProps ( { nCommitsAhead : 1 } ) } /> ) ;
122
154
123
- expect (
124
- screen
125
- . getByRole ( 'button' , { name : / ^ P u s h c o m m i t t e d c h a n g e s / } )
126
- . parentElement ?. querySelector ( `.${ badgeClass } > .MuiBadge-badge` )
127
- ) . not . toHaveClass ( 'MuiBadge-invisible' ) ;
155
+ await waitFor ( ( ) => {
156
+ expect (
157
+ screen
158
+ . getByRole ( 'button' , { name : / ^ P u s h c o m m i t t e d c h a n g e s / } )
159
+ . parentElement ?. querySelector ( `.${ badgeClass } > .MuiBadge-badge` )
160
+ ) . not . toHaveClass ( 'MuiBadge-invisible' ) ;
161
+ } ) ;
128
162
} ) ;
129
163
130
164
it ( 'should display a button to refresh the current repository' , ( ) => {
@@ -177,7 +211,7 @@ describe('Toolbar', () => {
177
211
} ) ;
178
212
} ) ;
179
213
180
- describe ( 'pull changes' , ( ) => {
214
+ describe ( 'push/ pull changes with remote ' , ( ) => {
181
215
it ( 'should pull changes when the button to pull the latest changes is clicked' , async ( ) => {
182
216
const mockedExecute = jest . fn ( ) ;
183
217
render (
@@ -190,6 +224,12 @@ describe('Toolbar', () => {
190
224
/>
191
225
) ;
192
226
227
+ await waitFor ( ( ) => {
228
+ expect (
229
+ screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
230
+ ) . toBeDefined ( ) ;
231
+ } ) ;
232
+
193
233
await userEvent . click (
194
234
screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
195
235
) ;
@@ -198,40 +238,55 @@ describe('Toolbar', () => {
198
238
expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPull ) ;
199
239
} ) ;
200
240
201
- it ( 'should not pull changes when the pull button is clicked but there is no remote branch ' , async ( ) => {
241
+ it ( 'should push changes when the button to push the latest changes is clicked ' , async ( ) => {
202
242
const mockedExecute = jest . fn ( ) ;
203
243
render (
204
244
< Toolbar
205
245
{ ...createProps ( {
206
- branches : [
207
- {
208
- is_current_branch : true ,
209
- is_remote_branch : false ,
210
- name : 'main' ,
211
- upstream : '' ,
212
- top_commit : '' ,
213
- tag : ''
214
- }
215
- ] ,
216
246
commands : {
217
247
execute : mockedExecute
218
248
} as any
219
249
} ) }
220
250
/>
221
251
) ;
222
252
253
+ await waitFor ( ( ) => {
254
+ expect (
255
+ screen . getByRole ( 'button' , { name : 'Push committed changes' } )
256
+ ) . toBeDefined ( ) ;
257
+ } ) ;
258
+
223
259
await userEvent . click (
224
- screen . getAllByRole ( 'button' , {
225
- name : 'No remote repository defined'
226
- } ) [ 0 ]
260
+ screen . getByRole ( 'button' , { name : 'Push committed changes' } )
227
261
) ;
228
262
229
- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
263
+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
264
+ expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
230
265
} ) ;
231
266
} ) ;
232
267
233
- describe ( 'push changes' , ( ) => {
234
- it ( 'should push changes when the button to push the latest changes is clicked' , async ( ) => {
268
+ describe ( 'push/pull changes without remote' , ( ) => {
269
+ beforeEach ( async ( ) => {
270
+ jest . restoreAllMocks ( ) ;
271
+
272
+ const mock = git as jest . Mocked < typeof git > ;
273
+ mock . requestAPI . mockImplementation (
274
+ mockedRequestAPI ( {
275
+ responses : {
276
+ ...defaultMockedResponses ,
277
+ 'remote/show' : {
278
+ body : ( ) => {
279
+ return { code : - 1 , remotes : [ ] } ;
280
+ }
281
+ }
282
+ }
283
+ } ) as any
284
+ ) ;
285
+
286
+ model = await createModel ( ) ;
287
+ } ) ;
288
+
289
+ it ( 'should not pull changes when the pull button is clicked but there is no remote branch' , async ( ) => {
235
290
const mockedExecute = jest . fn ( ) ;
236
291
render (
237
292
< Toolbar
@@ -242,28 +297,21 @@ describe('Toolbar', () => {
242
297
} ) }
243
298
/>
244
299
) ;
300
+
245
301
await userEvent . click (
246
- screen . getByRole ( 'button' , { name : 'Push committed changes' } )
302
+ screen . getAllByRole ( 'button' , {
303
+ name : 'No remote repository defined'
304
+ } ) [ 0 ]
247
305
) ;
248
- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
249
- expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
306
+
307
+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
250
308
} ) ;
251
309
252
310
it ( 'should not push changes when the push button is clicked but there is no remote branch' , async ( ) => {
253
311
const mockedExecute = jest . fn ( ) ;
254
312
render (
255
313
< Toolbar
256
314
{ ...createProps ( {
257
- branches : [
258
- {
259
- is_current_branch : true ,
260
- is_remote_branch : false ,
261
- name : 'main' ,
262
- upstream : '' ,
263
- top_commit : '' ,
264
- tag : ''
265
- }
266
- ] ,
267
315
commands : {
268
316
execute : mockedExecute
269
317
} as any
0 commit comments