forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanchor-scroll.spec.js
187 lines (156 loc) · 5.7 KB
/
anchor-scroll.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
describe('$anchorScroll', function() {
beforeEach(function() {
jasmine.addMatchers({
toBeInViewport: function() {
return {
compare: function(id) {
var result = {
pass: browser.driver.
executeScript(_script_isInViewport, id).
then(function(isInViewport) {
result.message = 'Expected #' + id + (isInViewport ? ' not' : '') +
' to be in viewport';
return isInViewport;
})
};
return result;
}
};
},
toHaveTop: function() {
return {
compare: function(id, expectedTop) {
var result = {
pass: browser.driver.
executeScript(_script_getTop, id).
then(function(actualTop) {
var passed = actualTop === expectedTop;
result.message = 'Expected #' + id + '\'s top' + (passed ? ' not' : '') +
' to be ' + expectedTop + ', but it was ' + actualTop;
return passed;
})
};
return result;
}
};
}
});
});
describe('basic functionality', function() {
beforeEach(function() {
loadFixture('anchor-scroll');
});
it('should scroll to #bottom when clicking #top and vice versa', function() {
expect('top').toBeInViewport();
expect('bottom').not.toBeInViewport();
element(by.id('top')).click();
expect('top').not.toBeInViewport();
expect('bottom').toBeInViewport();
element(by.id('bottom')).click();
expect('top').toBeInViewport();
expect('bottom').not.toBeInViewport();
});
});
describe('with `yOffset`', function() {
var yOffset = 50;
var buttons = element.all(by.repeater('x in [1, 2, 3, 4, 5]'));
var anchors = element.all(by.repeater('y in [1, 2, 3, 4, 5]'));
beforeEach(function() {
loadFixture('anchor-scroll-y-offset');
});
it('should scroll to the correct anchor when clicking each button', function() {
var lastAnchor = anchors.last();
// Make sure there is enough room to scroll the last anchor to the top
lastAnchor.getSize().then(function(size) {
var tempHeight = size.height - 10;
execWithTempViewportHeight(tempHeight, function() {
buttons.each(function(button, idx) {
// For whatever reason, we need to run the assertions inside a callback :(
button.click().then(function() {
var anchorId = 'anchor-' + (idx + 1);
expect(anchorId).toBeInViewport();
expect(anchorId).toHaveTop(yOffset);
});
});
});
});
});
it('should automatically scroll when navigating to a URL with a hash', function() {
var lastAnchor = anchors.last();
var lastAnchorId = 'anchor-5';
// Make sure there is enough room to scroll the last anchor to the top
lastAnchor.getSize().then(function(size) {
var tempHeight = size.height - 10;
execWithTempViewportHeight(tempHeight, function() {
// Test updating `$location.url()` from within the app
expect(lastAnchorId).not.toBeInViewport();
browser.setLocation('#' + lastAnchorId);
expect(lastAnchorId).toBeInViewport();
expect(lastAnchorId).toHaveTop(yOffset);
// Test navigating to the URL directly
scrollToTop();
expect(lastAnchorId).not.toBeInViewport();
browser.refresh();
expect(lastAnchorId).toBeInViewport();
expect(lastAnchorId).toHaveTop(yOffset);
});
});
});
it('should not scroll "overzealously"', function() {
var lastButton = buttons.last();
var lastAnchor = anchors.last();
var lastAnchorId = 'anchor-5';
// Make sure there is not enough room to scroll the last anchor to the top
lastAnchor.getSize().then(function(size) {
var tempHeight = size.height + (yOffset / 2);
execWithTempViewportHeight(tempHeight, function() {
scrollIntoView(lastAnchorId);
expect(lastAnchorId).toHaveTop(yOffset / 2);
lastButton.click();
expect(lastAnchorId).toBeInViewport();
expect(lastAnchorId).toHaveTop(yOffset);
});
});
});
});
// Helpers
function _script_getTop(id) {
var elem = document.getElementById(id);
var rect = elem.getBoundingClientRect();
return rect.top;
}
function _script_isInViewport(id) {
var elem = document.getElementById(id);
var rect = elem.getBoundingClientRect();
var docElem = document.documentElement;
return (rect.top < docElem.clientHeight) &&
(rect.bottom > 0) &&
(rect.left < docElem.clientWidth) &&
(rect.right > 0);
}
function execWithTempViewportHeight(tempHeight, fn) {
setViewportHeight(tempHeight).then(function(oldHeight) {
fn();
setViewportHeight(oldHeight);
});
}
function scrollIntoView(id) {
browser.driver.executeScript('document.getElementById("' + id + '").scrollIntoView()');
}
function scrollToTop() {
browser.driver.executeScript('window.scrollTo(0, 0)');
}
function setViewportHeight(newHeight) {
return browser.driver.
executeScript('return document.documentElement.clientHeight').
then(function(oldHeight) {
var heightDiff = newHeight - oldHeight;
var win = browser.driver.manage().window();
return win.getSize().then(function(size) {
return win.
setSize(size.width, size.height + heightDiff).
then(function() { return oldHeight; });
});
});
}
});