Skip to content

Commit 41f6e7e

Browse files
gkalpakpetebacondarwin
authored andcommitted
test($anchorScroll): refactored tests to make them more "unit"-like
Mock `getBoundingClientRect()` method of elements in tests, instead of relying on the browser to actually size and scroll the elements.
1 parent 73c44c8 commit 41f6e7e

File tree

1 file changed

+41
-94
lines changed

1 file changed

+41
-94
lines changed

test/ng/anchorScrollSpec.js

Lines changed: 41 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -417,46 +417,18 @@ describe('$anchorScroll', function() {
417417
return expectScrollingWithOffset(identifierCountMap, []);
418418
}
419419

420-
function setupBodyForOffsetTesting(styles) {
421-
styles = styles || {};
422-
423-
return function($window) {
424-
var style = $window.document.body.style;
425-
style.border = styles.border || 'none';
426-
style.margin = styles.margin || '0';
427-
style.padding = styles.padding || '0';
428-
style.boxSizing = styles.boxSizing || null;
429-
};
430-
}
431-
432-
function setupForNearEndOfPageTesting(borderWidth, marginWidth, paddingWidth,
433-
yOffset, necessaryYOffset) {
420+
function mockBoundingClientRect(childValuesMap) {
434421
return function($window) {
435-
// In order to make sure that when scrolling all the way down, the element's top can't reach
436-
// the top of the viewport, the element's height is calculated as follows:
437-
// 1. take the viewport's height
438-
// 2. substract the height of the bottom border/margin/padding
439-
// 3. substract the distance we want to have from the viewport's top
440-
// (yOffset - necessaryYOffset)
441-
var viewportHeight = $window.document.documentElement.clientHeight;
442-
var elemHeight = viewportHeight - (borderWidth + marginWidth + paddingWidth +
443-
yOffset - necessaryYOffset);
444-
var cssText = [
445-
'border:none',
446-
'display:block',
447-
'height:' + elemHeight + 'px',
448-
'margin:0',
449-
'padding:0',
450-
''].join(';');
451-
452-
forEach($window.document.body.children, function(elem) {
453-
elem.style.cssText = cssText;
454-
});
455-
456-
// Make sure scrolling does actually take place
457-
// (this is necessary for the current test)
458-
forEach(elmSpy, function(spy, identifier) {
459-
elmSpy[identifier] = spy.andCallThrough();
422+
var children = $window.document.body.children;
423+
forEach(childValuesMap, function(valuesList, childIdx) {
424+
var elem = children[childIdx];
425+
elem.getBoundingClientRect = function() {
426+
var val = valuesList.shift();
427+
return {
428+
top: val,
429+
bottom: val
430+
};
431+
};
460432
});
461433
};
462434
}
@@ -472,9 +444,6 @@ describe('$anchorScroll', function() {
472444

473445
describe('and body with no border/margin/padding', function() {
474446

475-
beforeEach(inject(setupBodyForOffsetTesting()));
476-
477-
478447
describe('when set as a fixed number', function() {
479448

480449
var yOffsetNumber = 50;
@@ -484,12 +453,14 @@ describe('$anchorScroll', function() {
484453

485454
it('should scroll with vertical offset', inject(
486455
addElements('id=some'),
456+
mockBoundingClientRect({0: [0]}),
487457
changeHashTo('some'),
488458
expectScrollingWithOffset('id=some', yOffsetNumber)));
489459

490460

491461
it('should use the correct vertical offset when changing `yOffset` at runtime', inject(
492462
addElements('id=some'),
463+
mockBoundingClientRect({0: [0, 0]}),
493464
changeHashTo('some'),
494465
setYOffset(yOffsetNumber - 10),
495466
callAnchorScroll(),
@@ -502,7 +473,7 @@ describe('$anchorScroll', function() {
502473

503474
inject(
504475
addElements('id=some1', 'id=some2'),
505-
setupForNearEndOfPageTesting(0, 0, 0, yOffsetNumber, targetAdjustedOffset),
476+
mockBoundingClientRect({1: [yOffsetNumber - targetAdjustedOffset]}),
506477
changeHashTo('some2'),
507478
expectScrollingWithOffset('id=some2', targetAdjustedOffset));
508479
});
@@ -523,6 +494,10 @@ describe('$anchorScroll', function() {
523494

524495
inject(
525496
addElements('id=id1', 'name=name2'),
497+
mockBoundingClientRect({
498+
0: [0, 0, 0],
499+
1: [0]
500+
}),
526501
setYOffset(yOffsetFunction),
527502
changeHashTo('id1'),
528503
changeHashTo('name2'),
@@ -543,13 +518,11 @@ describe('$anchorScroll', function() {
543518

544519
describe('when set as a jqLite element', function() {
545520

546-
function createAndSetYOffsetElement(styleSpecs) {
547-
var cssText = '';
548-
forEach(styleSpecs, function(value, key) {
549-
cssText += key + ':' + value + ';';
550-
});
521+
var elemBottom = 50;
551522

552-
var jqElem = jqLite('<div style="' + cssText + '"></div>');
523+
function createAndSetYOffsetElement(position) {
524+
var jqElem = jqLite('<div></div>');
525+
jqElem[0].style.position = position;
553526

554527
return function($anchorScroll, $window) {
555528
jqLite($window.document.body).append(jqElem);
@@ -558,37 +531,16 @@ describe('$anchorScroll', function() {
558531
}
559532

560533

561-
it('should scroll with vertical offset when `top === 0`', inject(
562-
createAndSetYOffsetElement({
563-
background: 'DarkOrchid',
564-
height: '50px',
565-
position: 'fixed',
566-
top: '0',
567-
}),
568-
addElements('id=some'),
569-
changeHashTo('some'),
570-
expectScrollingWithOffset('id=some', 50)));
571-
572-
573-
it('should scroll with vertical offset when `top > 0`', inject(
574-
createAndSetYOffsetElement({
575-
height: '50px',
576-
position: 'fixed',
577-
top: '50px',
578-
}),
534+
it('should scroll with vertical offset when `position === fixed`', inject(
535+
createAndSetYOffsetElement('fixed'),
579536
addElements('id=some'),
537+
mockBoundingClientRect({0: [elemBottom], 1: [0]}),
580538
changeHashTo('some'),
581-
expectScrollingWithOffset('id=some', 100)));
539+
expectScrollingWithOffset('id=some', elemBottom)));
582540

583541

584542
it('should scroll without vertical offset when `position !== fixed`', inject(
585-
createAndSetYOffsetElement({
586-
height: '50px',
587-
position: 'absolute',
588-
top: '0',
589-
}),
590-
addElements('id=some'),
591-
changeHashTo('some'),
543+
createAndSetYOffsetElement('absolute', elemBottom),
592544
expectScrollingWithoutOffset('id=some')));
593545
});
594546
});
@@ -602,23 +554,22 @@ describe('$anchorScroll', function() {
602554
var yOffsetNumber = 50;
603555
var necessaryYOffset = yOffsetNumber - borderWidth - marginWidth - paddingWidth;
604556

605-
beforeEach(inject(
606-
setupBodyForOffsetTesting({
607-
border: borderWidth + 'px solid',
608-
margin: marginWidth + 'px',
609-
padding: paddingWidth + 'px'
610-
}),
611-
setYOffset(yOffsetNumber)));
557+
beforeEach(inject(setYOffset(yOffsetNumber)));
612558

613559

614560
it('should scroll with vertical offset', inject(
615561
addElements('id=some'),
562+
mockBoundingClientRect({0: [yOffsetNumber - necessaryYOffset]}),
616563
changeHashTo('some'),
617564
expectScrollingWithOffset('id=some', necessaryYOffset)));
618565

619566

620567
it('should use the correct vertical offset when changing `yOffset` at runtime', inject(
621568
addElements('id=some'),
569+
mockBoundingClientRect({0: [
570+
yOffsetNumber - necessaryYOffset,
571+
yOffsetNumber - necessaryYOffset
572+
]}),
622573
changeHashTo('some'),
623574
setYOffset(yOffsetNumber - 10),
624575
callAnchorScroll(),
@@ -631,8 +582,7 @@ describe('$anchorScroll', function() {
631582

632583
inject(
633584
addElements('id=some1', 'id=some2'),
634-
setupForNearEndOfPageTesting(borderWidth, marginWidth, paddingWidth,
635-
yOffsetNumber, targetAdjustedOffset),
585+
mockBoundingClientRect({1: [yOffsetNumber - targetAdjustedOffset]}),
636586
changeHashTo('some2'),
637587
expectScrollingWithOffset('id=some2', targetAdjustedOffset));
638588
});
@@ -647,24 +597,22 @@ describe('$anchorScroll', function() {
647597
var yOffsetNumber = 50;
648598
var necessaryYOffset = yOffsetNumber - borderWidth - marginWidth - paddingWidth;
649599

650-
beforeEach(inject(
651-
setupBodyForOffsetTesting({
652-
border: borderWidth + 'px solid',
653-
margin: marginWidth + 'px',
654-
padding: paddingWidth + 'px',
655-
boxSizing: 'border-box'
656-
}),
657-
setYOffset(yOffsetNumber)));
600+
beforeEach(inject(setYOffset(yOffsetNumber)));
658601

659602

660603
it('should scroll with vertical offset', inject(
661604
addElements('id=some'),
605+
mockBoundingClientRect({0: [yOffsetNumber - necessaryYOffset]}),
662606
changeHashTo('some'),
663607
expectScrollingWithOffset('id=some', necessaryYOffset)));
664608

665609

666610
it('should use the correct vertical offset when changing `yOffset` at runtime', inject(
667611
addElements('id=some'),
612+
mockBoundingClientRect({0: [
613+
yOffsetNumber - necessaryYOffset,
614+
yOffsetNumber - necessaryYOffset
615+
]}),
668616
changeHashTo('some'),
669617
setYOffset(yOffsetNumber - 10),
670618
callAnchorScroll(),
@@ -677,8 +625,7 @@ describe('$anchorScroll', function() {
677625

678626
inject(
679627
addElements('id=some1', 'id=some2'),
680-
setupForNearEndOfPageTesting(borderWidth, marginWidth, paddingWidth,
681-
yOffsetNumber, targetAdjustedOffset),
628+
mockBoundingClientRect({1: [yOffsetNumber - targetAdjustedOffset]}),
682629
changeHashTo('some2'),
683630
expectScrollingWithOffset('id=some2', targetAdjustedOffset));
684631
});

0 commit comments

Comments
 (0)