3
3
describe ( '$anchorScroll' , function ( ) {
4
4
5
5
var elmSpy ;
6
+ var windowSpies ;
6
7
7
8
function addElements ( ) {
8
9
var elements = sliceArgs ( arguments ) ;
9
10
10
11
return function ( $window ) {
11
12
forEach ( elements , function ( identifier ) {
12
- var match = identifier . match ( / ( \w * ) ? ( \w * ) = ( \w * ) / ) ,
13
- jqElm = jqLite ( '<' + ( match [ 1 ] || 'a ' ) + match [ 2 ] + '="' + match [ 3 ] + '"/>' ) ,
13
+ var match = identifier . match ( / (?: ( \w * ) ) ? ( \w * ) = ( \w * ) / ) ,
14
+ nodeName = match [ 1 ] || 'a' ,
15
+ tmpl = '<' + nodeName + ' ' + match [ 2 ] + '="' + match [ 3 ] + '">' +
16
+ match [ 3 ] + // add some content or else Firefox and IE place the element
17
+ // in weird ways that break yOffset-testing.
18
+ '</' + nodeName + '>' ,
19
+ jqElm = jqLite ( tmpl ) ,
14
20
elm = jqElm [ 0 ] ;
15
21
16
22
elmSpy [ identifier ] = spyOn ( elm , 'scrollIntoView' ) ;
@@ -20,7 +26,7 @@ describe('$anchorScroll', function() {
20
26
}
21
27
22
28
function callAnchorScroll ( ) {
23
- return function ( $anchorScroll ) {
29
+ return function ( $anchorScroll ) {
24
30
$anchorScroll ( ) ;
25
31
} ;
26
32
}
@@ -33,7 +39,7 @@ describe('$anchorScroll', function() {
33
39
}
34
40
35
41
function changeHashTo ( hash ) {
36
- return function ( $anchorScroll , $location , $rootScope ) {
42
+ return function ( $anchorScroll , $location , $rootScope ) {
37
43
$rootScope . $apply ( function ( ) {
38
44
$location . hash ( hash ) ;
39
45
} ) ;
@@ -73,14 +79,29 @@ describe('$anchorScroll', function() {
73
79
return expectScrollingTo ( NaN ) ;
74
80
}
75
81
82
+ function resetAllSpies ( ) {
83
+ function resetSpy ( spy ) {
84
+ spy . reset ( ) ;
85
+ }
86
+
87
+ return function ( $window ) {
88
+ forEach ( elmSpy , resetSpy ) ;
89
+ forEach ( windowSpies , resetSpy ) ;
90
+ } ;
91
+ }
92
+
76
93
77
94
beforeEach ( module ( function ( $provide ) {
78
95
elmSpy = { } ;
96
+ windowSpies = [ ] ;
97
+ var i = 0 ;
98
+
79
99
$provide . value ( '$window' , {
80
- scrollTo : jasmine . createSpy ( '$window.scrollTo' ) ,
81
- scrollBy : jasmine . createSpy ( '$window.scrollBy' ) ,
100
+ scrollTo : ( windowSpies [ i ++ ] = jasmine . createSpy ( '$window.scrollTo' ) ) ,
101
+ scrollBy : ( windowSpies [ i ++ ] = jasmine . createSpy ( '$window.scrollBy' ) ) ,
82
102
document : document ,
83
103
navigator : { } ,
104
+ pageYOffset : 0 ,
84
105
getComputedStyle : function ( elem ) {
85
106
return getComputedStyle ( elem ) ;
86
107
}
@@ -251,7 +272,10 @@ describe('$anchorScroll', function() {
251
272
expectScrollingTo ( identifierCountMap ) ( $window ) ;
252
273
expect ( $window . scrollBy . calls . length ) . toBe ( list . length ) ;
253
274
forEach ( list , function ( offset , idx ) {
254
- expect ( $window . scrollBy . calls [ idx ] . args ) . toEqual ( [ 0 , - 1 * offset ] ) ;
275
+ // Due to sub-pixel rendering, there is a +/-1 error margin in the actual offset
276
+ var args = $window . scrollBy . calls [ idx ] . args ;
277
+ expect ( args [ 0 ] ) . toBe ( 0 ) ;
278
+ expect ( Math . abs ( offset + args [ 1 ] ) ) . toBeLessThan ( 1 ) ;
255
279
} ) ;
256
280
} ;
257
281
}
@@ -270,11 +294,17 @@ describe('$anchorScroll', function() {
270
294
}
271
295
272
296
function setYOffset ( yOffset ) {
273
- return function ( $anchorScroll ) {
297
+ return function ( $anchorScroll ) {
274
298
$anchorScroll . yOffset = yOffset ;
275
299
} ;
276
300
}
277
301
302
+ function updateMockPageYOffset ( ) {
303
+ return function ( $window ) {
304
+ $window . pageYOffset = window . pageYOffset ;
305
+ } ;
306
+ }
307
+
278
308
beforeEach ( inject ( setupBodyForOffsetTesting ( ) ) ) ;
279
309
280
310
afterEach ( inject ( function ( $document ) {
@@ -323,16 +353,20 @@ describe('$anchorScroll', function() {
323
353
'padding:0' ,
324
354
'' ] . join ( ';' ) ;
325
355
326
- forEach ( $window . document . body . children , function ( elem ) {
356
+ forEach ( $window . document . body . children , function ( elem ) {
327
357
elem . style . cssText = cssText ;
328
358
} ) ;
329
359
330
- // Make sure scrolling does actually take place (it is necessary for this test)
331
- forEach ( elmSpy , function ( spy , identifier ) {
360
+ // Make sure scrolling does actually take place
361
+ // (this is necessary for the current test)
362
+ forEach ( elmSpy , function ( spy , identifier ) {
332
363
elmSpy [ identifier ] = spy . andCallThrough ( ) ;
333
364
} ) ;
334
365
} ,
335
366
changeHashTo ( 'some2' ) ,
367
+ updateMockPageYOffset ( ) ,
368
+ resetAllSpies ( ) ,
369
+ callAnchorScroll ( ) ,
336
370
expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ;
337
371
} ) ;
338
372
} ) ;
@@ -380,7 +414,7 @@ describe('$anchorScroll', function() {
380
414
381
415
var jqElem = jqLite ( '<div style="' + cssText + '"></div>' ) ;
382
416
383
- return function ( $anchorScroll , $window ) {
417
+ return function ( $anchorScroll , $window ) {
384
418
jqLite ( $window . document . body ) . append ( jqElem ) ;
385
419
$anchorScroll . yOffset = jqElem ;
386
420
} ;
0 commit comments