11import { nullTranslator } from '@jupyterlab/translation' ;
22import '@testing-library/jest-dom' ;
3- import { render , screen } from '@testing-library/react' ;
3+ import { render , screen , waitFor } from '@testing-library/react' ;
44import userEvent from '@testing-library/user-event' ;
55import 'jest' ;
66import * as React from 'react' ;
77import { IToolbarProps , Toolbar } from '../../components/Toolbar' ;
88import * as git from '../../git' ;
99import { GitExtension } from '../../model' ;
1010import { badgeClass } from '../../style/Toolbar' ;
11- import { DEFAULT_REPOSITORY_PATH , mockedRequestAPI } from '../utils' ;
1211import { CommandIDs } from '../../tokens' ;
12+ import {
13+ DEFAULT_REPOSITORY_PATH ,
14+ defaultMockedResponses ,
15+ mockedRequestAPI
16+ } from '../utils' ;
1317
1418jest . mock ( '../../git' ) ;
1519
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+
1631async function createModel ( ) {
1732 const model = new GitExtension ( ) ;
1833 model . pathRepository = DEFAULT_REPOSITORY_PATH ;
@@ -65,7 +80,18 @@ describe('Toolbar', () => {
6580 jest . restoreAllMocks ( ) ;
6681
6782 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+ ) ;
6995
7096 model = await createModel ( ) ;
7197 } ) ;
@@ -79,12 +105,14 @@ describe('Toolbar', () => {
79105 } ) ;
80106
81107 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 ( ) => {
83109 render ( < Toolbar { ...createProps ( ) } /> ) ;
84110
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+ } ) ;
88116
89117 expect (
90118 screen
@@ -93,22 +121,26 @@ describe('Toolbar', () => {
93121 ) . toHaveClass ( 'MuiBadge-invisible' ) ;
94122 } ) ;
95123
96- it ( 'should display a badge on pull icon if behind' , ( ) => {
124+ it ( 'should display a badge on pull icon if behind' , async ( ) => {
97125 render ( < Toolbar { ...createProps ( { nCommitsBehind : 1 } ) } /> ) ;
98126
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+ } ) ;
104134 } ) ;
105135
106- it ( 'should display a button to push the latest changes' , ( ) => {
136+ it ( 'should display a button to push the latest changes' , async ( ) => {
107137 render ( < Toolbar { ...createProps ( ) } /> ) ;
108138
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+ } ) ;
112144
113145 expect (
114146 screen
@@ -117,14 +149,16 @@ describe('Toolbar', () => {
117149 ) . toHaveClass ( 'MuiBadge-invisible' ) ;
118150 } ) ;
119151
120- it ( 'should display a badge on push icon if behind' , ( ) => {
152+ it ( 'should display a badge on push icon if behind' , async ( ) => {
121153 render ( < Toolbar { ...createProps ( { nCommitsAhead : 1 } ) } /> ) ;
122154
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+ } ) ;
128162 } ) ;
129163
130164 it ( 'should display a button to refresh the current repository' , ( ) => {
@@ -177,7 +211,7 @@ describe('Toolbar', () => {
177211 } ) ;
178212 } ) ;
179213
180- describe ( 'pull changes' , ( ) => {
214+ describe ( 'push/ pull changes with remote ' , ( ) => {
181215 it ( 'should pull changes when the button to pull the latest changes is clicked' , async ( ) => {
182216 const mockedExecute = jest . fn ( ) ;
183217 render (
@@ -190,6 +224,12 @@ describe('Toolbar', () => {
190224 />
191225 ) ;
192226
227+ await waitFor ( ( ) => {
228+ expect (
229+ screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
230+ ) . toBeDefined ( ) ;
231+ } ) ;
232+
193233 await userEvent . click (
194234 screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
195235 ) ;
@@ -198,40 +238,55 @@ describe('Toolbar', () => {
198238 expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPull ) ;
199239 } ) ;
200240
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 ( ) => {
202242 const mockedExecute = jest . fn ( ) ;
203243 render (
204244 < Toolbar
205245 { ...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- ] ,
216246 commands : {
217247 execute : mockedExecute
218248 } as any
219249 } ) }
220250 />
221251 ) ;
222252
253+ await waitFor ( ( ) => {
254+ expect (
255+ screen . getByRole ( 'button' , { name : 'Push committed changes' } )
256+ ) . toBeDefined ( ) ;
257+ } ) ;
258+
223259 await userEvent . click (
224- screen . getAllByRole ( 'button' , {
225- name : 'No remote repository defined'
226- } ) [ 0 ]
260+ screen . getByRole ( 'button' , { name : 'Push committed changes' } )
227261 ) ;
228262
229- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
263+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
264+ expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
230265 } ) ;
231266 } ) ;
232267
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 ( ) => {
235290 const mockedExecute = jest . fn ( ) ;
236291 render (
237292 < Toolbar
@@ -242,28 +297,21 @@ describe('Toolbar', () => {
242297 } ) }
243298 />
244299 ) ;
300+
245301 await userEvent . click (
246- screen . getByRole ( 'button' , { name : 'Push committed changes' } )
302+ screen . getAllByRole ( 'button' , {
303+ name : 'No remote repository defined'
304+ } ) [ 0 ]
247305 ) ;
248- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
249- expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
306+
307+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
250308 } ) ;
251309
252310 it ( 'should not push changes when the push button is clicked but there is no remote branch' , async ( ) => {
253311 const mockedExecute = jest . fn ( ) ;
254312 render (
255313 < Toolbar
256314 { ...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- ] ,
267315 commands : {
268316 execute : mockedExecute
269317 } as any
0 commit comments