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
/
input-animations.spec.js
260 lines (208 loc) · 7.9 KB
/
input-animations.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
describe('md-input-container animations', function() {
var $rootScope, $compile, $material, $$mdInput, $window, $animate, $rootElement, $document, $timeout,
el, root, body, pageScope, computedStyle, invalidAnimation, messagesAnimation, messageAnimation;
// Load our modules
beforeEach(module('ngAnimate', 'ngMessages', 'material.components.input', 'material.components.checkbox'));
// Run pre-test setup
beforeEach(injectGlobals);
beforeEach(setupVariables);
// Run after-test teardown
afterEach(teardown);
it('set the proper styles when showing messages on an input', performInputAnimationTests);
it('set the proper styles when showing messages on an input with animations disabled', function() {
$animate.enabled(false);
performInputAnimationTests();
$animate.enabled(true);
});
function performInputAnimationTests() {
compile(
'<form name="testForm">' +
' <md-input-container>' +
' <input name="foo" ng-model="foo" required ng-pattern="/^1234$/" />' +
' <div class="errors" ng-messages="testForm.foo.$error">' +
' <div ng-message="required" style="transition: 0s none">required</div>' +
' <div ng-message="pattern" style="transition: 0s none">pattern</div>' +
' </div>' +
' </md-input-container>' +
'</form>'
);
var container = el.find('md-input-container'),
input = el.find('input'),
errors;
// Mimic the real validations/animations that fire
/*
* 1. Set to an invalid pattern but don't blur (so it's not invalid yet)
*
* Expect nothing to happen (message is hidden)
*/
setFoo('asdf');
flush();
errors = getError();
expectError(errors, 'pattern');
expect(container).not.toHaveClass('md-input-invalid');
computedStyle = $window.getComputedStyle(errors[0]);
expect(parseInt(computedStyle.opacity)).toEqual(0);
expect(parseInt(computedStyle.marginTop)).toBeLessThan(0);
/*
* 2. Blur the input, which adds the md-input-invalid class
*
* Expect to animate in the pattern message
*/
input.triggerHandler('blur');
flush();
errors = getError();
expectError(errors, 'pattern');
expect(container).toHaveClass('md-input-invalid');
computedStyle = $window.getComputedStyle(errors[0]);
expect(parseInt(computedStyle.opacity)).toEqual(1);
expect(parseInt(computedStyle.marginTop)).toEqual(0);
/*
* 3. Clear the field
*
* Expect to animate away pattern message and animate in the required message
*/
// Grab the pattern error before we change foo and it disappears
setFoo('');
expectError(getError(), 'required');
flush();
expect(container).toHaveClass('md-input-invalid');
computedStyle = $window.getComputedStyle(getError()[0]);
expect(parseInt(computedStyle.opacity)).toEqual(1);
expect(parseInt(computedStyle.marginTop)).toEqual(0);
}
describe('method tests', function() {
describe('#getMessagesElement', function() {
it('finds the messages element itself', function() {
var template = '<div class="md-input-messages-animation"></div>';
var dom = angular.element(template);
var messages = $$mdInput.messages.getElement(dom);
expect(dom).toEqual(messages);
});
it('finds a child element', function(){
var template = '<div><div class="md-input-messages-animation"></div></div>';
var dom = angular.element(template);
var realMessages = angular.element(dom[0].querySelector('.md-input-messages-animation'));
var messages = $$mdInput.messages.getElement(dom);
expect(realMessages).toEqual(messages);
});
it('finds the parent of a message animation element', function() {
var template =
'<div class="md-input-messages-animation">' +
' <div class="md-input-message-animation"></div>' +
'</div>';
var dom = angular.element(template);
var message = angular.element(dom[0].querySelector('.md-input-message-animation'));
var messages = $$mdInput.messages.getElement(message);
expect(dom).toEqual(messages);
});
});
});
it('set the proper styles when showing messages on an md-checkbox', performCheckboxAnimationTests);
it('set the proper styles when showing messages on an md-checkbox with animations disabled', function() {
$animate.enabled(false);
performCheckboxAnimationTests();
$animate.enabled(true);
});
function performCheckboxAnimationTests() {
compile(
'<form name="testForm">' +
' <md-input-container>' +
' <md-checkbox name="cb" ng-model="foo" required>Test</md-checkbox>' +
' <div class="errors" ng-messages="testForm.cb.$error">' +
' <div ng-message="required" style="transition: 0s none">required</div>' +
' </div>' +
' </md-input-container>' +
'</form>'
);
var container = el.find('md-input-container'),
checkbox = el.find('md-checkbox');
// Mimic the real validations/animations that fire
/*
* 1. Uncheck the checkbox but don't blur (so it's not invalid yet)
*
* Expect nothing to happen (message is hidden)
*/
setFoo(true);
checkbox.triggerHandler('click');
flush();
expectError(getError(), 'required');
expect(container).not.toHaveClass('md-input-invalid');
computedStyle = $window.getComputedStyle(getError()[0]);
expect(parseInt(computedStyle.opacity)).toEqual(0);
expect(parseInt(computedStyle.marginTop)).toBeLessThan(0);
/*
* 2. Blur the checkbox, which adds the md-input-invalid class
*
* Expect to animate in the required message
*/
checkbox.triggerHandler('blur');
flush();
expectError(getError(), 'required');
expect(container).toHaveClass('md-input-invalid');
computedStyle = $window.getComputedStyle(getError()[0]);
expect(parseInt(computedStyle.opacity)).toEqual(1);
expect(parseInt(computedStyle.marginTop)).toEqual(0);
/*
* 3. Clear the field
*
* Expect to animate away required message
*/
setFoo(true);
flush();
expect(getError().length).toBe(0);
}
/*
* Test Helper Functions
*/
function compile(template) {
el = $compile(template)(pageScope);
root = $rootElement.append(el)[0];
body = $document[0].body;
body.appendChild(root);
pageScope.$apply();
return el;
}
function setFoo(value) {
pageScope.foo = value;
pageScope.$digest();
}
function getError() {
return angular.element(el[0].querySelector('.errors div'));
}
function expectError(element, message) {
expect(element.text().trim()).toBe(message);
}
function flush() {
// Note: we use flushInterimElement() because it actually calls everything 3 times which seems
// to be enough to actually flush the animations
$material.flushInterimElement();
}
/*
* before/afterEach Helper Functions
*/
// Setup/grab our variables
function injectGlobals() {
inject(function($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
$material = $injector.get('$material');
$$mdInput = $injector.get('$$mdInput');
$window = $injector.get('$window');
$animate = $injector.get('$animate');
$rootElement = $injector.get('$rootElement');
$document = $injector.get('$document');
// Grab our input animations (we MUST use the injector to setup dependencies)
invalidAnimation = $injector.get('mdInputInvalidAnimation');
messagesAnimation = $injector.get('mdInputMessagesAnimation');
messageAnimation = $injector.get('mdInputMessageAnimation');
});
}
// Setup some custom variables for these tests
function setupVariables() {
pageScope = $rootScope.$new();
}
// Teardown our tests by resetting variables and removing our element
function teardown() {
el && el.remove && el.remove();
}
});