Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3e7c1ba

Browse files
committed
test($anchorScroll): fix tests in order to reliably pass on Firefox
There was an timing issue (that seemed to only affect Firefox) due to `scroll()` being wrapped in `$evalAsync()` (this is necessary to ensure that Firefox does not overwrite the `yOffset` when navigating to a new page). This commit wraps the some expectations in `$evalAsync()` in order to evaluate them after `scroll()` has been called.
1 parent f9018ab commit 3e7c1ba

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

test/ng/anchorScrollSpec.js

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ describe('$anchorScroll', function() {
4444
$rootScope.$apply(function() {
4545
$location.hash(hash);
4646
});
47-
if ($browser.deferredFns.length) {
48-
$browser.defer.flush();
49-
}
5047
};
5148
}
5249

53-
function expectScrollingToTop($window) {
54-
forEach(elmSpy, function(spy, id) {
55-
expect(spy).not.toHaveBeenCalled();
56-
});
50+
function expectAsync() {
51+
var expectations = sliceArgs(arguments);
5752

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+
};
5960
}
6061

6162
function expectScrollingTo(identifierCountMap) {
@@ -79,6 +80,14 @@ describe('$anchorScroll', function() {
7980
};
8081
}
8182

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+
8291
function expectNoScrolling() {
8392
return expectScrollingTo(NaN);
8493
}
@@ -212,13 +221,13 @@ describe('$anchorScroll', function() {
212221
addElements('id=some1'),
213222

214223
changeHashTo('some1'),
215-
expectNoScrolling(),
224+
expectAsync(expectNoScrolling()),
216225

217226
updateMockReadyState('some-arbitrary-state'),
218-
expectNoScrolling(),
227+
expectAsync(expectNoScrolling()),
219228

220229
updateMockReadyState('complete'),
221-
expectScrollingTo('id=some1')));
230+
expectAsync(expectScrollingTo('id=some1'))));
222231

223232

224233
it('should only register once for execution when `document.readyState === "complete"', inject(
@@ -230,10 +239,10 @@ describe('$anchorScroll', function() {
230239
updateMockReadyState('some-other-arbitrary-state'),
231240
changeHashTo('some1'),
232241
changeHashTo('some2'),
233-
expectNoScrolling(),
242+
expectAsync(expectNoScrolling()),
234243

235244
updateMockReadyState('complete'),
236-
expectScrollingTo('id=some2')));
245+
expectAsync(expectScrollingTo('id=some2'))));
237246

238247

239248
it('should properly register and unregister listeners for `readystatechange` event', inject(
@@ -247,7 +256,7 @@ describe('$anchorScroll', function() {
247256
changeHashTo('some2'),
248257
updateMockReadyState('complete'),
249258

250-
function() {
259+
expectAsync(function() {
251260
expect(docSpies.addEventListener.callCount).toBe(1);
252261
expect(docSpies.addEventListener).
253262
toHaveBeenCalledWith('readystatechange', jasmine.any(Function));
@@ -259,7 +268,7 @@ describe('$anchorScroll', function() {
259268
var registeredListener = docSpies.addEventListener.calls[0].args[1];
260269
var unregisteredListener = docSpies.removeEventListener.calls[0].args[1];
261270
expect(unregisteredListener).toBe(registeredListener);
262-
}));
271+
})));
263272

264273

265274
it('should scroll immediately if `readyState === "complete"`', inject(
@@ -269,11 +278,12 @@ describe('$anchorScroll', function() {
269278
updateMockReadyState('complete'),
270279
changeHashTo('some1'),
271280

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+
})));
277287

278288

279289
describe('watcher', function() {
@@ -308,7 +318,7 @@ describe('$anchorScroll', function() {
308318
initAnchorScroll(),
309319
addElements('id=some'),
310320
changeHashTo('some'),
311-
expectScrollingTo('id=some')
321+
expectAsync(expectScrollingTo('id=some'))
312322
);
313323
});
314324

@@ -319,7 +329,7 @@ describe('$anchorScroll', function() {
319329
initAnchorScroll(),
320330
addElements('id=some'),
321331
changeHashTo('some'),
322-
expectScrollingTo('id=some')
332+
expectAsync(expectScrollingTo('id=some'))
323333
);
324334
});
325335

@@ -337,7 +347,7 @@ describe('$anchorScroll', function() {
337347
initAnchorScroll(),
338348
addElements('id=some'),
339349
changeHashTo('other'),
340-
expectNoScrolling()
350+
expectAsync(expectNoScrolling())
341351
);
342352
});
343353

@@ -348,7 +358,7 @@ describe('$anchorScroll', function() {
348358
initAnchorScroll(),
349359
addElements('id=some'),
350360
changeHashTo('some'),
351-
expectScrollingTo('id=some')
361+
expectAsync(expectScrollingTo('id=some'))
352362
);
353363
});
354364

