@@ -44,18 +44,19 @@ describe('$anchorScroll', function() {
44
44
$rootScope . $apply ( function ( ) {
45
45
$location . hash ( hash ) ;
46
46
} ) ;
47
- if ( $browser . deferredFns . length ) {
48
- $browser . defer . flush ( ) ;
49
- }
50
47
} ;
51
48
}
52
49
53
- function expectScrollingToTop ( $window ) {
54
- forEach ( elmSpy , function ( spy , id ) {
55
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
56
- } ) ;
50
+ function expectAsync ( ) {
51
+ var expectations = sliceArgs ( arguments ) ;
57
52
58
- expect ( $window . scrollTo ) . toHaveBeenCalledWith ( 0 , 0 ) ;
53
+ return function ( $rootScope ) {
54
+ $rootScope . $evalAsync ( function ( ) {
55
+ expectations . forEach ( function ( expectation ) {
56
+ inject ( expectation ) ;
57
+ } ) ;
58
+ } ) ;
59
+ } ;
59
60
}
60
61
61
62
function expectScrollingTo ( identifierCountMap ) {
@@ -79,6 +80,14 @@ describe('$anchorScroll', function() {
79
80
} ;
80
81
}
81
82
83
+ function expectScrollingToTop ( $window ) {
84
+ forEach ( elmSpy , function ( spy , id ) {
85
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
86
+ } ) ;
87
+
88
+ expect ( $window . scrollTo ) . toHaveBeenCalledWith ( 0 , 0 ) ;
89
+ }
90
+
82
91
function expectNoScrolling ( ) {
83
92
return expectScrollingTo ( NaN ) ;
84
93
}
@@ -212,13 +221,13 @@ describe('$anchorScroll', function() {
212
221
addElements ( 'id=some1' ) ,
213
222
214
223
changeHashTo ( 'some1' ) ,
215
- expectNoScrolling ( ) ,
224
+ expectAsync ( expectNoScrolling ( ) ) ,
216
225
217
226
updateMockReadyState ( 'some-arbitrary-state' ) ,
218
- expectNoScrolling ( ) ,
227
+ expectAsync ( expectNoScrolling ( ) ) ,
219
228
220
229
updateMockReadyState ( 'complete' ) ,
221
- expectScrollingTo ( 'id=some1' ) ) ) ;
230
+ expectAsync ( expectScrollingTo ( 'id=some1' ) ) ) ) ;
222
231
223
232
224
233
it ( 'should only register once for execution when `document.readyState === "complete"' , inject (
@@ -230,10 +239,10 @@ describe('$anchorScroll', function() {
230
239
updateMockReadyState ( 'some-other-arbitrary-state' ) ,
231
240
changeHashTo ( 'some1' ) ,
232
241
changeHashTo ( 'some2' ) ,
233
- expectNoScrolling ( ) ,
242
+ expectAsync ( expectNoScrolling ( ) ) ,
234
243
235
244
updateMockReadyState ( 'complete' ) ,
236
- expectScrollingTo ( 'id=some2' ) ) ) ;
245
+ expectAsync ( expectScrollingTo ( 'id=some2' ) ) ) ) ;
237
246
238
247
239
248
it ( 'should properly register and unregister listeners for `readystatechange` event' , inject (
@@ -247,7 +256,7 @@ describe('$anchorScroll', function() {
247
256
changeHashTo ( 'some2' ) ,
248
257
updateMockReadyState ( 'complete' ) ,
249
258
250
- function ( ) {
259
+ expectAsync ( function ( ) {
251
260
expect ( docSpies . addEventListener . callCount ) . toBe ( 1 ) ;
252
261
expect ( docSpies . addEventListener ) .
253
262
toHaveBeenCalledWith ( 'readystatechange' , jasmine . any ( Function ) ) ;
@@ -259,7 +268,7 @@ describe('$anchorScroll', function() {
259
268
var registeredListener = docSpies . addEventListener . calls [ 0 ] . args [ 1 ] ;
260
269
var unregisteredListener = docSpies . removeEventListener . calls [ 0 ] . args [ 1 ] ;
261
270
expect ( unregisteredListener ) . toBe ( registeredListener ) ;
262
- } ) ) ;
271
+ } ) ) ) ;
263
272
264
273
265
274
it ( 'should scroll immediately if `readyState === "complete"`' , inject (
@@ -269,11 +278,12 @@ describe('$anchorScroll', function() {
269
278
updateMockReadyState ( 'complete' ) ,
270
279
changeHashTo ( 'some1' ) ,
271
280
272
- expectScrollingTo ( 'id=some1' ) ,
273
- function ( ) {
274
- expect ( docSpies . addEventListener . callCount ) . toBe ( 0 ) ;
275
- expect ( docSpies . removeEventListener . callCount ) . toBe ( 0 ) ;
276
- } ) ) ;
281
+ expectAsync (
282
+ expectScrollingTo ( 'id=some1' ) ,
283
+ function ( ) {
284
+ expect ( docSpies . addEventListener . callCount ) . toBe ( 0 ) ;
285
+ expect ( docSpies . removeEventListener . callCount ) . toBe ( 0 ) ;
286
+ } ) ) ) ;
277
287
278
288
279
289
describe ( 'watcher' , function ( ) {
@@ -308,7 +318,7 @@ describe('$anchorScroll', function() {
308
318
initAnchorScroll ( ) ,
309
319
addElements ( 'id=some' ) ,
310
320
changeHashTo ( 'some' ) ,
311
- expectScrollingTo ( 'id=some' )
321
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
312
322
) ;
313
323
} ) ;
314
324
@@ -319,7 +329,7 @@ describe('$anchorScroll', function() {
319
329
initAnchorScroll ( ) ,
320
330
addElements ( 'id=some' ) ,
321
331
changeHashTo ( 'some' ) ,
322
- expectScrollingTo ( 'id=some' )
332
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
323
333
) ;
324
334
} ) ;
325
335
@@ -337,7 +347,7 @@ describe('$anchorScroll', function() {
337
347
initAnchorScroll ( ) ,
338
348
addElements ( 'id=some' ) ,
339
349
changeHashTo ( 'other' ) ,
340
- expectNoScrolling ( )
350
+ expectAsync ( expectNoScrolling ( ) )
341
351
) ;
342
352
} ) ;
343
353
@@ -348,7 +358,7 @@ describe('$anchorScroll', function() {
348
358
initAnchorScroll ( ) ,
349
359
addElements ( 'id=some' ) ,
350
360
changeHashTo ( 'some' ) ,
351
- expectScrollingTo ( 'id=some' )
361
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
352
362
) ;
353
363
} ) ;
354
364
@@ -361,7 +371,7 @@ describe('$anchorScroll', function() {
361
371
inject (
362
372
addElements ( 'id=fake' ) ,
363
373
changeHashTo ( 'fake' ) ,
364
- expectNoScrolling ( )
374
+ expectAsync ( expectNoScrolling ( ) )
365
375
) ;
366
376
} ) ;
367
377
@@ -374,7 +384,7 @@ describe('$anchorScroll', function() {
374
384
inject (
375
385
addElements ( 'id=fake' ) ,
376
386
changeHashTo ( 'fake' ) ,
377
- expectNoScrolling ( ) ,
387
+ expectAsync ( expectNoScrolling ( ) ) ,
378
388
callAnchorScroll ( ) ,
379
389
expectScrollingTo ( 'id=fake' )
380
390
) ;
@@ -389,7 +399,7 @@ describe('$anchorScroll', function() {
389
399
function expectScrollingWithOffset ( identifierCountMap , offsetList ) {
390
400
var list = isArray ( offsetList ) ? offsetList : [ offsetList ] ;
391
401
392
- return function ( $window ) {
402
+ return function ( $rootScope , $ window) {
393
403
inject ( expectScrollingTo ( identifierCountMap ) ) ;
394
404
expect ( $window . scrollBy . callCount ) . toBe ( list . length ) ;
395
405
forEach ( list , function ( offset , idx ) {
@@ -437,6 +447,7 @@ describe('$anchorScroll', function() {
437
447
438
448
var yOffsetNumber = 50 ;
439
449
450
+
440
451
beforeEach ( inject (
441
452
setupBodyForOffsetTesting ( ) ,
442
453
setYOffset ( yOffsetNumber ) ) ) ;
@@ -445,15 +456,15 @@ describe('$anchorScroll', function() {
445
456
it ( 'should scroll with vertical offset' , inject (
446
457
addElements ( 'id=some' ) ,
447
458
changeHashTo ( 'some' ) ,
448
- expectScrollingWithOffset ( 'id=some' , yOffsetNumber ) ) ) ;
459
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , yOffsetNumber ) ) ) ) ;
449
460
450
461
451
462
it ( 'should use the correct vertical offset when changing `yOffset` at runtime' , inject (
452
463
addElements ( 'id=some' ) ,
453
464
changeHashTo ( 'some' ) ,
454
465
setYOffset ( yOffsetNumber - 10 ) ,
455
466
callAnchorScroll ( ) ,
456
- expectScrollingWithOffset ( { 'id=some' : 2 } , [ yOffsetNumber , yOffsetNumber - 10 ] ) ) ) ;
467
+ expectAsync ( expectScrollingWithOffset ( { 'id=some' : 2 } , [ yOffsetNumber , yOffsetNumber - 10 ] ) ) ) ) ;
457
468
458
469
459
470
it ( 'should adjust the vertical offset for elements near the end of the page' , function ( ) {
@@ -486,9 +497,10 @@ describe('$anchorScroll', function() {
486
497
} ,
487
498
changeHashTo ( 'some2' ) ,
488
499
updateMockPageYOffset ( ) ,
489
- resetAllSpies ( ) ,
490
- callAnchorScroll ( ) ,
491
- expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ;
500
+ expectAsync (
501
+ resetAllSpies ( ) ,
502
+ callAnchorScroll ( ) ,
503
+ expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ) ;
492
504
} ) ;
493
505
} ) ;
494
506
@@ -512,15 +524,16 @@ describe('$anchorScroll', function() {
512
524
changeHashTo ( 'name2' ) ,
513
525
changeHashTo ( 'id1' ) ,
514
526
callAnchorScroll ( ) ,
515
- expectScrollingWithOffset ( {
516
- 'id=id1' : 3 ,
517
- 'name=name2' : 1
518
- } , [
519
- 1 * increment ,
520
- 2 * increment ,
521
- 3 * increment ,
522
- 4 * increment
523
- ] ) ) ;
527
+ expectAsync (
528
+ expectScrollingWithOffset ( {
529
+ 'id=id1' : 3 ,
530
+ 'name=name2' : 1
531
+ } , [
532
+ 1 * increment ,
533
+ 2 * increment ,
534
+ 3 * increment ,
535
+ 4 * increment
536
+ ] ) ) ) ;
524
537
} ) ;
525
538
} ) ;
526
539
@@ -551,7 +564,7 @@ describe('$anchorScroll', function() {
551
564
} ) ,
552
565
addElements ( 'id=some' ) ,
553
566
changeHashTo ( 'some' ) ,
554
- expectScrollingWithOffset ( 'id=some' , 50 ) ) ) ;
567
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , 50 ) ) ) ) ;
555
568
556
569
557
570
it ( 'should scroll with vertical offset when `top > 0`' , inject (
@@ -562,7 +575,7 @@ describe('$anchorScroll', function() {
562
575
} ) ,
563
576
addElements ( 'id=some' ) ,
564
577
changeHashTo ( 'some' ) ,
565
- expectScrollingWithOffset ( 'id=some' , 100 ) ) ) ;
578
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , 100 ) ) ) ) ;
566
579
567
580
568
581
it ( 'should scroll without vertical offset when `position !== fixed`' , inject (
@@ -573,7 +586,7 @@ describe('$anchorScroll', function() {
573
586
} ) ,
574
587
addElements ( 'id=some' ) ,
575
588
changeHashTo ( 'some' ) ,
576
- expectScrollingWithoutOffset ( 'id=some' ) ) ) ;
589
+ expectAsync ( expectScrollingWithoutOffset ( 'id=some' ) ) ) ) ;
577
590
} ) ;
578
591
} ) ;
579
592
} ) ;
0 commit comments