@@ -18,16 +18,20 @@ describe('transition', function () {
18
18
return $transitions . create ( fromPath , $state . target ( to , null , options ) ) ;
19
19
}
20
20
21
- const wait = ( val ?) =>
21
+ const _delay = ( millis ) =>
22
+ new Promise ( resolve => setTimeout ( resolve , millis ) ) ;
23
+ const delay = ( millis ) => ( ) => _delay ( millis ) ;
24
+
25
+ const tick = ( val ?) =>
22
26
new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( val ) ) ) ;
23
27
24
28
// Use this in a .then(go('from', 'to')) when you want to run a transition you expect to succeed
25
29
const go = ( from , to , options ?) =>
26
- ( ) => makeTransition ( from , to , options ) . run ( ) . then ( wait ) ;
30
+ ( ) => makeTransition ( from , to , options ) . run ( ) . then ( tick ) ;
27
31
28
32
// Use this in a .then(goFail('from', 'to')) when you want to run a transition you expect to fail
29
33
const goFail = ( from , to , options ?) =>
30
- ( ) => makeTransition ( from , to , options ) . run ( ) . catch ( wait ) ;
34
+ ( ) => makeTransition ( from , to , options ) . run ( ) . catch ( tick ) ;
31
35
32
36
beforeEach ( ( ) => {
33
37
router = new UIRouter ( ) ;
@@ -121,7 +125,7 @@ describe('transition', function () {
121
125
122
126
var tsecond = makeTransition ( "" , "second" ) ;
123
127
tsecond . run ( )
124
- . then ( wait )
128
+ . then ( tick )
125
129
. then ( ( ) => expect ( t ) . toBe ( tsecond ) )
126
130
. then ( done ) ;
127
131
} ) ) ;
@@ -132,7 +136,7 @@ describe('transition', function () {
132
136
router . stateRegistry . register ( {
133
137
name : 'slowResolve' ,
134
138
resolve : {
135
- foo : ( ) => new Promise ( resolve => setTimeout ( resolve , 50 ) )
139
+ foo : delay ( 50 )
136
140
}
137
141
} ) ;
138
142
@@ -143,10 +147,13 @@ describe('transition', function () {
143
147
144
148
$state . go ( 'slowResolve' ) ;
145
149
146
- setTimeout ( ( ) => $state . go ( 'slowResolve' ) . transition . promise . then ( ( ) => {
147
- expect ( results ) . toEqual ( { success : 1 , error : 1 } ) ;
148
- done ( ) ;
149
- } ) , 20 ) ;
150
+ _delay ( 20 )
151
+ . then ( ( ) =>
152
+ $state . go ( 'slowResolve' ) . transition . promise )
153
+ . then ( delay ( 50 ) )
154
+ . then ( ( ) =>
155
+ expect ( results ) . toEqual ( { success : 1 , error : 1 } ) )
156
+ . then ( done )
150
157
} ) ) ;
151
158
152
159
describe ( '.onCreate()' , function ( ) {
@@ -275,7 +282,7 @@ describe('transition', function () {
275
282
var transition = makeTransition ( "" , "third" ) ;
276
283
var result = new PromiseResult ( transition . promise ) ;
277
284
transition . run ( )
278
- . then ( wait )
285
+ . then ( tick )
279
286
. then ( ( ) => {
280
287
expect ( result . called ( ) ) . toEqual ( { resolve : true , reject : false , complete : true } ) ;
281
288
expect ( typeof args . trans . from ) . toBe ( 'function' ) ;
@@ -532,7 +539,7 @@ describe('transition', function () {
532
539
533
540
transition . promise . catch ( function ( err ) { rejection = err ; } ) ;
534
541
transition . run ( )
535
- . catch ( wait )
542
+ . catch ( tick )
536
543
. then ( ( ) => {
537
544
expect ( pluck ( states , 'name' ) ) . toEqual ( [ 'A' , 'B' , 'C' ] ) ;
538
545
expect ( rejection . type ) . toEqual ( RejectType . ABORTED ) ;
@@ -547,7 +554,7 @@ describe('transition', function () {
547
554
transition . promise . catch ( function ( err ) { rejection = err ; } ) ;
548
555
549
556
transition . run ( )
550
- . catch ( wait )
557
+ . catch ( tick )
551
558
. then ( ( ) => {
552
559
expect ( pluck ( states , 'name' ) ) . toEqual ( [ 'B' , 'C' ] ) ;
553
560
expect ( rejection . type ) . toEqual ( RejectType . SUPERSEDED ) ;
@@ -574,12 +581,12 @@ describe('transition', function () {
574
581
575
582
transition . promise . catch ( function ( err ) { rejection = err ; } ) ;
576
583
transition . run ( )
577
- . then ( wait , wait )
584
+ . then ( tick , tick )
578
585
. then ( ( ) => {
579
586
// .onEnter() from A->C should have set transition2.
580
587
transition2 . promise . then ( function ( ) { transition2success = true ; } ) ;
581
588
} )
582
- . then ( wait , wait )
589
+ . then ( tick , tick )
583
590
. then ( ( ) => {
584
591
expect ( pluck ( states , 'name' ) ) . toEqual ( [ 'B' , 'C' , 'G' ] ) ;
585
592
expect ( rejection instanceof Rejection ) . toBeTruthy ( ) ;
@@ -607,7 +614,7 @@ describe('transition', function () {
607
614
} ) ;
608
615
609
616
transition . run ( )
610
- . then ( wait , wait )
617
+ . then ( tick , tick )
611
618
. then ( ( ) => expect ( log . join ( '' ) ) . toBe ( "#B^B#C^C#D^D" ) )
612
619
. then ( done ) ;
613
620
} ) ;
@@ -619,7 +626,7 @@ describe('transition', function () {
619
626
function resolveDeferredFor ( name ) {
620
627
log . push ( "^" + name ) ;
621
628
defers [ name ] . resolve ( "ok, go ahead!" ) ;
622
- return wait ( ) ;
629
+ return tick ( ) ;
623
630
}
624
631
625
632
$transitions . onEnter ( { entering : '**' } , function waitWhileEnteringState ( trans , state ) {
@@ -630,7 +637,7 @@ describe('transition', function () {
630
637
transition . promise . then ( function ( ) { log . push ( "DONE" ) ; } ) ;
631
638
transition . run ( ) ;
632
639
633
- wait ( ) . then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "#B" ) )
640
+ tick ( ) . then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "#B" ) )
634
641
635
642
. then ( ( ) => resolveDeferredFor ( "B" ) )
636
643
. then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "#B;^B;#C" ) )
@@ -667,9 +674,9 @@ describe('transition', function () {
667
674
668
675
transition . run ( ) ;
669
676
670
- wait ( ) . then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "adding resolve;Entered#B;resolving" ) )
677
+ tick ( ) . then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "adding resolve;Entered#B;resolving" ) )
671
678
. then ( ( ) => defer . resolve ( "resolvedval" ) )
672
- . then ( wait , wait )
679
+ . then ( tick , tick )
673
680
. then ( ( ) => expect ( log . join ( ';' ) ) . toBe ( "adding resolve;Entered#B;resolving;resolvedval;Entered#C;Entered#D;DONE!" ) )
674
681
. then ( done , done ) ;
675
682
} ) ) ;
0 commit comments