forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformSpec.js
794 lines (623 loc) · 25.4 KB
/
formSpec.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* global FormController: false */
'use strict';
describe('form', function() {
var doc, control, scope, $compile, changeInputValue;
beforeEach(module(function($compileProvider) {
$compileProvider.directive('storeModelCtrl', function() {
return {
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
control = ctrl;
}
};
});
}));
beforeEach(inject(function($injector, $sniffer) {
$compile = $injector.get('$compile');
scope = $injector.get('$rootScope');
changeInputValue = function(elm, value) {
elm.val(value);
browserTrigger(elm, $sniffer.hasEvent('input') ? 'input' : 'change');
};
}));
afterEach(function() {
dealoc(doc);
});
it('should instantiate form and attach it to DOM', function() {
doc = $compile('<form>')(scope);
expect(doc.data('$formController')).toBeTruthy();
expect(doc.data('$formController') instanceof FormController).toBe(true);
});
it('should remove form control references from the form when nested control is removed from the DOM', function() {
doc = $compile(
'<form name="myForm">' +
'<input ng-if="inputPresent" name="alias" ng-model="value" store-model-ctrl/>' +
'</form>')(scope);
scope.inputPresent = true;
scope.$digest();
var form = scope.myForm;
control.$setValidity('required', false);
expect(form.alias).toBe(control);
expect(form.$error.required).toEqual([control]);
// remove nested control
scope.inputPresent = false;
scope.$apply();
expect(form.$error.required).toBe(false);
expect(form.alias).toBeUndefined();
});
it('should use ngForm value as form name', function() {
doc = $compile(
'<div ng-form="myForm">' +
'<input type="text" name="alias" ng-model="value"/>' +
'</div>')(scope);
expect(scope.myForm).toBeDefined();
expect(scope.myForm.alias).toBeDefined();
});
it('should use ngForm value as form name when nested inside form', function () {
doc = $compile(
'<form name="myForm">' +
'<div ng-form="nestedForm"><input type="text" name="alias" ng-model="value"/></div>' +
'</form>')(scope);
expect(scope.myForm).toBeDefined();
expect(scope.myForm.nestedForm).toBeDefined();
expect(scope.myForm.nestedForm.alias).toBeDefined();
});
it('should publish form to scope when name attr is defined', function() {
doc = $compile('<form name="myForm"></form>')(scope);
expect(scope.myForm).toBeTruthy();
expect(doc.data('$formController')).toBeTruthy();
expect(doc.data('$formController')).toEqual(scope.myForm);
});
it('should support expression in form name', function() {
doc = $compile('<form name="obj.myForm"></form>')(scope);
expect(scope.obj).toBeDefined();
expect(scope.obj.myForm).toBeTruthy();
});
it('should support two forms on a single scope', function() {
doc = $compile(
'<div>' +
'<form name="formA">' +
'<input name="firstName" ng-model="firstName" required>' +
'</form>' +
'<form name="formB">' +
'<input name="lastName" ng-model="lastName" required>' +
'</form>' +
'</div>'
)(scope);
scope.$apply();
expect(scope.formA.$error.required.length).toBe(1);
expect(scope.formA.$error.required).toEqual([scope.formA.firstName]);
expect(scope.formB.$error.required.length).toBe(1);
expect(scope.formB.$error.required).toEqual([scope.formB.lastName]);
var inputA = doc.find('input').eq(0),
inputB = doc.find('input').eq(1);
changeInputValue(inputA, 'val1');
changeInputValue(inputB, 'val2');
expect(scope.firstName).toBe('val1');
expect(scope.lastName).toBe('val2');
expect(scope.formA.$error.required).toBe(false);
expect(scope.formB.$error.required).toBe(false);
});
it('should publish widgets', function() {
doc = jqLite('<form name="form"><input type="text" name="w1" ng-model="some" /></form>');
$compile(doc)(scope);
var widget = scope.form.w1;
expect(widget).toBeDefined();
expect(widget.$pristine).toBe(true);
expect(widget.$dirty).toBe(false);
expect(widget.$valid).toBe(true);
expect(widget.$invalid).toBe(false);
});
it('should throw an exception if an input has name="hasOwnProperty"', function() {
doc = jqLite(
'<form name="form">'+
'<input name="hasOwnProperty" ng-model="some" />'+
'<input name="other" ng-model="someOther" />'+
'</form>');
expect(function() {
$compile(doc)(scope);
}).toThrowMinErr('ng', 'badname');
});
describe('triggering commit value on submit', function () {
it('should trigger update on form submit', function() {
var form = $compile(
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
'<input type="text" ng-model="name" />' +
'</form>')(scope);
scope.$digest();
var inputElm = form.find('input').eq(0);
changeInputValue(inputElm, 'a');
expect(scope.name).toEqual(undefined);
browserTrigger(form, 'submit');
expect(scope.name).toEqual('a');
dealoc(form);
});
it('should trigger update on form submit with nested forms', function() {
var form = $compile(
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
'<div class="ng-form" name="child">' +
'<input type="text" ng-model="name" />' +
'</div>' +
'</form>')(scope);
scope.$digest();
var inputElm = form.find('input').eq(0);
changeInputValue(inputElm, 'a');
expect(scope.name).toEqual(undefined);
browserTrigger(form, 'submit');
expect(scope.name).toEqual('a');
dealoc(form);
});
it('should trigger update before ng-submit is invoked', function() {
var form = $compile(
'<form name="test" ng-submit="submit()" ' +
'ng-model-options="{ updateOn: \'\' }" >' +
'<input type="text" ng-model="name" />' +
'</form>')(scope);
scope.$digest();
var inputElm = form.find('input').eq(0);
changeInputValue(inputElm, 'a');
scope.submit = jasmine.createSpy('submit').andCallFake(function() {
expect(scope.name).toEqual('a');
});
browserTrigger(form, 'submit');
expect(scope.submit).toHaveBeenCalled();
dealoc(form);
});
});
describe('rollback view value', function () {
it('should trigger rollback on form controls', function() {
var form = $compile(
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
'<input type="text" ng-model="name" />' +
'<button ng-click="test.$rollbackViewValue()" />' +
'</form>')(scope);
scope.$digest();
var inputElm = form.find('input').eq(0);
changeInputValue(inputElm, 'a');
expect(inputElm.val()).toBe('a');
browserTrigger(form.find('button'), 'click');
expect(inputElm.val()).toBe('');
dealoc(form);
});
it('should trigger rollback on form controls with nested forms', function() {
var form = $compile(
'<form name="test" ng-model-options="{ updateOn: \'\' }" >' +
'<div class="ng-form" name="child">' +
'<input type="text" ng-model="name" />' +
'</div>' +
'<button ng-click="test.$rollbackViewValue()" />' +
'</form>')(scope);
scope.$digest();
var inputElm = form.find('input').eq(0);
changeInputValue(inputElm, 'a');
expect(inputElm.val()).toBe('a');
browserTrigger(form.find('button'), 'click');
expect(inputElm.val()).toBe('');
dealoc(form);
});
});
describe('preventing default submission', function() {
it('should prevent form submission', function() {
var nextTurn = false,
submitted = false,
reloadPrevented;
doc = jqLite('<form ng-submit="submitMe()">' +
'<input type="submit" value="submit">' +
'</form>');
var assertPreventDefaultListener = function(e) {
reloadPrevented = e.defaultPrevented || (e.returnValue === false);
};
// native dom event listeners in IE8 fire in LIFO order so we have to register them
// there in different order than in other browsers
if (msie==8) addEventListenerFn(doc[0], 'submit', assertPreventDefaultListener);
$compile(doc)(scope);
scope.submitMe = function() {
submitted = true;
};
if (msie!=8) addEventListenerFn(doc[0], 'submit', assertPreventDefaultListener);
browserTrigger(doc.find('input'));
// let the browser process all events (and potentially reload the page)
setTimeout(function() { nextTurn = true;});
waitsFor(function() { return nextTurn; });
runs(function() {
expect(reloadPrevented).toBe(true);
expect(submitted).toBe(true);
// prevent mem leak in test
removeEventListenerFn(doc[0], 'submit', assertPreventDefaultListener);
});
});
it('should prevent the default when the form is destroyed by a submission via a click event',
inject(function($timeout) {
doc = jqLite('<div>' +
'<form ng-submit="submitMe()">' +
'<button ng-click="destroy()"></button>' +
'</form>' +
'</div>');
var form = doc.find('form'),
destroyed = false,
nextTurn = false,
submitted = false,
reloadPrevented;
scope.destroy = function() {
// yes, I know, scope methods should not do direct DOM manipulation, but I wanted to keep
// this test small. Imagine that the destroy action will cause a model change (e.g.
// $location change) that will cause some directive to destroy the dom (e.g. ngView+$route)
doc.empty();
destroyed = true;
};
scope.submitMe = function() {
submitted = true;
};
var assertPreventDefaultListener = function(e) {
reloadPrevented = e.defaultPrevented || (e.returnValue === false);
};
// native dom event listeners in IE8 fire in LIFO order so we have to register them
// there in different order than in other browsers
if (msie == 8) addEventListenerFn(form[0], 'submit', assertPreventDefaultListener);
$compile(doc)(scope);
if (msie != 8) addEventListenerFn(form[0], 'submit', assertPreventDefaultListener);
browserTrigger(doc.find('button'), 'click');
// let the browser process all events (and potentially reload the page)
setTimeout(function() { nextTurn = true;}, 100);
waitsFor(function() { return nextTurn; });
// I can't get IE8 to automatically trigger submit in this test, in production it does it
// properly
if (msie == 8) browserTrigger(form, 'submit');
runs(function() {
expect(doc.html()).toBe('');
expect(destroyed).toBe(true);
expect(submitted).toBe(false); // this is known corner-case that is not currently handled
// the issue is that the submit listener is destroyed before
// the event propagates there. we can fix this if we see
// the issue in the wild, I'm not going to bother to do it
// now. (i)
// IE9 and IE10 are special and don't fire submit event when form was destroyed
if (msie < 9) {
expect(reloadPrevented).toBe(true);
$timeout.flush();
}
// prevent mem leak in test
removeEventListenerFn(form[0], 'submit', assertPreventDefaultListener);
});
}));
it('should NOT prevent form submission if action attribute present', function() {
var callback = jasmine.createSpy('submit').andCallFake(function(event) {
expect(event.isDefaultPrevented()).toBe(false);
event.preventDefault();
});
doc = $compile('<form action="some.py"></form>')(scope);
doc.on('submit', callback);
browserTrigger(doc, 'submit');
expect(callback).toHaveBeenCalledOnce();
});
});
describe('nested forms', function() {
it('should chain nested forms', function() {
doc = jqLite(
'<ng:form name="parent">' +
'<ng:form name="child">' +
'<input ng:model="modelA" name="inputA">' +
'<input ng:model="modelB" name="inputB">' +
'</ng:form>' +
'</ng:form>');
$compile(doc)(scope);
var parent = scope.parent,
child = scope.child,
inputA = child.inputA,
inputB = child.inputB;
inputA.$setValidity('MyError', false);
inputB.$setValidity('MyError', false);
expect(parent.$error.MyError).toEqual([child]);
expect(child.$error.MyError).toEqual([inputA, inputB]);
inputA.$setValidity('MyError', true);
expect(parent.$error.MyError).toEqual([child]);
expect(child.$error.MyError).toEqual([inputB]);
inputB.$setValidity('MyError', true);
expect(parent.$error.MyError).toBe(false);
expect(child.$error.MyError).toBe(false);
child.$setDirty();
expect(parent.$dirty).toBeTruthy();
child.$setSubmitted();
expect(parent.$submitted).toBeTruthy();
});
it('should deregister a child form when its DOM is removed', function() {
doc = jqLite(
'<form name="parent">' +
'<div class="ng-form" name="child">' +
'<input ng:model="modelA" name="inputA" required>' +
'</div>' +
'</form>');
$compile(doc)(scope);
scope.$apply();
var parent = scope.parent,
child = scope.child;
expect(parent).toBeDefined();
expect(child).toBeDefined();
expect(parent.$error.required).toEqual([child]);
doc.children().remove(); //remove child
expect(parent.child).toBeUndefined();
expect(scope.child).toBeUndefined();
expect(parent.$error.required).toBe(false);
});
it('should deregister a child form whose name is an expression when its DOM is removed', function() {
doc = jqLite(
'<form name="parent">' +
'<div class="ng-form" name="child.form">' +
'<input ng:model="modelA" name="inputA" required>' +
'</div>' +
'</form>');
$compile(doc)(scope);
scope.$apply();
var parent = scope.parent,
child = scope.child.form;
expect(parent).toBeDefined();
expect(child).toBeDefined();
expect(parent.$error.required).toEqual([child]);
doc.children().remove(); //remove child
expect(parent.child).toBeUndefined();
expect(scope.child.form).toBeUndefined();
expect(parent.$error.required).toBe(false);
});
it('should deregister a input when it is removed from DOM', function() {
doc = jqLite(
'<form name="parent">' +
'<div class="ng-form" name="child">' +
'<input ng-if="inputPresent" ng-model="modelA" name="inputA" required>' +
'</div>' +
'</form>');
$compile(doc)(scope);
scope.inputPresent = true;
scope.$apply();
var parent = scope.parent,
child = scope.child,
input = child.inputA;
expect(parent).toBeDefined();
expect(child).toBeDefined();
expect(parent.$error.required).toEqual([child]);
expect(child.$error.required).toEqual([input]);
expect(doc.hasClass('ng-invalid')).toBe(true);
expect(doc.hasClass('ng-invalid-required')).toBe(true);
expect(doc.find('div').hasClass('ng-invalid')).toBe(true);
expect(doc.find('div').hasClass('ng-invalid-required')).toBe(true);
//remove child input
scope.inputPresent = false;
scope.$apply();
expect(parent.$error.required).toBe(false);
expect(child.$error.required).toBe(false);
expect(doc.hasClass('ng-valid')).toBe(true);
expect(doc.hasClass('ng-valid-required')).toBe(true);
expect(doc.find('div').hasClass('ng-valid')).toBe(true);
expect(doc.find('div').hasClass('ng-valid-required')).toBe(true);
});
it('should chain nested forms in repeater', function() {
doc = jqLite(
'<ng:form name=parent>' +
'<ng:form ng:repeat="f in forms" name=child>' +
'<input type=text ng:model=text name=text>' +
'</ng:form>' +
'</ng:form>');
$compile(doc)(scope);
scope.$apply(function() {
scope.forms = [1];
});
var parent = scope.parent;
var child = doc.find('input').scope().child;
var input = child.text;
expect(parent).toBeDefined();
expect(child).toBeDefined();
expect(input).toBeDefined();
input.$setValidity('myRule', false);
expect(input.$error.myRule).toEqual(true);
expect(child.$error.myRule).toEqual([input]);
expect(parent.$error.myRule).toEqual([child]);
input.$setValidity('myRule', true);
expect(parent.$error.myRule).toBe(false);
expect(child.$error.myRule).toBe(false);
});
});
describe('validation', function() {
beforeEach(function() {
doc = $compile(
'<form name="form">' +
'<input ng-model="name" name="name" store-model-ctrl/>' +
'</form>')(scope);
scope.$digest();
});
it('should have ng-valid/ng-invalid css class', function() {
expect(doc).toBeValid();
control.$setValidity('error', false);
expect(doc).toBeInvalid();
expect(doc.hasClass('ng-valid-error')).toBe(false);
expect(doc.hasClass('ng-invalid-error')).toBe(true);
control.$setValidity('another', false);
expect(doc.hasClass('ng-valid-error')).toBe(false);
expect(doc.hasClass('ng-invalid-error')).toBe(true);
expect(doc.hasClass('ng-valid-another')).toBe(false);
expect(doc.hasClass('ng-invalid-another')).toBe(true);
control.$setValidity('error', true);
expect(doc).toBeInvalid();
expect(doc.hasClass('ng-valid-error')).toBe(true);
expect(doc.hasClass('ng-invalid-error')).toBe(false);
expect(doc.hasClass('ng-valid-another')).toBe(false);
expect(doc.hasClass('ng-invalid-another')).toBe(true);
control.$setValidity('another', true);
expect(doc).toBeValid();
expect(doc.hasClass('ng-valid-error')).toBe(true);
expect(doc.hasClass('ng-invalid-error')).toBe(false);
expect(doc.hasClass('ng-valid-another')).toBe(true);
expect(doc.hasClass('ng-invalid-another')).toBe(false);
});
it('should have ng-pristine/ng-dirty css class', function() {
expect(doc).toBePristine();
control.$setViewValue('');
scope.$apply();
expect(doc).toBeDirty();
});
});
describe('$setPristine', function() {
it('should reset pristine state of form and controls', function() {
doc = $compile(
'<form name="testForm">' +
'<input ng-model="named1" name="foo">' +
'<input ng-model="named2" name="bar">' +
'</form>')(scope);
scope.$digest();
var form = doc,
formCtrl = scope.testForm,
input1 = form.find('input').eq(0),
input1Ctrl = input1.controller('ngModel'),
input2 = form.find('input').eq(1),
input2Ctrl = input2.controller('ngModel');
input1Ctrl.$setViewValue('xx');
input2Ctrl.$setViewValue('yy');
scope.$apply();
expect(form).toBeDirty();
expect(input1).toBeDirty();
expect(input2).toBeDirty();
formCtrl.$setPristine();
expect(form).toBePristine();
expect(formCtrl.$pristine).toBe(true);
expect(formCtrl.$dirty).toBe(false);
expect(input1).toBePristine();
expect(input1Ctrl.$pristine).toBe(true);
expect(input1Ctrl.$dirty).toBe(false);
expect(input2).toBePristine();
expect(input2Ctrl.$pristine).toBe(true);
expect(input2Ctrl.$dirty).toBe(false);
});
it('should reset pristine state of anonymous form controls', function() {
doc = $compile(
'<form name="testForm">' +
'<input ng-model="anonymous">' +
'</form>')(scope);
scope.$digest();
var form = doc,
formCtrl = scope.testForm,
input = form.find('input').eq(0),
inputCtrl = input.controller('ngModel');
inputCtrl.$setViewValue('xx');
scope.$apply();
expect(form).toBeDirty();
expect(input).toBeDirty();
formCtrl.$setPristine();
expect(form).toBePristine();
expect(formCtrl.$pristine).toBe(true);
expect(formCtrl.$dirty).toBe(false);
expect(input).toBePristine();
expect(inputCtrl.$pristine).toBe(true);
expect(inputCtrl.$dirty).toBe(false);
});
it('should reset pristine state of nested forms', function() {
doc = $compile(
'<form name="testForm">' +
'<div ng-form>' +
'<input ng-model="named" name="foo">' +
'</div>' +
'</form>')(scope);
scope.$digest();
var form = doc,
formCtrl = scope.testForm,
nestedForm = form.find('div'),
nestedFormCtrl = nestedForm.controller('form'),
nestedInput = form.find('input').eq(0),
nestedInputCtrl = nestedInput.controller('ngModel');
nestedInputCtrl.$setViewValue('xx');
scope.$apply();
expect(form).toBeDirty();
expect(nestedForm).toBeDirty();
expect(nestedInput).toBeDirty();
formCtrl.$setPristine();
expect(form).toBePristine();
expect(formCtrl.$pristine).toBe(true);
expect(formCtrl.$dirty).toBe(false);
expect(nestedForm).toBePristine();
expect(nestedFormCtrl.$pristine).toBe(true);
expect(nestedFormCtrl.$dirty).toBe(false);
expect(nestedInput).toBePristine();
expect(nestedInputCtrl.$pristine).toBe(true);
expect(nestedInputCtrl.$dirty).toBe(false);
});
});
describe('$setSubmitted', function() {
beforeEach(function() {
doc = $compile(
'<form name="form" ng-submit="submitted = true">' +
'<input type="text" ng-model="name" required />' +
'<input type="submit" />' +
'</form>')(scope);
scope.$digest();
});
it('should not init in submitted state', function() {
expect(scope.form.$submitted).toBe(false);
});
it('should be in submitted state when submitted', function() {
browserTrigger(doc, 'submit');
expect(scope.form.$submitted).toBe(true);
});
it('should revert submitted back to false when $setPristine is called on the form', function() {
scope.form.$submitted = true;
scope.form.$setPristine();
expect(scope.form.$submitted).toBe(false);
});
});
});
describe('form animations', function() {
beforeEach(module('ngAnimateMock'));
function assertValidAnimation(animation, event, classNameAdded, classNameRemoved) {
expect(animation.event).toBe(event);
expect(animation.args[1]).toBe(classNameAdded);
expect(animation.args[2]).toBe(classNameRemoved);
}
var doc, scope, form;
beforeEach(inject(function($rootScope, $compile, $rootElement, $animate) {
scope = $rootScope.$new();
doc = jqLite('<form name="myForm"></form>');
$rootElement.append(doc);
$compile(doc)(scope);
$animate.queue = [];
form = scope.myForm;
}));
afterEach(function() {
dealoc(doc);
});
it('should trigger an animation when invalid', inject(function($animate) {
form.$setValidity('required', false);
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-valid');
assertValidAnimation($animate.queue[1], 'addClass', 'ng-invalid');
assertValidAnimation($animate.queue[2], 'removeClass', 'ng-valid-required');
assertValidAnimation($animate.queue[3], 'addClass', 'ng-invalid-required');
}));
it('should trigger an animation when valid', inject(function($animate) {
form.$setValidity('required', false);
$animate.queue = [];
form.$setValidity('required', true);
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-invalid');
assertValidAnimation($animate.queue[1], 'addClass', 'ng-valid');
assertValidAnimation($animate.queue[2], 'removeClass', 'ng-invalid-required');
assertValidAnimation($animate.queue[3], 'addClass', 'ng-valid-required');
}));
it('should trigger an animation when dirty', inject(function($animate) {
form.$setDirty();
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-pristine');
assertValidAnimation($animate.queue[1], 'addClass', 'ng-dirty');
}));
it('should trigger an animation when pristine', inject(function($animate) {
form.$setDirty();
$animate.queue = [];
form.$setPristine();
assertValidAnimation($animate.queue[0], 'setClass', 'ng-pristine', 'ng-dirty ng-submitted');
}));
it('should trigger custom errors as addClass/removeClass when invalid/valid', inject(function($animate) {
form.$setValidity('custom-error', false);
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-valid');
assertValidAnimation($animate.queue[1], 'addClass', 'ng-invalid');
assertValidAnimation($animate.queue[2], 'removeClass', 'ng-valid-custom-error');
assertValidAnimation($animate.queue[3], 'addClass', 'ng-invalid-custom-error');
$animate.queue = [];
form.$setValidity('custom-error', true);
assertValidAnimation($animate.queue[0], 'removeClass', 'ng-invalid');
assertValidAnimation($animate.queue[1], 'addClass', 'ng-valid');
assertValidAnimation($animate.queue[2], 'removeClass', 'ng-invalid-custom-error');
assertValidAnimation($animate.queue[3], 'addClass', 'ng-valid-custom-error');
}));
});