|
| 1 | +describe('$anchorScroll', function() { |
| 2 | + describe('basic functionality', function() { |
| 3 | + beforeEach(function() { |
| 4 | + loadFixture('anchor-scroll'); |
| 5 | + }); |
| 6 | + |
| 7 | + it('should scroll to #bottom when clicking #top', function() { |
| 8 | + scrollToTop(); |
| 9 | + expectVisible('top', true); |
| 10 | + expectVisible('bottom', false); |
| 11 | + |
| 12 | + element(by.id('top')).click(); |
| 13 | + expectVisible('top', false); |
| 14 | + expectVisible('bottom', true); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should scroll to #top when clicking #bottom', function() { |
| 18 | + element(by.id('top')).click(); |
| 19 | + expectVisible('top', false); |
| 20 | + expectVisible('bottom', true); |
| 21 | + |
| 22 | + element(by.id('bottom')).click(); |
| 23 | + expectVisible('top', true); |
| 24 | + expectVisible('bottom', false); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('with `yOffset`', function() { |
| 29 | + var yOffset = 50; |
| 30 | + |
| 31 | + beforeEach(function() { |
| 32 | + loadFixture('anchor-scroll-y-offset'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should scroll to the correct anchor when clicking each button', function() { |
| 36 | + var buttons = element.all(by.repeater('x in [1, 2, 3, 4, 5]')); |
| 37 | + var lastAnchor = element.all(by.repeater('y in [1, 2, 3, 4, 5]')).last(); |
| 38 | + |
| 39 | + // Make sure there is enough room to scroll the last anchor to the top |
| 40 | + lastAnchor.getSize().then(function(size) { |
| 41 | + var tempHeight = size.height - 10; |
| 42 | + |
| 43 | + execWithTempViewportHeight(tempHeight, function() { |
| 44 | + buttons.each(function(button, idx) { |
| 45 | + // For whatever reason, we need to run the assertions in the callback :( |
| 46 | + button.click().then(function() { |
| 47 | + var targetAnchorId = 'anchor-' + (idx + 1); |
| 48 | + |
| 49 | + expectVisible(targetAnchorId, true); |
| 50 | + expectTop(targetAnchorId, yOffset); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should automatically scroll when navigating to a URL with a hash', function() { |
| 58 | + var lastAnchor = element.all(by.repeater('y in [1, 2, 3, 4, 5]')).last(); |
| 59 | + var lastAnchorId = 'anchor-5'; |
| 60 | + |
| 61 | + // Make sure there is enough room to scroll the last anchor to the top |
| 62 | + lastAnchor.getSize().then(function(size) { |
| 63 | + var tempHeight = size.height - 10; |
| 64 | + |
| 65 | + execWithTempViewportHeight(tempHeight, function() { |
| 66 | + // Test updating `$location.url()` from within the app |
| 67 | + expectVisible(lastAnchorId, false); |
| 68 | + |
| 69 | + browser.setLocation('#' + lastAnchorId); |
| 70 | + expectVisible(lastAnchorId, true); |
| 71 | + expectTop(lastAnchorId, yOffset); |
| 72 | + |
| 73 | + // Test navigating to the URL directly |
| 74 | + scrollToTop(); |
| 75 | + expectVisible(lastAnchorId, false); |
| 76 | + |
| 77 | + browser.refresh(); |
| 78 | + expectVisible(lastAnchorId, true); |
| 79 | + expectTop(lastAnchorId, yOffset); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should not scroll "overzealously"', function () { |
| 85 | + var lastButton = element.all(by.repeater('x in [1, 2, 3, 4, 5]')).last(); |
| 86 | + var lastAnchor = element.all(by.repeater('y in [1, 2, 3, 4, 5]')).last(); |
| 87 | + var lastAnchorId = 'anchor-5'; |
| 88 | + |
| 89 | + // Make sure there is not enough room to scroll the last anchor to the top |
| 90 | + lastAnchor.getSize().then(function(size) { |
| 91 | + var tempHeight = size.height + (yOffset / 2); |
| 92 | + |
| 93 | + execWithTempViewportHeight(tempHeight, function() { |
| 94 | + scrollIntoView(lastAnchorId); |
| 95 | + expectTop(lastAnchorId, yOffset / 2); |
| 96 | + |
| 97 | + lastButton.click(); |
| 98 | + expectVisible(lastAnchorId, true); |
| 99 | + expectTop(lastAnchorId, yOffset); |
| 100 | + }); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + // Helpers |
| 106 | + function _scriptGetElemTop(id) { |
| 107 | + var elem = document.getElementById(id); |
| 108 | + var rect = elem.getBoundingClientRect(); |
| 109 | + |
| 110 | + return rect.top; |
| 111 | + } |
| 112 | + |
| 113 | + function _scriptIsElemVisible(id) { |
| 114 | + var elem = document.getElementById(id); |
| 115 | + var rect = elem.getBoundingClientRect(); |
| 116 | + var docElem = document.documentElement; |
| 117 | + |
| 118 | + return (rect.top < docElem.clientHeight) && |
| 119 | + (rect.bottom > 0) && |
| 120 | + (rect.left < docElem.clientWidth) && |
| 121 | + (rect.right > 0); |
| 122 | + } |
| 123 | + |
| 124 | + function execWithTempViewportHeight(tempHeight, fn) { |
| 125 | + setViewportHeight(tempHeight).then(function(oldHeight) { |
| 126 | + fn(); |
| 127 | + setViewportHeight(oldHeight); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + function expectTop(id, expected) { |
| 132 | + browser.driver. |
| 133 | + executeScript(_scriptGetElemTop, id). |
| 134 | + then(function(top) { expect(top).toBe(expected); }); |
| 135 | + } |
| 136 | + |
| 137 | + function expectVisible(id, expected) { |
| 138 | + browser.driver. |
| 139 | + executeScript(_scriptIsElemVisible, id). |
| 140 | + then(function(isVisible) { expect(isVisible).toBe(expected); }); |
| 141 | + } |
| 142 | + |
| 143 | + function scrollIntoView(id) { |
| 144 | + browser.driver.executeScript('document.getElementById("' + id + '").scrollIntoView()'); |
| 145 | + } |
| 146 | + |
| 147 | + function scrollToTop() { |
| 148 | + browser.driver.executeScript('window.scrollTo(0, 0)'); |
| 149 | + } |
| 150 | + |
| 151 | + function setViewportHeight(newHeight) { |
| 152 | + return browser.driver. |
| 153 | + executeScript('return document.documentElement.clientHeight'). |
| 154 | + then(function(oldHeight) { |
| 155 | + var heightDiff = newHeight - oldHeight; |
| 156 | + var win = browser.driver.manage().window(); |
| 157 | + |
| 158 | + return win.getSize().then(function(size) { |
| 159 | + return win. |
| 160 | + setSize(size.width, size.height + heightDiff). |
| 161 | + then(function() { return oldHeight; }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + } |
| 165 | +}); |
0 commit comments