@@ -235,6 +235,47 @@ describe('$anchorScroll', function() {
235
235
} ;
236
236
}
237
237
238
+
239
+ function spyOnJQLiteOnOff ( ) {
240
+ return function ( ) {
241
+ spyOn ( jqLite . prototype , 'on' ) . andCallThrough ( ) ;
242
+ spyOn ( jqLite . prototype , 'off' ) . andCallThrough ( ) ;
243
+ } ;
244
+ }
245
+
246
+ function unspyOnJQLiteOnOff ( ) {
247
+ return function ( ) {
248
+ jqLite . prototype . on = jqLite . prototype . on . originalValue ;
249
+ jqLite . prototype . off = jqLite . prototype . off . originalValue ;
250
+ } ;
251
+ }
252
+
253
+ function expectJQLiteOnOffCallsToEqual ( callCount ) {
254
+ return function ( ) {
255
+ var onCalls = 0 , offCalls = 0 ;
256
+
257
+ forEach ( jqLite . prototype . on . calls , function ( call ) {
258
+ if ( call . args [ 0 ] === 'load' ) {
259
+ onCalls += 1 ;
260
+ }
261
+ } ) ;
262
+
263
+ forEach ( jqLite . prototype . off . calls , function ( call ) {
264
+ if ( call . args [ 0 ] === 'load' ) {
265
+ offCalls += 1 ;
266
+ }
267
+ } ) ;
268
+ } ;
269
+ }
270
+
271
+ function expectJQLiteOnOffCallsToHaveSameHandler ( ) {
272
+ return function ( ) {
273
+ var registeredListener = jqLite . prototype . on . mostRecentCall . args [ 1 ] ;
274
+ var unregisteredListener = jqLite . prototype . off . mostRecentCall . args [ 1 ] ;
275
+ expect ( unregisteredListener ) . toBe ( registeredListener ) ;
276
+ } ;
277
+ }
278
+
238
279
beforeEach ( createMockWindow ( 'interactive' ) ) ;
239
280
240
281
@@ -259,26 +300,21 @@ describe('$anchorScroll', function() {
259
300
expectScrollingTo ( 'id=some2' ) ) ) ;
260
301
261
302
262
- it ( 'should properly register and unregister listeners for the `load` event' , inject (
263
- addElements ( 'id=some1' , 'id=some2' ) ,
264
-
265
- changeHashTo ( 'some1' ) ,
266
- changeHashTo ( 'some2' ) ,
267
- triggerLoadEvent ( ) ,
303
+ it ( 'should properly register and unregister listeners for the `load` event' , function ( ) {
304
+ module ( spyOnJQLiteOnOff ( ) ) ;
305
+ inject (
306
+ addElements ( 'id=some1' , 'id=some2' ) ,
268
307
269
- function ( ) {
270
- expect ( windowSpies . addEventListener . callCount ) . toBe ( 1 ) ;
271
- expect ( windowSpies . addEventListener ) .
272
- toHaveBeenCalledWith ( 'load' , jasmine . any ( Function ) , false ) ;
308
+ changeHashTo ( 'some1' ) ,
309
+ changeHashTo ( 'some2' ) ,
273
310
274
- expect ( windowSpies . removeEventListener . callCount ) . toBe ( 1 ) ;
275
- expect ( windowSpies . removeEventListener ) .
276
- toHaveBeenCalledWith ( 'load' , jasmine . any ( Function ) , false ) ;
311
+ triggerLoadEvent ( ) ,
277
312
278
- var registeredListener = windowSpies . addEventListener . calls [ 0 ] . args [ 1 ] ;
279
- var unregisteredListener = windowSpies . removeEventListener . calls [ 0 ] . args [ 1 ] ;
280
- expect ( unregisteredListener ) . toBe ( registeredListener ) ;
281
- } ) ) ;
313
+ expectJQLiteOnOffCallsToEqual ( 1 ) ,
314
+ expectJQLiteOnOffCallsToHaveSameHandler ( ) ,
315
+ unspyOnJQLiteOnOff ( )
316
+ ) ;
317
+ } ) ;
282
318
283
319
284
320
it ( 'should scroll immediately if already `readyState === "complete"`' , inject (
0 commit comments