This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
sticky.spec.js
235 lines (190 loc) · 7.31 KB
/
sticky.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
describe('$mdSticky service', function() {
beforeEach(module('material.components.sticky'));
it('should compile our cloned element in the same scope', function(done) {
inject(function($compile, $rootScope, $mdSticky, $timeout) {
var scope = $rootScope.$new();
var contentEl = $compile(angular.element(
'<md-content>' +
'<sticky-directive>Sticky Element</sticky-directive>' +
'</md-content>'
))(scope);
document.body.appendChild(contentEl[0]);
var stickyEl = contentEl.children().eq(0);
$mdSticky(scope, stickyEl);
// Flush the `$$sticky.add()` timeout.
$timeout.flush();
// When the current browser, which executes that spec, supports the sticky position, then we will always succeed
// the test, because otherwise our test will fail.
if (stickyEl.css('position')) {
expect(true).toBe(true);
} else {
expect(contentEl.children().length).toBe(2);
var stickyClone = contentEl[0].querySelector('.md-sticky-clone');
expect(stickyClone).toBeTruthy();
expect(angular.element(stickyClone).scope()).toBe(scope);
}
contentEl.remove();
done();
});
});
it('should not compile our self specified clone in the given scope', function(done) {
inject(function($compile, $rootScope, $mdSticky, $timeout) {
var scope = $rootScope.$new();
var cloneScope = $rootScope.$new();
var contentEl = $compile(angular.element(
'<md-content>' +
'<sticky-directive>Sticky Element</sticky-directive>' +
'</md-content>'
))(scope);
var cloneEl = $compile(angular.element(
'<sticky-directive>Self-cloned Element</sticky-directive>'
))(cloneScope);
document.body.appendChild(contentEl[0]);
var stickyEl = contentEl.children().eq(0);
$mdSticky(scope, stickyEl, cloneEl);
// Flush the `$$sticky.add()` timeout.
$timeout.flush();
// When the current browser, which executes that spec, supports the sticky position, then we will always succeed
// the test, because otherwise our test will fail.
if (stickyEl.css('position')) {
expect(true).toBe(true);
} else {
expect(contentEl.children().length).toBe(2);
var stickyClone = contentEl[0].querySelector('.md-sticky-clone');
expect(stickyClone).toBeTruthy();
expect(angular.element(stickyClone).scope()).toBe(cloneScope);
}
contentEl.remove();
done();
});
});
});
/*
* TODO: adjust to work properly with refactors of original code
*/
/*
describe('$mdStickySpec', function() {
var $document, $compile, $rootScope, $mdSticky;
beforeEach(module('material.components.sticky'));
beforeEach(inject(function(_$document_, _$compile_, _$rootScope_, _$mdSticky_) {
$document = _$document_;
$rootScope = _$rootScope_;
$compile = _$compile_;
$mdSticky = _$mdSticky_;
}));
var $container, $firstSticky, $secondSticky, $sticky;
function setup(opts) {
opts = opts || {};
var TEST_HTML = '<md-content><h2>First sticky</h2><h2>Second sticky</h2></md-content>';
var scope = $rootScope.$new();
$container = $compile(TEST_HTML)(scope);
$firstSticky = $container.children().eq(0);
$secondSticky = $container.children().eq(1);
// Wire up our special $container instance;
$firstSticky.controller('mdContent').$element = $container;
$document.find('body').html('');
$document.find('body').append($container);
if(!opts.skipFirst) {
$mdSticky($rootScope.$new(), $firstSticky);
}
if(!opts.skipSecond) {
$mdSticky($rootScope.$new(), $secondSticky);
}
// Overwrite the scrollTop property to return the opts.containerScroll
if(opts.containerScroll) {
var originalProp = $container.prop;
$container.prop = function(prop) {
if(prop == 'scrollTop') {
return opts.containerScroll;
} else {
originalProp.call($container, prop);
}
};
}
// Overwrite children() to provide mock rect positions
if(opts.firstActual) {
$firstSticky[0].getBoundingClientRect = function() { return opts.firstActual; };
}
if(opts.secondActual) {
$secondSticky[0].getBoundingClientRect = function() { return opts.secondActual; };
}
if(opts.firstTarget) {
var $firstOuter = $firstSticky.parent();
$firstOuter[0].getBoundingClientRect = function() { return opts.firstTarget; };
}
if(opts.secondTarget) {
var $secondOuter = $secondSticky.parent();
$secondOuter[0].getBoundingClientRect = function() { return opts.secondTarget; };
}
$sticky = $container.data('$sticky');
if(opts.lastScroll) { $sticky.lastScroll = opts.lastScroll; }
scope.$digest();
}
it('throws an error if uses outside of md-content', inject(function($mdSticky, $compile, $rootScope) {
var html = '<h2>Hello world!</h2>';
function useWithoutMdContent() {
$mdSticky($rootScope.$new(), angular.element(html));
}
expect(useWithoutMdContent).toThrow('$mdSticky used outside of md-content');
}));
it('adds class md-sticky-active when an element would scroll off screen', function() {
var firstActual = { top: -10, bottom: 9, height: 19 };
setup({containerScroll: 10, firstActual: firstActual, skipSecond: true});
$sticky.check();
expect($firstSticky.hasClass('md-sticky-active')).toBe(true);
});
it('removes class md-sticky-active when an element is no longer sticky', function() {
var firstTarget = { top: 1, bottom: 10, height: 9 };
setup({
containerScroll: 10,
lastScroll: 11
});
$firstSticky.addClass('md-sticky-active');
$sticky.check();
expect($firstSticky.hasClass('md-sticky-active')).toBe(false);
});
it('pushes the active element when the next sticky element touches it', function() {
var firstTarget = { top: -10, bottom: 9, height: 19 };
var firstActual = { top: 0, bottom: 19, height: 19 };
var secondActual = { top: 18, bottom: 37, height: 19 };
setup({
containerScroll: 19,
firstActual: firstActual,
firstTarget: firstTarget,
secondActual: secondActual
});
$firstSticky.attr('md-sticky-active', true);
$sticky.check();
expect($firstSticky.data('translatedHeight')).toBe(-1);
});
it('increments the active element when it is pushed off screen', function() {
var firstActual = { top: -9, bottom: 0, height: 10 };
setup({
containerScroll: 10,
firstActual: firstActual
});
$firstSticky.addClass('md-sticky-active');
$sticky.check();
expect($firstSticky.hasClass('md-sticky-active')).toBe(false);
expect($sticky.targetIndex).toBe(1);
});
it('pulls the previous element when the sticky element losens', function() {
var firstActual = { top: -10, bottom: -1, height: 9 };
var firstTarget = { top: -50, bottom: -41, height: 9 };
var secondActual = { top: 0, bottom: 9, height: 9 };
setup({
containerScroll: 30,
lastScroll: 31,
firstActual: firstActual,
firstTarget: firstTarget,
secondTarget: secondActual,
secondActual: secondActual
});
$sticky.targetIndex = 0;
$firstSticky.data('translatedHeight', -10);
$firstSticky.addClass('md-sticky-active');
$sticky.check();
expect($firstSticky.data('translatedHeight')).toBe(-9);
});
});
*/