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,48 +224,61 @@ describe('Toolbar', () => {
190224 />
191225 ) ;
192226
193- await userEvent . click (
194- screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
195- ) ;
227+ await waitFor ( async ( ) => {
228+ await userEvent . click (
229+ screen . getByRole ( 'button' , { name : 'Pull latest changes' } )
230+ ) ;
231+ } ) ;
196232
197233 expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
198234 expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPull ) ;
199235 } ) ;
200236
201- it ( 'should not pull changes when the pull button is clicked but there is no remote branch ' , async ( ) => {
237+ it ( 'should push changes when the button to push the latest changes is clicked ' , async ( ) => {
202238 const mockedExecute = jest . fn ( ) ;
203239 render (
204240 < Toolbar
205241 { ...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- ] ,
216242 commands : {
217243 execute : mockedExecute
218244 } as any
219245 } ) }
220246 />
221247 ) ;
222248
223- await userEvent . click (
224- screen . getAllByRole ( 'button' , {
225- name : 'No remote repository defined'
226- } ) [ 0 ]
227- ) ;
249+ await waitFor ( async ( ) => {
250+ await userEvent . click (
251+ screen . getByRole ( 'button' , { name : 'Push committed changes' } )
252+ ) ;
253+ } ) ;
228254
229- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
255+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
256+ expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
230257 } ) ;
231258 } ) ;
232259
233- describe ( 'push changes' , ( ) => {
234- it ( 'should push changes when the button to push the latest changes is clicked' , async ( ) => {
260+ describe ( 'push/pull changes without remote' , ( ) => {
261+ beforeEach ( async ( ) => {
262+ jest . restoreAllMocks ( ) ;
263+
264+ const mock = git as jest . Mocked < typeof git > ;
265+ mock . requestAPI . mockImplementation (
266+ mockedRequestAPI ( {
267+ responses : {
268+ ...defaultMockedResponses ,
269+ 'remote/show' : {
270+ body : ( ) => {
271+ return { code : - 1 , remotes : [ ] } ;
272+ }
273+ }
274+ }
275+ } ) as any
276+ ) ;
277+
278+ model = await createModel ( ) ;
279+ } ) ;
280+
281+ it ( 'should not pull changes when the pull button is clicked but there is no remote branch' , async ( ) => {
235282 const mockedExecute = jest . fn ( ) ;
236283 render (
237284 < Toolbar
@@ -242,28 +289,21 @@ describe('Toolbar', () => {
242289 } ) }
243290 />
244291 ) ;
292+
245293 await userEvent . click (
246- screen . getByRole ( 'button' , { name : 'Push committed changes' } )
294+ screen . getAllByRole ( 'button' , {
295+ name : 'No remote repository defined'
296+ } ) [ 0 ]
247297 ) ;
248- expect ( mockedExecute ) . toHaveBeenCalledTimes ( 1 ) ;
249- expect ( mockedExecute ) . toHaveBeenCalledWith ( CommandIDs . gitPush ) ;
298+
299+ expect ( mockedExecute ) . toHaveBeenCalledTimes ( 0 ) ;
250300 } ) ;
251301
252302 it ( 'should not push changes when the push button is clicked but there is no remote branch' , async ( ) => {
253303 const mockedExecute = jest . fn ( ) ;
254304 render (
255305 < Toolbar
256306 { ...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- ] ,
267307 commands : {
268308 execute : mockedExecute
269309 } as any
0 commit comments