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

Commit 4e0e4f1

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 a27f05d commit 4e0e4f1

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() {
@@ -301,7 +311,7 @@ describe('$anchorScroll', function() {
301311
inject(
302312
addElements('id=some'),
303313
changeHashTo('some'),
304-
expectScrollingTo('id=some')
314+
expectAsync(expectScrollingTo('id=some'))
305315
);
306316
});
307317

@@ -311,7 +321,7 @@ describe('$anchorScroll', function() {
311321
inject(
312322
addElements('id=some'),
313323
changeHashTo('some'),
314-
expectScrollingTo('id=some')
324+
expectAsync(expectScrollingTo('id=some'))
315325
);
316326
});
317327

@@ -321,7 +331,7 @@ describe('$anchorScroll', function() {
321331
inject(
322332
addElements('id=some'),
323333
changeHashTo('other'),
324-
expectNoScrolling()
334+
expectAsync(expectNoScrolling())
325335
);
326336
});
327337

@@ -331,7 +341,7 @@ describe('$anchorScroll', function() {
331341
inject(
332342
addElements('id=some'),
333343
changeHashTo('some'),
334-
expectScrollingTo('id=some')
344+
expectAsync(expectScrollingTo('id=some'))
335345
);
336346
});
337347

@@ -344,7 +354,7 @@ describe('$anchorScroll', function() {
344354
inject(
345355
addElements('id=fake'),
346356
changeHashTo('fake'),
347-
expectNoScrolling()
357+
expectAsync(expectNoScrolling())
348358
);
349359
});
350360

@@ -357,7 +367,7 @@ describe('$anchorScroll', function() {
357367
inject(
358368
addElements('id=fake'),
359369
changeHashTo('fake'),
360-
expectNoScrolling(),
370+
expectAsync(expectNoScrolling()),
361371
callAnchorScroll(),
362372
expectScrollingTo('id=fake')
363373
);
@@ -372,7 +382,7 @@ describe('$anchorScroll', function() {
372382
function expectScrollingWithOffset(identifierCountMap, offsetList) {
373383
var list = isArray(offsetList) ? offsetList : [offsetList];
374384

375-
return function($window) {
385+
return function($rootScope, $window) {
376386
inject(expectScrollingTo(identifierCountMap));
377387
expect($window.scrollBy.callCount).toBe(list.length);
378388
forEach(list, function(offset, idx) {
@@ -420,6 +430,7 @@ describe('$anchorScroll', function() {
420430

421431
var yOffsetNumber = 50;
422432

433+
423434
beforeEach(inject(
424435
setupBodyForOffsetTesting(),
425436
setYOffset(yOffsetNumber)));
@@ -428,15 +439,15 @@ describe('$anchorScroll', function() {
428439
it('should scroll with vertical offset', inject(
429440
addElements('id=some'),
430441
changeHashTo('some'),
431-
expectScrollingWithOffset('id=some', yOffsetNumber)));
442+
expectAsync(expectScrollingWithOffset('id=some', yOffsetNumber))));
432443

433444

434445
it('should use the correct vertical offset when changing `yOffset` at runtime', inject(
435446
addElements('id=some'),
436447
changeHashTo('some'),
437448
setYOffset(yOffsetNumber - 10),
438449
callAnchorScroll(),
439-
expectScrollingWithOffset({'id=some': 2}, [yOffsetNumber, yOffsetNumber - 10])));
450+
expectAsync(expectScrollingWithOffset({'id=some': 2}, [yOffsetNumber, yOffsetNumber - 10]))));
440451

441452

442453
it('should adjust the vertical offset for elements near the end of the page', function() {
@@ -469,9 +480,10 @@ describe('$anchorScroll', function() {
469480
},
470481
changeHashTo('some2'),
471482
updateMockPageYOffset(),
472-
resetAllSpies(),
473-
callAnchorScroll(),
474-
expectScrollingWithOffset('id=some2', targetAdjustedOffset));
483+
expectAsync(
484+
resetAllSpies(),
485+
callAnchorScroll(),
486+
expectScrollingWithOffset('id=some2', targetAdjustedOffset)));
475487
});
476488
});
477489

@@ -495,15 +507,16 @@ describe('$anchorScroll', function() {
495507
changeHashTo('name2'),
496508
changeHashTo('id1'),
497509
callAnchorScroll(),
498-
expectScrollingWithOffset({
499-
'id=id1': 3,
500-
'name=name2': 1
501-
}, [
502-
1 * increment,
503-
2 * increment,
504-
3 * increment,
505-
4 * increment
506-
]));
510+
expectAsync(
511+
expectScrollingWithOffset({
512+
'id=id1': 3,
513+
'name=name2': 1
514+
}, [
515+
1 * increment,
516+
2 * increment,
517+
3 * increment,
518+
4 * increment
519+
])));
507520
});
508521
});
509522

@@ -534,7 +547,7 @@ describe('$anchorScroll', function() {
534547
}),
535548
addElements('id=some'),
536549
changeHashTo('some'),
537-
expectScrollingWithOffset('id=some', 50)));
550+
expectAsync(expectScrollingWithOffset('id=some', 50))));
538551

539552

540553
it('should scroll with vertical offset when `top > 0`', inject(
@@ -545,7 +558,7 @@ describe('$anchorScroll', function() {
545558
}),
546559
addElements('id=some'),
547560
changeHashTo('some'),
548-
expectScrollingWithOffset('id=some', 100)));
561+
expectAsync(expectScrollingWithOffset('id=some', 100))));
549562

550563

551564
it('should scroll without vertical offset when `position !== fixed`', inject(
@@ -556,7 +569,7 @@ describe('$anchorScroll', function() {
556569
}),
557570
addElements('id=some'),
558571
changeHashTo('some'),
559-
expectScrollingWithoutOffset('id=some')));
572+
expectAsync(expectScrollingWithoutOffset('id=some'))));
560573
});
561574
});
562575
});

0 commit comments

Comments
 (0)