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
/
toolbar.spec.js
308 lines (245 loc) · 9.37 KB
/
toolbar.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
describe('<md-toolbar>', function() {
var pageScope, element, controller;
var $rootScope, $timeout;
beforeEach(function() {
module('material.components.toolbar', function($controllerProvider) {
$controllerProvider.register('MockController', function() {});
});
});
beforeEach(inject(function(_$rootScope_, _$timeout_) {
$rootScope = _$rootScope_;
$timeout = _$timeout_;
}));
it('with scrollShrink, it should shrink scrollbar when going to bottom',
inject(function($compile, $rootScope, $mdConstant, mdToolbarDirective) {
var parent = angular.element('<div>');
var toolbar = angular.element('<md-toolbar>');
var contentEl = angular.element('<div>');
// Make content and toolbar siblings
parent.append(toolbar).append(contentEl);
// Prop will be used for offsetHeight, give a fake offsetHeight
spyOn(toolbar, 'prop').and.callFake(function() {
return 100;
});
// Fake the css function so we can read css values properly,
// no matter which browser the tests are being run on.
// (IE, firefox, chrome all give different results when reading element.style)
var toolbarCss = {};
spyOn(toolbar, 'css').and.callFake(function(k, v) {
toolbarCss[k] = v;
});
var contentCss = {};
spyOn(contentEl, 'css').and.callFake(function(properties, value) {
var k;
if (angular.isObject(properties)) {
for (k in properties) {
if (properties.hasOwnProperty(k)) {
contentCss[k] = properties[k];
}
}
} else {
contentCss[properties] = value;
}
});
// Manually link so we can give our own elements with spies on them
mdToolbarDirective[0].link($rootScope, toolbar, {
mdScrollShrink: true,
mdShrinkSpeedFactor: 1,
$observe: function() {}
});
$rootScope.$apply();
$rootScope.$broadcast('$mdContentLoaded', contentEl);
$timeout.flush();
// Expect everything to be in its proper initial state.
expect(toolbarCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,0px,0)');
expect(contentCss['margin-top']).toEqual('-100px');
expect(contentCss['margin-bottom']).toEqual('-100px');
expect(contentCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,100px,0)');
// Fake scroll to the bottom
contentEl.triggerHandler({
type: 'scroll',
target: {scrollTop: 500}
});
expect(toolbarCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,-100px,0)');
expect(contentCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,0px,0)');
// Fake scroll back to the top
contentEl.triggerHandler({
type: 'scroll',
target: {scrollTop: 0}
});
expect(toolbarCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,0px,0)');
expect(contentCss[$mdConstant.CSS.TRANSFORM]).toEqual('translate3d(0,100px,0)');
}));
it('works without ng-if', inject(function() {
build(
'<div>' +
' <md-toolbar md-scroll-shrink="true"></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
expect(element.find('md-content').attr('scroll-shrink')).toEqual('true');
}));
it('works with ng-if', inject(function() {
build(
'<div>' +
' <md-toolbar md-scroll-shrink="true" ng-if="shouldShowToolbar"></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
// It starts out undefined
expect(element.find('md-content').attr('scroll-shrink')).toEqual(undefined);
// Change the ng-if to add the toolbar which then injects a scrollShrink
// on the mdContent
pageScope.$apply('shouldShowToolbar = true');
expect(element.find('md-content').attr('scroll-shrink')).toEqual('true');
// Change the ng-if to remove the toolbar
pageScope.$apply('shouldShowToolbar = false');
expect(element.find('md-toolbar').length).toBe(0);
}));
it('works with ng-show', inject(function($timeout) {
var template =
'<div layout="column" style="height: 600px;">' +
' <md-toolbar md-scroll-shrink="true" ng-show="shouldShowToolbar">test</md-toolbar>' +
' <md-content flex><div style="height: 5000px;"></div></md-content>' +
'</div>';
// Build/append the element
build(template);
document.body.appendChild(element[0]);
//
// Initial tests
//
var toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
var contentStyles = getComputedStyle(element.find('md-content')[0]);
// Should start out hidden because we have not set shouldShowToolbar
expect(toolbarStyles.display).toBeTruthy();
expect(toolbarStyles.display).toEqual('none');
// Expect the content to have a zero margin top
expect(contentStyles.marginTop).toBeTruthy();
expect(contentStyles.marginTop).toEqual('0px');
//
// After showing toolbar tests
//
// Show the toolbar and ensure it is visible
pageScope.$apply('shouldShowToolbar = true');
pageScope.$digest();
$timeout.flush();
toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
contentStyles = getComputedStyle(element.find('md-content')[0]);
// Expect the toolbar to be visible
expect(toolbarStyles.display).toBeTruthy();
expect(toolbarStyles.display).not.toEqual('none');
// Expect the content to have a non-zero margin top (because updateToolbarHeight() was called)
expect(contentStyles.marginTop).toBeTruthy();
expect(contentStyles.marginTop).not.toEqual('0px');
// Remove the element
document.body.removeChild(element[0]);
}));
it('works with ng-hide', inject(function($timeout) {
var template =
'<div layout="column" style="height: 600px;">' +
' <md-toolbar md-scroll-shrink="true" ng-hide="shouldNotShowToolbar">test</md-toolbar>' +
' <md-content flex><div style="height: 5000px;"></div></md-content>' +
'</div>';
// Build/append the element
build(template);
document.body.appendChild(element[0]);
// Flushing to get the actual height of toolbar
$timeout.flush();
//
// Initial tests
//
var toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
var contentStyles = getComputedStyle(element.find('md-content')[0]);
// Should start out visible because we have not set shouldNotShowToolbar
expect(toolbarStyles.display).toBeTruthy();
expect(toolbarStyles.display).not.toEqual('none');
// Expect the content to have a non-zero margin top
expect(contentStyles.marginTop).toBeTruthy();
expect(contentStyles.marginTop).not.toEqual('0px');
//
// After showing toolbar tests
//
// Show the toolbar and ensure it is hidden
pageScope.$apply('shouldNotShowToolbar = true');
pageScope.$digest();
$timeout.flush();
toolbarStyles = getComputedStyle(element.find('md-toolbar')[0]);
contentStyles = getComputedStyle(element.find('md-content')[0]);
// Expect the toolbar to be hidden
expect(toolbarStyles.display).toBeTruthy();
expect(toolbarStyles.display).toEqual('none');
// Expect the content to have a zero margin top (because updateToolbarHeight() was called)
expect(contentStyles.marginTop).toBeTruthy();
expect(contentStyles.marginTop).toEqual('0px');
// Remove the element
document.body.removeChild(element[0]);
}));
// The toolbar is like a container component, so we want to make sure it works with ng-controller
it('works with ng-controller', inject(function($exceptionHandler) {
build(
'<div>' +
' <md-toolbar md-scroll-shrink ng-controller="MockController"></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
// Expect no errors
expect($exceptionHandler.errors).toEqual([]);
}));
it('should have `._md` class indicator', inject(function() {
build(
'<div>' +
' <md-toolbar></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
expect(element.find('md-toolbar').hasClass('_md')).toBe(true);
}));
it('disables scroll shrink when the attribute is not provided', inject(function() {
build(
'<div>' +
' <md-toolbar></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
expect(element.find('md-content').attr('scroll-shrink')).toEqual(undefined);
}));
it('enables scroll shrink when the attribute has no value', function() {
build(
'<div>' +
' <md-toolbar md-scroll-shrink></md-toolbar>' +
' <md-content></md-content>' +
'</div>'
);
expect(element.find('md-content').attr('scroll-shrink')).toEqual('true');
});
it('disables scroll shrink if the expression evaluates to false', function() {
var pageScope = $rootScope.$new();
// Set the value to false
pageScope.$apply('someValue = false');
// Build the element
build(
// Pass our template
'<div>' +
' <md-toolbar md-scroll-shrink="someValue"></md-toolbar>' +
' <md-content></md-content>' +
'</div>',
// Pass our custom pageScope
pageScope
);
// Check that scroll shrink is disabled
expect(element.find('md-content').attr('scroll-shrink')).toEqual('false');
});
function build(template, scope) {
inject(function($compile) {
if (scope) {
pageScope = scope
} else {
pageScope = $rootScope.$new();
}
element = $compile(template)(pageScope);
controller = element.controller('mdToolbar');
pageScope.$apply();
$timeout.flush();
});
}
});