@@ -152,6 +152,61 @@ describe('transition', function () {
152
152
} ) , 20 ) ;
153
153
} ) ) ;
154
154
155
+ describe ( '.onBefore()' , function ( ) {
156
+ beforeEach ( ( ) => $state . defaultErrorHandler ( ( ) => { } ) ) ;
157
+
158
+ it ( 'should stop running remaining hooks if hook modifies transition synchronously' , ( ( done ) => {
159
+ let counter = 0 ;
160
+ let increment = ( amount ) => {
161
+ return ( ) => {
162
+ counter += amount ;
163
+ return false ;
164
+ } ;
165
+ } ;
166
+
167
+ $transitions . onBefore ( { } , increment ( 1 ) , { priority : 2 } ) ;
168
+ $transitions . onBefore ( { } , increment ( 100 ) , { priority : 1 } ) ;
169
+
170
+ Promise . resolve ( )
171
+ . then ( goFail ( 'first' , 'second' ) )
172
+ . then ( ( ) => expect ( counter ) . toBe ( 1 ) )
173
+ . then ( goFail ( 'second' , 'third' ) )
174
+ . then ( ( ) => expect ( counter ) . toBe ( 2 ) )
175
+ . then ( done ) ;
176
+ } ) ) ;
177
+
178
+ it ( 'should stop running remaining hooks when synchronous result throws or returns false|TargetState' , ( ( done ) => {
179
+ let current = null ;
180
+
181
+ $transitions . onBefore ( { } , ( t ) => { current = t . to ( ) . name ; } ) ;
182
+ $transitions . onBefore ( { to : 'first' } , ( ) => {
183
+ throw Error ( 'first-error' ) ;
184
+ } , { priority : 1 } ) ;
185
+ $transitions . onBefore ( { to : 'second' } , ( ) => false , { priority : 3 } ) ;
186
+ $transitions . onBefore ( { to : 'third' } , ( ) => $state . target ( 'A' ) , { priority : 2 } ) ;
187
+
188
+ Promise . resolve ( )
189
+ . then ( goFail ( 'A' , 'first' ) )
190
+ . then ( ( res ) => {
191
+ expect ( res . type ) . toBe ( RejectType . ERROR ) ;
192
+ expect ( current ) . toBe ( null ) ;
193
+ } )
194
+ . then ( goFail ( 'A' , 'second' ) )
195
+ . then ( ( res ) => {
196
+ expect ( res . type ) . toBe ( RejectType . ABORTED ) ;
197
+ expect ( current ) . toBe ( null ) ;
198
+ } )
199
+ . then ( goFail ( 'A' , 'third' ) )
200
+ . then ( ( res ) => {
201
+ expect ( res . type ) . toBe ( RejectType . SUPERSEDED ) ;
202
+ expect ( current ) . toBe ( null ) ;
203
+ } )
204
+ . then ( go ( 'A' , 'B' ) )
205
+ . then ( ( ) => expect ( current ) . toBe ( 'B' ) )
206
+ . then ( done ) ;
207
+ } ) ) ;
208
+ } ) ;
209
+
155
210
describe ( '.onStart()' , function ( ) {
156
211
it ( 'should fire matching events when transition starts' , ( ( done ) => {
157
212
var t = null ;
@@ -317,7 +372,9 @@ describe('transition', function () {
317
372
. then ( done ) ;
318
373
} ) ) ;
319
374
320
- it ( 'should be called even if other .onSuccess() callbacks fail (throw errors, etc)' , ( ( done ) => {
375
+ it ( 'should call all .onSuccess() even when callbacks fail (throw errors, etc)' , ( ( done ) => {
376
+ $transitions . onSuccess ( { from : "*" , to : "*" } , ( ) => false ) ;
377
+ $transitions . onSuccess ( { from : "*" , to : "*" } , ( ) => $state . target ( 'A' ) ) ;
321
378
$transitions . onSuccess ( { from : "*" , to : "*" } , function ( ) { throw new Error ( "oops!" ) ; } ) ;
322
379
$transitions . onSuccess ( { from : "*" , to : "*" } , function ( trans ) { states . push ( trans . to ( ) . name ) ; } ) ;
323
380
@@ -364,6 +421,33 @@ describe('transition', function () {
364
421
. then ( ( ) => expect ( hooks ) . toEqual ( [ 'splatsplat' , 'AD' ] ) )
365
422
. then ( done ) ;
366
423
} ) ) ;
424
+
425
+ it ( 'should call all error handlers when transition fails.' , ( ( done ) => {
426
+ let count = 0 ;
427
+
428
+ $state . defaultErrorHandler ( ( ) => { } ) ;
429
+ $transitions . onStart ( { } , ( ) => false ) ;
430
+ $transitions . onError ( { } , ( ) => {
431
+ count += 1 ;
432
+ return false ;
433
+ } ) ;
434
+ $transitions . onError ( { } , ( ) => {
435
+ count += 10 ;
436
+ $state . target ( 'A' ) ;
437
+ } ) ;
438
+ $transitions . onError ( { } , function ( ) {
439
+ count += 100 ;
440
+ throw new Error ( "oops!" ) ;
441
+ } ) ;
442
+ $transitions . onError ( { } , ( ) => {
443
+ count += 1000 ;
444
+ } ) ;
445
+
446
+ Promise . resolve ( )
447
+ . then ( goFail ( "B" , "C" ) )
448
+ . then ( ( ) => expect ( count ) . toBe ( 1111 ) )
449
+ . then ( done ) ;
450
+ } ) ) ;
367
451
} ) ;
368
452
369
453
// Test for #2866
0 commit comments