@@ -191,9 +191,6 @@ class Environment {
191
191
file : filename ,
192
192
path : repoPath ,
193
193
} ) ;
194
- if ( this . page ) {
195
- await this . ensureRefresh ( ) ;
196
- }
197
194
}
198
195
199
196
// browser helpers
@@ -284,16 +281,18 @@ class Environment {
284
281
async commit ( commitMessage ) {
285
282
await this . waitForElementVisible ( '.files .file .btn-default' ) ;
286
283
await this . insert ( '.staging input.form-control' , commitMessage ) ;
284
+ const postCommitProm = this . setApiListener ( '/commit' , 'POST' ) ;
287
285
await this . click ( '.commit-btn' ) ;
288
- await this . ensureRefresh ( ) ;
286
+ await postCommitProm ;
289
287
await this . waitForElementHidden ( '.files .file .btn-default' ) ;
290
288
}
291
289
292
290
async _createRef ( type , name ) {
293
291
await this . click ( '.current ~ .new-ref button.showBranchingForm' ) ;
294
292
await this . insert ( '.ref-icons.new-ref.editing input' , name ) ;
293
+ const createRefProm = type === 'branch' ? this . setApiListener ( '/branches' , 'POST' ) : this . setApiListener ( '/tags' , 'POST' )
295
294
await this . click ( `.new-ref ${ type === 'branch' ? '.btn-primary' : '.btn-default' } ` ) ;
296
- await this . ensureRefresh ( ) ;
295
+ await createRefProm ;
297
296
await this . waitForElementVisible ( `.ref.${ type } [data-ta-name="${ name } "]` ) ;
298
297
}
299
298
createTag ( name ) {
@@ -314,22 +313,73 @@ class Environment {
314
313
/* ignore */
315
314
}
316
315
await this . waitForElementHidden ( `[data-ta-action="${ action } "]:not([style*="display: none"])` ) ;
317
- await this . ensureRefresh ( ) ;
316
+ await this . ensureRedraw ( ) ;
318
317
}
319
318
320
- async refAction ( ref , local , action ) {
319
+ async _refAction ( ref , local , action , validateFunc ) {
320
+ if ( ! this [ `_${ action } ResponseWatcher` ] ) {
321
+ this . page . on ( 'response' , async ( response ) => {
322
+ const url = response . url ( ) ;
323
+ const method = response . request ( ) . method ( ) ;
324
+
325
+ if ( validateFunc ( url , method ) ) {
326
+ this . page . evaluate ( `ungit._${ action } Response = true` ) ;
327
+ }
328
+ } ) ;
329
+ this [ `_${ action } ResponseWatcher` ] = true ;
330
+ }
321
331
await this . clickOnNode ( `.branch[data-ta-name="${ ref } "][data-ta-local="${ local } "]` ) ;
322
332
await this . click ( `[data-ta-action="${ action } "]:not([style*="display: none"]) .dropmask` ) ;
323
- await this . ensureRefresh ( ) ;
324
333
await this . _verifyRefAction ( action ) ;
334
+ await this . page . waitForFunction ( `ungit._${ action } Response` , { polling : 250 } ) ;
335
+ await this . page . evaluate ( `ungit._${ action } Response = undefined` ) ;
336
+ }
337
+
338
+ async pushRefAction ( ref , local ) {
339
+ await this . _refAction ( ref , local , 'push' , ( url , method ) => {
340
+ if ( method !== 'POST' ) {
341
+ return false ;
342
+ }
343
+ if ( url . indexOf ( '/push' ) === - 1 && url . indexOf ( '/tags' ) === - 1 && url . indexOf ( '/branches' ) === - 1 ) {
344
+ return false ;
345
+ }
346
+ return true ;
347
+ } ) ;
348
+ }
349
+
350
+ async rebaseRefAction ( ref , local ) {
351
+ await this . _refAction ( ref , local , 'rebase' , ( url , method ) => {
352
+ return method === 'POST' && url . indexOf ( '/rebase' ) >= - 1 ;
353
+ } ) ;
354
+ }
355
+
356
+ async mergeRefAction ( ref , local ) {
357
+ await this . _refAction ( ref , local , 'merge' , ( url , method ) => {
358
+ return method === 'POST' && url . indexOf ( '/merge' ) >= - 1 ;
359
+ } ) ;
325
360
}
361
+
326
362
async moveRef ( ref , targetNodeCommitTitle ) {
327
363
await this . clickOnNode ( `.branch[data-ta-name="${ ref } "]` ) ;
364
+ if ( ! this . _isMoveResponseWatcherSet ) {
365
+ this . page . on ( 'response' , async ( response ) => {
366
+ const url = response . url ( ) ;
367
+ if ( response . request ( ) . method ( ) !== 'POST' ) {
368
+ return ;
369
+ }
370
+ if ( url . indexOf ( '/reset' ) === - 1 && url . indexOf ( '/tags' ) === - 1 && url . indexOf ( '/branches' ) === - 1 ) {
371
+ return ;
372
+ }
373
+ this . page . evaluate ( `ungit._moveEventResponded = true` )
374
+ } ) ;
375
+ this . _isMoveResponseWatcherSet = true ;
376
+ }
328
377
await this . click (
329
378
`[data-ta-node-title="${ targetNodeCommitTitle } "] [data-ta-action="move"]:not([style*="display: none"]) .dropmask`
330
379
) ;
331
- await this . ensureRefresh ( ) ;
332
380
await this . _verifyRefAction ( 'move' ) ;
381
+ await this . page . waitForFunction ( `ungit._moveEventResponded` ) ;
382
+ await this . page . evaluate ( 'ungit._moveEventResponded = undefined' ) ;
333
383
}
334
384
335
385
// Stop program event propagation.
@@ -365,6 +415,21 @@ class Environment {
365
415
} ) ;
366
416
}
367
417
418
+ async ensureRedraw ( ) {
419
+ if ( ! this . _gitlogResposneWatcher ) {
420
+ this . page . on ( 'response' , async ( response ) => {
421
+ if ( response . url ( ) . indexOf ( '/gitlog' ) > 0 && response . request ( ) . method ( ) === 'GET' ) {
422
+ this . page . evaluate ( 'ungit._gitlogResponse = true' ) ;
423
+ }
424
+ } ) ;
425
+ this . _gitlogResposneWatcher = true ;
426
+ }
427
+ await this . page . evaluate ( `ungit._gitlogResponse = undefined` ) ;
428
+ await this . triggerProgramEvents ( ) ;
429
+ await this . page . waitForFunction ( `ungit._gitlogResponse` , { polling : 250 } ) ;
430
+ await this . page . waitForFunction ( `ungit.__app.content().repository().graph._isLoadNodesFromApiRunning === false` , { polling : 250 } ) ;
431
+ }
432
+
368
433
// ensure UI refresh is triggered with the latest information at the time of the call.
369
434
async ensureRefresh ( ) {
370
435
logger . info ( 'ensure refresh...' ) ;
@@ -421,15 +486,15 @@ class Environment {
421
486
// to true. Use for detect if an API call was made and responded.
422
487
setApiListener ( apiPart , method , bodyMatcher = ( ) => true ) {
423
488
const randomVariable = `ungit._${ Math . floor ( Math . random ( ) * 500000 ) } ` ;
424
- this . page . evaluate ( `${ randomVariable } = undefined` ) ;
425
489
this . page . on ( 'response' , async ( response ) => {
426
490
if ( response . url ( ) . indexOf ( apiPart ) > - 1 && response . request ( ) . method ( ) === method ) {
427
491
if ( bodyMatcher ( await response . json ( ) ) ) {
428
492
// reponse body matcher is matched, set the value to true
429
493
this . page . evaluate ( `${ randomVariable } = true` ) ;
430
494
}
431
495
}
432
- } ) ;
433
- return this . page . waitForFunction ( `${ randomVariable } === true` ) ;
496
+ } , { polling : 250 } ) ;
497
+ return this . page . waitForFunction ( `${ randomVariable } === true` )
498
+ . then ( ( ) => this . page . evaluate ( `${ randomVariable } = undefined` ) ) ;
434
499
}
435
500
}
0 commit comments