1
1
describe ( 'md-input-container animations' , function ( ) {
2
- var $rootScope , $compile , $animateCss , $material , $$mdInput ,
3
- el , pageScope , invalidAnimation , messagesAnimation , messageAnimation ,
4
- cssTransitionsDisabled = false , lastAnimateCall ;
2
+ var $rootScope , $compile , $material , $window ,
3
+ el , pageScope , computedStyle ;
5
4
6
5
// Load our modules
7
6
beforeEach ( module ( 'ngAnimate' , 'ngMessages' , 'material.components.input' , 'material.components.checkbox' ) ) ;
8
7
9
8
// Run pre-test setup
10
- beforeEach ( decorateAnimateCss ) ;
11
9
beforeEach ( injectGlobals ) ;
12
10
beforeEach ( setupVariables ) ;
13
11
@@ -20,169 +18,69 @@ describe('md-input-container animations', function() {
20
18
' <md-input-container>' +
21
19
' <input name="foo" ng-model="foo" required ng-pattern="/^1234$/" />' +
22
20
' <div class="errors" ng-messages="testForm.foo.$error">' +
23
- ' <div ng-message="required">required</div>' +
24
- ' <div ng-message="pattern">pattern</div>' +
21
+ ' <div ng-message="required" style="transition:0s none" >required</div>' +
22
+ ' <div ng-message="pattern" style="transition:0s none" >pattern</div>' +
25
23
' </div>' +
26
24
' </md-input-container>' +
27
25
'</form>'
28
26
) ;
29
27
30
28
var container = el . find ( 'md-input-container' ) ,
31
- input = el . find ( 'input' ) ,
32
- doneSpy = jasmine . createSpy ( 'done' ) ;
29
+ input = el . find ( 'input' ) ;
33
30
34
31
// Mimic the real validations/animations that fire
35
32
36
33
/*
37
34
* 1. Set to an invalid pattern but don't blur (so it's not invalid yet)
38
35
*
39
- * Expect nothing to happen ($animateCss called with no options )
36
+ * Expect nothing to happen (message is hidden )
40
37
*/
41
38
42
39
setFoo ( 'asdf' ) ;
43
- messageAnimation . enter ( getError ( ) , doneSpy ) ;
44
40
flush ( ) ;
45
41
46
42
expectError ( getError ( ) , 'pattern' ) ;
47
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
48
43
expect ( container ) . not . toHaveClass ( 'md-input-invalid' ) ;
49
- expect ( lastAnimateCall ) . toEqual ( { element : getError ( ) , options : { } } ) ;
44
+
45
+ computedStyle = $window . getComputedStyle ( getError ( ) [ 0 ] ) ;
46
+ expect ( parseInt ( computedStyle . opacity ) ) . toEqual ( 0 ) ; // not visible
47
+ expect ( parseInt ( computedStyle . marginTop ) ) . toBeLessThan ( 0 ) ;
50
48
51
49
/*
52
50
* 2. Blur the input, which adds the md-input-invalid class
53
51
*
54
52
* Expect to animate in the pattern message
55
53
*/
56
54
57
- doneSpy . calls . reset ( ) ;
58
55
input . triggerHandler ( 'blur' ) ;
59
- invalidAnimation . addClass ( container , 'md-input-invalid' , doneSpy ) ;
60
56
flush ( ) ;
61
57
62
58
expectError ( getError ( ) , 'pattern' ) ;
63
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
64
59
expect ( container ) . toHaveClass ( 'md-input-invalid' ) ;
65
- expect ( lastAnimateCall . element ) . toEqual ( getError ( ) ) ;
66
- expect ( lastAnimateCall . options . event ) . toEqual ( 'enter' ) ;
67
- expect ( lastAnimateCall . options . to ) . toEqual ( { "opacity" : 1 , "margin-top" : "0" } ) ;
60
+ computedStyle = $window . getComputedStyle ( getError ( ) [ 0 ] ) ;
61
+ expect ( parseInt ( computedStyle . opacity ) ) . toEqual ( 1 ) ; // visisble
62
+ expect ( parseInt ( computedStyle . marginTop ) ) . toEqual ( 0 ) ;
68
63
69
64
/*
70
65
* 3. Clear the field
71
66
*
72
67
* Expect to animate away pattern message and animate in the required message
73
68
*/
74
69
75
- // Grab the pattern error before we change foo and it disappears
76
- var patternError = getError ( ) ;
77
-
78
- doneSpy . calls . reset ( ) ;
79
- messageAnimation . leave ( patternError , doneSpy ) ;
80
- flush ( ) ;
81
-
82
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
83
- expect ( lastAnimateCall . element ) . toEqual ( patternError ) ;
84
- expect ( lastAnimateCall . options . event ) . toEqual ( 'leave' ) ;
85
- expect ( parseInt ( lastAnimateCall . options . to [ "margin-top" ] ) ) . toBeLessThan ( 0 ) ;
86
-
87
70
setFoo ( '' ) ;
88
71
expectError ( getError ( ) , 'required' ) ;
89
-
90
- doneSpy . calls . reset ( ) ;
91
- messageAnimation . enter ( getError ( ) , doneSpy ) ;
92
72
flush ( ) ;
93
73
94
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
95
74
expect ( container ) . toHaveClass ( 'md-input-invalid' ) ;
96
- expect ( lastAnimateCall . element ) . toEqual ( getError ( ) ) ;
97
- expect ( lastAnimateCall . options . event ) . toEqual ( 'enter' ) ;
98
- expect ( lastAnimateCall . options . to ) . toEqual ( { "opacity" : 1 , "margin-top" : "0" } ) ;
99
- } ) ;
100
-
101
- describe ( 'method tests' , function ( ) {
102
-
103
- describe ( '#showInputMessages' , function ( ) {
104
- it ( 'logs a warning with no messages element' , inject ( function ( $log ) {
105
- // Note that the element does NOT have a parent md-input-messages-animation class
106
- var element = angular . element ( '<div><div class="md-input-message-animation"></div></div>' ) ;
107
- var done = jasmine . createSpy ( 'done' ) ;
108
- var warnSpy = spyOn ( $log , 'warn' ) ;
109
-
110
- $$mdInput . messages . show ( element , done ) ;
111
-
112
- expect ( done ) . toHaveBeenCalled ( ) ;
113
- expect ( warnSpy ) . toHaveBeenCalled ( ) ;
114
- } ) ) ;
115
-
116
- it ( 'logs a warning with no messages children' , inject ( function ( $log ) {
117
- // Note that the element does NOT have any child md-input-message-animation divs
118
- var element = angular . element ( '<div class="md-input-messages-animation"></div>' ) ;
119
- var done = jasmine . createSpy ( 'done' ) ;
120
- var warnSpy = spyOn ( $log , 'warn' ) ;
121
-
122
- $$mdInput . messages . show ( element , done ) ;
123
-
124
- expect ( done ) . toHaveBeenCalled ( ) ;
125
- expect ( warnSpy ) . toHaveBeenCalled ( ) ;
126
- } ) ) ;
127
- } ) ;
128
-
129
- describe ( '#hideInputMessages' , function ( ) {
130
- it ( 'logs a warning with no messages element' , inject ( function ( $log ) {
131
- // Note that the element does NOT have a parent md-input-messages-animation class
132
- var element = angular . element ( '<div><div class="md-input-message-animation"></div></div>' ) ;
133
- var done = jasmine . createSpy ( 'done' ) ;
134
- var warnSpy = spyOn ( $log , 'warn' ) ;
75
+
76
+ // Expect required messsage to be visible
77
+ computedStyle = $window . getComputedStyle ( getError ( ) [ 0 ] ) ;
78
+ expect ( parseInt ( computedStyle . opacity ) ) . toEqual ( 1 ) ;
79
+ expect ( parseInt ( computedStyle . marginTop ) ) . toEqual ( 0 ) ;
135
80
136
- $$mdInput . messages . hide ( element , done ) ;
81
+ // Expect pattern messsage to not be visible
82
+ expect ( getError ( ) [ 1 ] ) . toBeUndefined ( ) ;
137
83
138
- expect ( done ) . toHaveBeenCalled ( ) ;
139
- expect ( warnSpy ) . toHaveBeenCalled ( ) ;
140
- } ) ) ;
141
-
142
- it ( 'logs a warning with no messages children' , inject ( function ( $log ) {
143
- // Note that the element does NOT have any child md-input-message-animation divs
144
- var element = angular . element ( '<div class="md-input-messages-animation"></div>' ) ;
145
- var done = jasmine . createSpy ( 'done' ) ;
146
- var warnSpy = spyOn ( $log , 'warn' ) ;
147
-
148
- $$mdInput . messages . hide ( element , done ) ;
149
-
150
- expect ( done ) . toHaveBeenCalled ( ) ;
151
- expect ( warnSpy ) . toHaveBeenCalled ( ) ;
152
- } ) ) ;
153
- } ) ;
154
-
155
- describe ( '#getMessagesElement' , function ( ) {
156
-
157
- it ( 'finds the messages element itself' , function ( ) {
158
- var template = '<div class="md-input-messages-animation"></div>' ;
159
- var dom = angular . element ( template ) ;
160
- var messages = $$mdInput . messages . getElement ( dom ) ;
161
-
162
- expect ( dom ) . toEqual ( messages ) ;
163
- } ) ;
164
-
165
- it ( 'finds a child element' , function ( ) {
166
- var template = '<div><div class="md-input-messages-animation"></div></div>' ;
167
- var dom = angular . element ( template ) ;
168
- var realMessages = angular . element ( dom [ 0 ] . querySelector ( '.md-input-messages-animation' ) ) ;
169
- var messages = $$mdInput . messages . getElement ( dom ) ;
170
-
171
- expect ( realMessages ) . toEqual ( messages ) ;
172
- } ) ;
173
-
174
- it ( 'finds the parent of a message animation element' , function ( ) {
175
- var template =
176
- '<div class="md-input-messages-animation">' +
177
- ' <div class="md-input-message-animation"></div>' +
178
- '</div>' ;
179
- var dom = angular . element ( template ) ;
180
- var message = angular . element ( dom [ 0 ] . querySelector ( '.md-input-message-animation' ) ) ;
181
- var messages = $$mdInput . messages . getElement ( message ) ;
182
-
183
- expect ( dom ) . toEqual ( messages ) ;
184
- } ) ;
185
- } ) ;
186
84
} ) ;
187
85
188
86
it ( 'set the proper styles when showing messages on an md-checkbox' , function ( ) {
@@ -191,7 +89,7 @@ describe('md-input-container animations', function() {
191
89
' <md-input-container>' +
192
90
' <md-checkbox name="cb" ng-model="foo" required>Test</md-checkbox>' +
193
91
' <div class="errors" ng-messages="testForm.cb.$error">' +
194
- ' <div ng-message="required">required</div>' +
92
+ ' <div ng-message="required" style="transition:0s none" >required</div>' +
195
93
' </div>' +
196
94
' </md-input-container>' +
197
95
'</form>'
@@ -211,46 +109,40 @@ describe('md-input-container animations', function() {
211
109
212
110
setFoo ( true ) ;
213
111
checkbox . triggerHandler ( 'click' ) ;
214
- messageAnimation . enter ( getError ( ) , doneSpy ) ;
215
112
flush ( ) ;
216
113
217
114
expectError ( getError ( ) , 'required' ) ;
218
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
219
115
expect ( container ) . not . toHaveClass ( 'md-input-invalid' ) ;
220
- expect ( lastAnimateCall ) . toEqual ( { element : getError ( ) , options : { } } ) ;
116
+
117
+ computedStyle = $window . getComputedStyle ( getError ( ) [ 0 ] ) ;
118
+ expect ( parseInt ( computedStyle . opacity ) ) . toEqual ( 0 ) ; // not visible
119
+ expect ( parseInt ( computedStyle . marginTop ) ) . toBeLessThan ( 0 ) ;
221
120
222
121
/*
223
122
* 2. Blur the checkbox, which adds the md-input-invalid class
224
123
*
225
124
* Expect to animate in the required message
226
125
*/
227
126
228
- doneSpy . calls . reset ( ) ;
229
127
checkbox . triggerHandler ( 'blur' ) ;
230
- invalidAnimation . addClass ( container , 'md-input-invalid' , doneSpy ) ;
231
128
flush ( ) ;
232
129
233
130
expectError ( getError ( ) , 'required' ) ;
234
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
235
131
expect ( container ) . toHaveClass ( 'md-input-invalid' ) ;
236
- expect ( lastAnimateCall . element ) . toEqual ( getError ( ) ) ;
237
- expect ( lastAnimateCall . options . event ) . toEqual ( 'enter' ) ;
238
- expect ( lastAnimateCall . options . to ) . toEqual ( { "opacity" : 1 , "margin-top" : "0" } ) ;
132
+ computedStyle = $window . getComputedStyle ( getError ( ) [ 0 ] ) ;
133
+ expect ( parseInt ( computedStyle . opacity ) ) . toEqual ( 1 ) ; // visisble
134
+ expect ( parseInt ( computedStyle . marginTop ) ) . toEqual ( 0 ) ;
239
135
240
136
/*
241
137
* 3. Clear the field
242
138
*
243
139
* Expect to animate away required message
244
140
*/
245
141
246
- doneSpy . calls . reset ( ) ;
247
- messageAnimation . leave ( getError ( ) , doneSpy ) ;
142
+ setFoo ( true ) ;
248
143
flush ( ) ;
249
144
250
- expect ( doneSpy ) . toHaveBeenCalled ( ) ;
251
- expect ( lastAnimateCall . element ) . toEqual ( getError ( ) ) ;
252
- expect ( lastAnimateCall . options . event ) . toEqual ( 'leave' ) ;
253
- expect ( parseInt ( lastAnimateCall . options . to [ "margin-top" ] ) ) . toBeLessThan ( 0 ) ;
145
+ expect ( getError ( ) [ 0 ] ) . toBeUndefined ( ) ;
254
146
255
147
} ) ;
256
148
@@ -290,59 +182,23 @@ describe('md-input-container animations', function() {
290
182
* before/afterEach Helper Functions
291
183
*/
292
184
293
- // Decorate the $animateCss service so we can spy on it and disable any CSS transitions
294
- function decorateAnimateCss ( ) {
295
- module ( function ( $provide ) {
296
- $provide . decorator ( '$animateCss' , function ( $delegate ) {
297
- return jasmine . createSpy ( '$animateCss' ) . and . callFake ( function ( element , options ) {
298
-
299
- // Store the last call to $animateCss
300
- //
301
- // NOTE: We handle this manually because the actual code modifies the options
302
- // and can make the tests fail if it executes before the expect() fires
303
- lastAnimateCall = {
304
- element : element ,
305
- options : angular . copy ( options )
306
- } ;
307
-
308
- // Make sure any transitions happen immediately; NOTE: this is REQUIRED for the above
309
- // tests to pass without using window.setTimeout to wait for the animations
310
- if ( cssTransitionsDisabled ) {
311
- element . css ( 'transition' , '0s none' ) ;
312
- }
313
-
314
- return $delegate ( element , options ) ;
315
- } ) ;
316
- } ) ;
317
- } ) ;
318
- }
319
-
320
185
// Setup/grab our variables
321
186
function injectGlobals ( ) {
322
187
inject ( function ( $injector ) {
323
188
$rootScope = $injector . get ( '$rootScope' ) ;
324
189
$compile = $injector . get ( '$compile' ) ;
325
- $animateCss = $injector . get ( '$animateCss' ) ;
326
190
$material = $injector . get ( '$material' ) ;
327
- $$mdInput = $injector . get ( '$$mdInput' ) ;
328
-
329
- // Grab our input animations (we MUST use the injector to setup dependencies)
330
- invalidAnimation = $injector . get ( 'mdInputInvalidAnimation' ) ;
331
- messagesAnimation = $injector . get ( 'mdInputMessagesAnimation' ) ;
332
- messageAnimation = $injector . get ( 'mdInputMessageAnimation' ) ;
191
+ $window = $injector . get ( '$window' ) ;
333
192
} ) ;
334
193
}
335
194
336
195
// Setup some custom variables for these tests
337
196
function setupVariables ( ) {
338
197
pageScope = $rootScope . $new ( ) ;
339
- cssTransitionsDisabled = true ;
340
198
}
341
199
342
200
// Teardown our tests by resetting variables and removing our element
343
201
function teardown ( ) {
344
- cssTransitionsDisabled = false ;
345
-
346
202
el && el . remove && el . remove ( ) ;
347
203
}
348
204
} ) ;
0 commit comments