@@ -361,7 +371,7 @@ describe('$anchorScroll', function() {
361371
inject(
362372
addElements('id=fake'),
363373
changeHashTo('fake'),
364-
expectNoScrolling()
374+
expectAsync(expectNoScrolling())
365375
);
366376
});
367377

@@ -374,7 +384,7 @@ describe('$anchorScroll', function() {
374384
inject(
375385
addElements('id=fake'),
376386
changeHashTo('fake'),
377-
expectNoScrolling(),
387+
expectAsync(expectNoScrolling()),
378388
callAnchorScroll(),
379389
expectScrollingTo('id=fake')
380390
);
@@ -389,7 +399,7 @@ describe('$anchorScroll', function() {
389399
function expectScrollingWithOffset(identifierCountMap, offsetList) {
390400
var list = isArray(offsetList) ? offsetList : [offsetList];
391401

392-
return function($window) {
402+
return function($rootScope, $window) {
393403
inject(expectScrollingTo(identifierCountMap));
394404
expect($window.scrollBy.callCount).toBe(list.length);
395405
forEach(list, function(offset, idx) {
@@ -437,6 +447,7 @@ describe('$anchorScroll', function() {
437447

438448
var yOffsetNumber = 50;
439449

450+
440451
beforeEach(inject(
441452
setupBodyForOffsetTesting(),
442453
setYOffset(yOffsetNumber)));
@@ -445,15 +456,15 @@ describe('$anchorScroll', function() {
445456
it('should scroll with vertical offset', inject(
446457
addElements('id=some'),
447458
changeHashTo('some'),
448-
expectScrollingWithOffset('id=some', yOffsetNumber)));
459+
expectAsync(expectScrollingWithOffset('id=some', yOffsetNumber))));
449460

450461

451462
it('should use the correct vertical offset when changing `yOffset` at runtime', inject(
452463
addElements('id=some'),
453464
changeHashTo('some'),
454465
setYOffset(yOffsetNumber - 10),
455466
callAnchorScroll(),
456-
expectScrollingWithOffset({'id=some': 2}, [yOffsetNumber, yOffsetNumber - 10])));
467+
expectAsync(expectScrollingWithOffset({'id=some': 2}, [yOffsetNumber, yOffsetNumber - 10]))));
457468

458469

459470
it('should adjust the vertical offset for elements near the end of the page', function() {
@@ -486,9 +497,10 @@ describe('$anchorScroll', function() {
486497
},
487498
changeHashTo('some2'),
488499
updateMockPageYOffset(),
489-
resetAllSpies(),
490-
callAnchorScroll(),
491-
expectScrollingWithOffset('id=some2', targetAdjustedOffset));
500+
expectAsync(
501+
resetAllSpies(),
502+
callAnchorScroll(),
503+
expectScrollingWithOffset('id=some2', targetAdjustedOffset)));
492504
});
493505
});
494506

@@ -512,15 +524,16 @@ describe('$anchorScroll', function() {
512524
changeHashTo('name2'),
513525
changeHashTo('id1'),
514526
callAnchorScroll(),
515-
expectScrollingWithOffset({
516-
'id=id1': 3,
517-
'name=name2': 1
518-
}, [
519-
1 * increment,
520-
2 * increment,
521-
3 * increment,
522-
4 * increment
523-
]));
527+
expectAsync(
528+
expectScrollingWithOffset({
529+
'id=id1': 3,
530+
'name=name2': 1
531+
}, [
532+
1 * increment,
533+
2 * increment,
534+
3 * increment,
535+
4 * increment
536+
])));
524537
});
525538
});
526539

@@ -551,7 +564,7 @@ describe('$anchorScroll', function() {
551564
}),
552565
addElements('id=some'),
553566
changeHashTo('some'),
554-
expectScrollingWithOffset('id=some', 50)));
567+
expectAsync(expectScrollingWithOffset('id=some', 50))));
555568

556569

557570
it('should scroll with vertical offset when `top > 0`', inject(
@@ -562,7 +575,7 @@ describe('$anchorScroll', function() {
562575
}),
563576
addElements('id=some'),
564577
changeHashTo('some'),
565-
expectScrollingWithOffset('id=some', 100)));
578+
expectAsync(expectScrollingWithOffset('id=some', 100))));
566579

567580

568581
it('should scroll without vertical offset when `position !== fixed`', inject(
@@ -573,7 +586,7 @@ describe('$anchorScroll', function() {
573586
}),
574587
addElements('id=some'),
575588
changeHashTo('some'),
576-
expectScrollingWithoutOffset('id=some')));
589+
expectAsync(expectScrollingWithoutOffset('id=some'))));
577590
});
578591
});
579592
});

0 commit comments

Comments
 (0)