3
3
describe ( '$anchorScroll' , function ( ) {
4
4
5
5
var elmSpy ;
6
- var docSpies ;
6
+ var jqLiteSpies ;
7
7
var windowSpies ;
8
8
9
9
function addElements ( ) {
@@ -54,14 +54,11 @@ describe('$anchorScroll', function() {
54
54
55
55
function createMockDocument ( initialReadyState ) {
56
56
var mockedDoc = { } ;
57
- docSpies = { } ;
58
57
59
58
var propsToPassThrough = [ 'body' , 'documentElement' ] ;
60
59
var methodsToPassThrough = [
61
60
'getElementById' ,
62
- 'getElementsByName' ,
63
- 'addEventListener' ,
64
- 'removeEventListener'
61
+ 'getElementsByName'
65
62
] ;
66
63
67
64
var document_ = document ;
@@ -71,19 +68,9 @@ describe('$anchorScroll', function() {
71
68
} ) ;
72
69
methodsToPassThrough . forEach ( function ( method ) {
73
70
mockedDoc [ method ] = document_ [ method ] . bind ( document_ ) ;
74
- docSpies [ method ] = spyOn ( mockedDoc , method ) . andCallThrough ( ) ;
75
71
} ) ;
76
72
77
73
mockedDoc . readyState = initialReadyState || 'complete' ;
78
- mockedDoc . dispatchFakeReadyStateChangeEvent = function ( ) {
79
- var evt = document_ . createEvent ( 'Event' ) ;
80
- evt . initEvent ( 'readystatechange' , false , false ) ;
81
- document_ . dispatchEvent ( evt ) ;
82
- } ;
83
- mockedDoc . updateReadyState = function ( newState ) {
84
- this . readyState = newState ;
85
- this . dispatchFakeReadyStateChangeEvent ( ) ;
86
- } ;
87
74
88
75
return mockedDoc ;
89
76
}
@@ -103,16 +90,13 @@ describe('$anchorScroll', function() {
103
90
return getComputedStyle ( elem ) ;
104
91
} ,
105
92
addEventListener : function ( eventType , callback , unsupported ) {
106
- window . addEventListener ( eventType , callback , unsupported ) ;
93
+ addEventListener ( eventType , callback , unsupported ) ;
107
94
} ,
108
95
removeEventListener : function ( eventType , callback , unsupported ) {
109
- window . removeEventListener ( eventType , callback , unsupported ) ;
96
+ removeEventListener ( eventType , callback , unsupported ) ;
110
97
}
111
98
} ;
112
99
113
- windowSpies . addEventListener = spyOn ( mockedWin , 'addEventListener' ) . andCallThrough ( ) ;
114
- windowSpies . removeEventListener = spyOn ( mockedWin , 'removeEventListener' ) . andCallThrough ( ) ;
115
-
116
100
$provide . value ( '$window' , mockedWin ) ;
117
101
} ) ;
118
102
} ;
@@ -158,7 +142,7 @@ describe('$anchorScroll', function() {
158
142
159
143
return function ( $window ) {
160
144
forEach ( elmSpy , resetSpy ) ;
161
- forEach ( docSpies , resetSpy ) ;
145
+ forEach ( jqLiteSpies , resetSpy ) ;
162
146
forEach ( windowSpies , resetSpy ) ;
163
147
} ;
164
148
}
@@ -235,16 +219,18 @@ describe('$anchorScroll', function() {
235
219
} ;
236
220
}
237
221
238
-
239
222
function spyOnJQLiteOnOff ( ) {
223
+ jqLiteSpies = { } ;
224
+
240
225
return function ( ) {
241
- spyOn ( jqLite . prototype , 'on' ) . andCallThrough ( ) ;
242
- spyOn ( jqLite . prototype , 'off' ) . andCallThrough ( ) ;
226
+ jqLiteSpies . on = spyOn ( jqLite . prototype , 'on' ) . andCallThrough ( ) ;
227
+ jqLiteSpies . off = spyOn ( jqLite . prototype , 'off' ) . andCallThrough ( ) ;
243
228
} ;
244
229
}
245
230
246
231
function unspyOnJQLiteOnOff ( ) {
247
232
return function ( ) {
233
+ jqLiteSpies = { } ;
248
234
jqLite . prototype . on = jqLite . prototype . on . originalValue ;
249
235
jqLite . prototype . off = jqLite . prototype . off . originalValue ;
250
236
} ;
@@ -266,8 +252,8 @@ describe('$anchorScroll', function() {
266
252
}
267
253
} ) ;
268
254
269
- expect ( onCalls ) . toEqual ( callCount ) ;
270
- expect ( offCalls ) . toEqual ( callCount ) ;
255
+ expect ( onCalls ) . toBe ( callCount ) ;
256
+ expect ( offCalls ) . toBe ( callCount ) ;
271
257
} ;
272
258
}
273
259
@@ -320,17 +306,19 @@ describe('$anchorScroll', function() {
320
306
} ) ;
321
307
322
308
323
- it ( 'should scroll immediately if already `readyState === "complete"`' , inject (
324
- addElements ( 'id=some1' ) ,
309
+ it ( 'should scroll immediately if already `readyState === "complete"`' , function ( ) {
310
+ module ( spyOnJQLiteOnOff ( ) ) ;
311
+ inject (
312
+ addElements ( 'id=some1' ) ,
325
313
326
- triggerLoadEvent ( ) ,
327
- changeHashTo ( 'some1' ) ,
314
+ triggerLoadEvent ( ) ,
315
+ changeHashTo ( 'some1' ) ,
328
316
329
- expectScrollingTo ( 'id=some1' ) ,
330
- function ( ) {
331
- expect ( windowSpies . addEventListener . callCount ) . toBe ( 0 ) ;
332
- expect ( windowSpies . removeEventListener . callCount ) . toBe ( 0 ) ;
333
- } ) ) ;
317
+ expectScrollingTo ( 'id=some1' ) ,
318
+ expectJQLiteOnOffCallsToEqual ( 0 ) ,
319
+ unspyOnJQLiteOnOff ( )
320
+ ) ;
321
+ } ) ;
334
322
} ) ;
335
323
336
324
0 commit comments