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
}
@@ -234,7 +255,10 @@ describe('$anchorScroll', function() {
234
255
expectScrollingTo ( identifierCountMap ) ( $window ) ;
235
256
expect ( $window . scrollBy . calls . length ) . toBe ( list . length ) ;
236
257
forEach ( list , function ( offset , idx ) {
237
- expect ( $window . scrollBy . calls [ idx ] . args ) . toEqual ( [ 0 , - 1 * offset ] ) ;
258
+ // Due to sub-pixel rendering, there is a +/-1 error margin in the actual offset
259
+ var args = $window . scrollBy . calls [ idx ] . args ;
260
+ expect ( args [ 0 ] ) . toBe ( 0 ) ;
261
+ expect ( Math . abs ( offset + args [ 1 ] ) ) . toBeLessThan ( 1 ) ;
238
262
} ) ;
239
263
} ;
240
264
}
@@ -253,11 +277,17 @@ describe('$anchorScroll', function() {
253
277
}
254
278
255
279
function setYOffset ( yOffset ) {
256
- return function ( $anchorScroll ) {
280
+ return function ( $anchorScroll ) {
257
281
$anchorScroll . yOffset = yOffset ;
258
282
} ;
259
283
}
260
284
285
+ function updateMockPageYOffset ( ) {
286
+ return function ( $window ) {
287
+ $window . pageYOffset = window . pageYOffset ;
288
+ } ;
289
+ }
290
+
261
291
beforeEach ( inject ( setupBodyForOffsetTesting ( ) ) ) ;
262
292
263
293
afterEach ( inject ( function ( $document ) {
@@ -306,16 +336,20 @@ describe('$anchorScroll', function() {
306
336
'padding:0' ,
307
337
'' ] . join ( ';' ) ;
308
338
309
- forEach ( $window . document . body . children , function ( elem ) {
339
+ forEach ( $window . document . body . children , function ( elem ) {
310
340
elem . style . cssText = cssText ;
311
341
} ) ;
312
342
313
- // Make sure scrolling does actually take place (it is necessary for this test)
314
- forEach ( elmSpy , function ( spy , identifier ) {
343
+ // Make sure scrolling does actually take place
344
+ // (this is necessary for the current test)
345
+ forEach ( elmSpy , function ( spy , identifier ) {
315
346
elmSpy [ identifier ] = spy . andCallThrough ( ) ;
316
347
} ) ;
317
348
} ,
318
349
changeHashTo ( 'some2' ) ,
350
+ updateMockPageYOffset ( ) ,
351
+ resetAllSpies ( ) ,
352
+ callAnchorScroll ( ) ,
319
353
expectScrollingWithOffset ( 'id=some2' , targetAdjustedOffset ) ) ;
320
354
} ) ;
321
355
} ) ;
@@ -363,7 +397,7 @@ describe('$anchorScroll', function() {
363
397
364
398
var jqElem = jqLite ( '<div style="' + cssText + '"></div>' ) ;
365
399
366
- return function ( $anchorScroll , $window ) {
400
+ return function ( $anchorScroll , $window ) {
367
401
jqLite ( $window . document . body ) . append ( jqElem ) ;
368
402
$anchorScroll . yOffset = jqElem ;
369
403
} ;
0 commit comments