@@ -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 ( ) {
@@ -301,7 +311,7 @@ describe('$anchorScroll', function() {
301
311
inject (
302
312
addElements ( 'id=some' ) ,
303
313
changeHashTo ( 'some' ) ,
304
- expectScrollingTo ( 'id=some' )
314
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
305
315
) ;
306
316
} ) ;
307
317
@@ -311,7 +321,7 @@ describe('$anchorScroll', function() {
311
321
inject (
312
322
addElements ( 'id=some' ) ,
313
323
changeHashTo ( 'some' ) ,
314
- expectScrollingTo ( 'id=some' )
324
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
315
325
) ;
316
326
} ) ;
317
327
@@ -321,7 +331,7 @@ describe('$anchorScroll', function() {
321
331
inject (
322
332
addElements ( 'id=some' ) ,
323
333
changeHashTo ( 'other' ) ,
324
- expectNoScrolling ( )
334
+ expectAsync ( expectNoScrolling ( ) )
325
335
) ;
326
336
} ) ;
327
337
@@ -331,7 +341,7 @@ describe('$anchorScroll', function() {
331
341
inject (
332
342
addElements ( 'id=some' ) ,
333
343
changeHashTo ( 'some' ) ,
334
- expectScrollingTo ( 'id=some' )
344
+ expectAsync ( expectScrollingTo ( 'id=some' ) )
335
345
) ;
336
346
} ) ;
337
347
@@ -344,7 +354,7 @@ describe('$anchorScroll', function() {
344
354
inject (
345
355
addElements ( 'id=fake' ) ,
346
356
changeHashTo ( 'fake' ) ,
347
- expectNoScrolling ( )
357
+ expectAsync ( expectNoScrolling ( ) )
348
358
) ;
349
359
} ) ;
350
360
@@ -357,7 +367,7 @@ describe('$anchorScroll', function() {
357
367
inject (
358
368
addElements ( 'id=fake' ) ,
359
369
changeHashTo ( 'fake' ) ,
360
- expectNoScrolling ( ) ,
370
+ expectAsync ( expectNoScrolling ( ) ) ,
361
371
callAnchorScroll ( ) ,
362
372
expectScrollingTo ( 'id=fake' )
363
373
) ;
@@ -372,7 +382,7 @@ describe('$anchorScroll', function() {
372
382
function expectScrollingWithOffset ( identifierCountMap , offsetList ) {
373
383
var list = isArray ( offsetList ) ? offsetList : [ offsetList ] ;
374
384
375
- return function ( $window ) {
385
+ return function ( $rootScope , $ window) {
376
386
inject ( expectScrollingTo ( identifierCountMap ) ) ;
377
387
expect ( $window . scrollBy . callCount ) . toBe ( list . length ) ;
378
388
forEach ( list , function ( offset , idx ) {
@@ -420,6 +430,7 @@ describe('$anchorScroll', function() {
420
430
421
431
var yOffsetNumber = 50 ;
422
432
433
+
423
434
beforeEach ( inject (
424
435
setupBodyForOffsetTesting ( ) ,
425
436
setYOffset ( yOffsetNumber ) ) ) ;
@@ -428,15 +439,15 @@ describe('$anchorScroll', function() {
428
439
it ( 'should scroll with vertical offset' , inject (
429
440
addElements ( 'id=some' ) ,
430
441
changeHashTo ( 'some' ) ,
431
- expectScrollingWithOffset ( 'id=some' , yOffsetNumber ) ) ) ;
442
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , yOffsetNumber ) ) ) ) ;
432
443
433
444
434
445
it ( 'should use the correct vertical offset when changing `yOffset` at runtime' , inject (
435
446
addElements ( 'id=some' ) ,
436
447
changeHashTo ( 'some' ) ,
437
448
setYOffset ( yOffsetNumber - 10 ) ,
438
449
callAnchorScroll ( ) ,
439
- expectScrollingWithOffset ( { 'id=some' : 2 } , [ yOffsetNumber , yOffsetNumber - 10 ] ) ) ) ;
450
+ expectAsync ( expectScrollingWithOffset ( { 'id=some' : 2 } , [ yOffsetNumber , yOffsetNumber - 10 ] ) ) ) ) ;
440
451
441
452
442
453
it ( 'should adjust the vertical offset for elements near the end of the page' , function ( ) {
@@ -469,9 +480,10 @@ describe('$anchorScroll', function() {
469
480
} ,
470
481
changeHashTo ( 'some2' ) ,
471
482
updateMockPageYOffset ( ) ,
472
- resetAllSpies ( ) ,
473
- callAnchorScroll ( ) ,
474
- expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ;
483
+ expectAsync (
484
+ resetAllSpies ( ) ,
485
+ callAnchorScroll ( ) ,
486
+ expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ) ;
475
487
} ) ;
476
488
} ) ;
477
489
@@ -495,15 +507,16 @@ describe('$anchorScroll', function() {
495
507
changeHashTo ( 'name2' ) ,
496
508
changeHashTo ( 'id1' ) ,
497
509
callAnchorScroll ( ) ,
498
- expectScrollingWithOffset ( {
499
- 'id=id1' : 3 ,
500
- 'name=name2' : 1
501
- } , [
502
- 1 * increment ,
503
- 2 * increment ,
504
- 3 * increment ,
505
- 4 * increment
506
- ] ) ) ;
510
+ expectAsync (
511
+ expectScrollingWithOffset ( {
512
+ 'id=id1' : 3 ,
513
+ 'name=name2' : 1
514
+ } , [
515
+ 1 * increment ,
516
+ 2 * increment ,
517
+ 3 * increment ,
518
+ 4 * increment
519
+ ] ) ) ) ;
507
520
} ) ;
508
521
} ) ;
509
522
@@ -534,7 +547,7 @@ describe('$anchorScroll', function() {
534
547
} ) ,
535
548
addElements ( 'id=some' ) ,
536
549
changeHashTo ( 'some' ) ,
537
- expectScrollingWithOffset ( 'id=some' , 50 ) ) ) ;
550
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , 50 ) ) ) ) ;
538
551
539
552
540
553
it ( 'should scroll with vertical offset when `top > 0`' , inject (
@@ -545,7 +558,7 @@ describe('$anchorScroll', function() {
545
558
} ) ,
546
559
addElements ( 'id=some' ) ,
547
560
changeHashTo ( 'some' ) ,
548
- expectScrollingWithOffset ( 'id=some' , 100 ) ) ) ;
561
+ expectAsync ( expectScrollingWithOffset ( 'id=some' , 100 ) ) ) ) ;
549
562
550
563
551
564
it ( 'should scroll without vertical offset when `position !== fixed`' , inject (
@@ -556,7 +569,7 @@ describe('$anchorScroll', function() {
556
569
} ) ,
557
570
addElements ( 'id=some' ) ,
558
571
changeHashTo ( 'some' ) ,
559
- expectScrollingWithoutOffset ( 'id=some' ) ) ) ;
572
+ expectAsync ( expectScrollingWithoutOffset ( 'id=some' ) ) ) ) ;
560
573
} ) ;
561
574
} ) ;
562
575
} ) ;
0 commit comments