/
form_control_spec.ts
1628 lines (1258 loc) Β· 50.1 KB
/
form_control_spec.ts
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {fakeAsync, tick} from '@angular/core/testing';
import {FormArray, FormControl, FormGroup, Validators} from '@angular/forms';
import {asyncValidator, asyncValidatorReturningObservable} from './util';
(function() {
function otherAsyncValidator() {
return Promise.resolve({'other': true});
}
function syncValidator(_: any /** TODO #9100 */): any /** TODO #9100 */ {
return null;
}
describe('FormControl', () => {
it('should default the value to null', () => {
const c = new FormControl();
expect(c.value).toBe(null);
});
describe('markAllAsTouched', () => {
it('should mark only the control itself as touched', () => {
const control = new FormControl('');
expect(control.touched).toBe(false);
control.markAllAsTouched();
expect(control.touched).toBe(true);
});
});
describe('boxed values', () => {
it('should support valid boxed values on creation', () => {
const c = new FormControl({value: 'some val', disabled: true}, null!, null!);
expect(c.disabled).toBe(true);
expect(c.value).toBe('some val');
expect(c.status).toBe('DISABLED');
});
it('should not treat objects as boxed values when `disabled` field is present, but `value` is missing',
() => {
const c = new FormControl({disabled: true});
expect(c.value).toEqual({disabled: true});
expect(c.disabled).toBe(false);
});
it('should honor boxed value with disabled control when validating', () => {
const c = new FormControl({value: '', disabled: true}, Validators.required);
expect(c.disabled).toBe(true);
expect(c.valid).toBe(false);
expect(c.status).toBe('DISABLED');
});
it('should not treat objects as boxed values if they have more than two props', () => {
const c: FormControl =
new FormControl({value: '', disabled: true, test: 'test'} as any, null!, null!);
expect(c.value).toEqual({value: '', disabled: true, test: 'test'});
expect(c.disabled).toBe(false);
});
it('should not treat objects as boxed values if disabled is missing', () => {
const c = new FormControl({value: '', test: 'test'}, null!, null!);
expect(c.value).toEqual({value: '', test: 'test'});
expect(c.disabled).toBe(false);
});
});
describe('updateOn', () => {
it('should default to on change', () => {
const c = new FormControl('');
expect(c.updateOn).toEqual('change');
});
it('should default to on change with an options obj', () => {
const c = new FormControl('', {validators: Validators.required});
expect(c.updateOn).toEqual('change');
});
it('should set updateOn when updating on blur', () => {
const c = new FormControl('', {updateOn: 'blur'});
expect(c.updateOn).toEqual('blur');
});
describe('in groups and arrays', () => {
it('should default to group updateOn when not set in control', () => {
const g =
new FormGroup({one: new FormControl(), two: new FormControl()}, {updateOn: 'blur'});
expect(g.get('one')!.updateOn).toEqual('blur');
expect(g.get('two')!.updateOn).toEqual('blur');
});
it('should default to array updateOn when not set in control', () => {
const a = new FormArray([new FormControl(), new FormControl()], {updateOn: 'blur'});
expect(a.get([0])!.updateOn).toEqual('blur');
expect(a.get([1])!.updateOn).toEqual('blur');
});
it('should set updateOn with nested groups', () => {
const g = new FormGroup(
{
group: new FormGroup({one: new FormControl(), two: new FormControl()}),
},
{updateOn: 'blur'});
expect(g.get('group.one')!.updateOn).toEqual('blur');
expect(g.get('group.two')!.updateOn).toEqual('blur');
expect(g.get('group')!.updateOn).toEqual('blur');
});
it('should set updateOn with nested arrays', () => {
const g = new FormGroup(
{
arr: new FormArray([new FormControl(), new FormControl()]),
},
{updateOn: 'blur'});
expect(g.get(['arr', 0])!.updateOn).toEqual('blur');
expect(g.get(['arr', 1])!.updateOn).toEqual('blur');
expect(g.get('arr')!.updateOn).toEqual('blur');
});
it('should allow control updateOn to override group updateOn', () => {
const g = new FormGroup(
{one: new FormControl('', {updateOn: 'change'}), two: new FormControl()},
{updateOn: 'blur'});
expect(g.get('one')!.updateOn).toEqual('change');
expect(g.get('two')!.updateOn).toEqual('blur');
});
it('should set updateOn with complex setup', () => {
const g = new FormGroup({
group: new FormGroup(
{one: new FormControl('', {updateOn: 'change'}), two: new FormControl()},
{updateOn: 'blur'}),
groupTwo: new FormGroup({one: new FormControl()}, {updateOn: 'submit'}),
three: new FormControl()
});
expect(g.get('group.one')!.updateOn).toEqual('change');
expect(g.get('group.two')!.updateOn).toEqual('blur');
expect(g.get('groupTwo.one')!.updateOn).toEqual('submit');
expect(g.get('three')!.updateOn).toEqual('change');
});
});
});
describe('validator', () => {
it('should run validator with the initial value', () => {
const c = new FormControl('value', Validators.required);
expect(c.valid).toEqual(true);
});
it('should rerun the validator when the value changes', () => {
const c = new FormControl('value', Validators.required);
c.setValue(null);
expect(c.valid).toEqual(false);
});
it('should support arrays of validator functions if passed', () => {
const c = new FormControl('value', [Validators.required, Validators.minLength(3)]);
c.setValue('a');
expect(c.valid).toEqual(false);
c.setValue('aaa');
expect(c.valid).toEqual(true);
});
it('should support single validator from options obj', () => {
const c: FormControl = new FormControl(null, {validators: Validators.required});
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({required: true});
c.setValue('value');
expect(c.valid).toEqual(true);
});
it('should support multiple validators from options obj', () => {
const c: FormControl =
new FormControl(null, {validators: [Validators.required, Validators.minLength(3)]});
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({required: true});
c.setValue('aa');
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({minlength: {requiredLength: 3, actualLength: 2}});
c.setValue('aaa');
expect(c.valid).toEqual(true);
});
it('should support a null validators value', () => {
const c = new FormControl(null, {validators: null});
expect(c.valid).toEqual(true);
});
it('should support an empty options obj', () => {
const c = new FormControl(null, {});
expect(c.valid).toEqual(true);
});
it('should return errors', () => {
const c = new FormControl(null, Validators.required);
expect(c.errors).toEqual({'required': true});
});
it('should set single validator', () => {
const c: FormControl = new FormControl(null);
expect(c.valid).toEqual(true);
c.setValidators(Validators.required);
c.setValue(null);
expect(c.valid).toEqual(false);
c.setValue('abc');
expect(c.valid).toEqual(true);
});
it('should set multiple validators from array', () => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
c.setValidators([Validators.minLength(5), Validators.required]);
c.setValue('');
expect(c.valid).toEqual(false);
c.setValue('abc');
expect(c.valid).toEqual(false);
c.setValue('abcde');
expect(c.valid).toEqual(true);
});
it('should override validators using `setValidators` function', () => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
c.setValidators([Validators.minLength(5), Validators.required]);
c.setValue('');
expect(c.valid).toEqual(false);
c.setValue('abc');
expect(c.valid).toEqual(false);
c.setValue('abcde');
expect(c.valid).toEqual(true);
// Define new set of validators, overriding previously applied ones.
c.setValidators([Validators.maxLength(2)]);
c.setValue('abcdef');
expect(c.valid).toEqual(false);
c.setValue('a');
expect(c.valid).toEqual(true);
});
it('should not mutate the validators array when overriding using setValidators', () => {
const control = new FormControl('');
const originalValidators = [Validators.required];
control.setValidators(originalValidators);
control.addValidators(Validators.minLength(10));
expect(originalValidators.length).toBe(1);
});
it('should override validators by setting `control.validator` field value', () => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
// Define new set of validators, overriding previously applied ones.
c.validator = Validators.compose([Validators.minLength(5), Validators.required]);
c.setValue('');
expect(c.valid).toEqual(false);
c.setValue('abc');
expect(c.valid).toEqual(false);
c.setValue('abcde');
expect(c.valid).toEqual(true);
// Define new set of validators, overriding previously applied ones.
c.validator = Validators.compose([Validators.maxLength(2)]);
c.setValue('abcdef');
expect(c.valid).toEqual(false);
c.setValue('a');
expect(c.valid).toEqual(true);
});
it('should clear validators', () => {
const c = new FormControl('', Validators.required);
expect(c.valid).toEqual(false);
c.clearValidators();
expect(c.validator).toEqual(null);
c.setValue('');
expect(c.valid).toEqual(true);
});
it('should add after clearing', () => {
const c = new FormControl('', Validators.required);
expect(c.valid).toEqual(false);
c.clearValidators();
expect(c.validator).toEqual(null);
c.setValidators([Validators.required]);
expect(c.validator).not.toBe(null);
});
it('should check presence of and remove a validator set in the control constructor', () => {
const c = new FormControl('', Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(true);
c.removeValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(false);
c.addValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(true);
});
it('should check presence of and remove a validator set with addValidators', () => {
const c = new FormControl('');
expect(c.hasValidator(Validators.required)).toEqual(false);
c.addValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(true);
c.removeValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(false);
});
it('should check presence of and remove multiple validators set at the same time', () => {
const c = new FormControl('3');
const minValidator = Validators.min(4);
c.addValidators([Validators.required, minValidator]);
expect(c.hasValidator(Validators.required)).toEqual(true);
expect(c.hasValidator(minValidator)).toEqual(true);
c.removeValidators([Validators.required, minValidator]);
expect(c.hasValidator(Validators.required)).toEqual(false);
expect(c.hasValidator(minValidator)).toEqual(false);
});
it('should be able to remove a validator added multiple times', () => {
const c = new FormControl('', Validators.required);
c.addValidators(Validators.required);
c.addValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(true);
c.removeValidators(Validators.required);
expect(c.hasValidator(Validators.required)).toEqual(false);
});
it('should not mutate the validators array when adding/removing sync validators', () => {
const originalValidators = [Validators.required];
const control = new FormControl('', originalValidators);
control.addValidators(Validators.min(10));
expect(originalValidators.length).toBe(1);
control.removeValidators(Validators.required);
expect(originalValidators.length).toBe(1);
});
it('should not mutate the validators array when adding/removing async validators', () => {
const firstValidator = asyncValidator('one');
const secondValidator = asyncValidator('two');
const originalValidators = [firstValidator];
const control = new FormControl('', null, originalValidators);
control.addAsyncValidators(secondValidator);
expect(originalValidators.length).toBe(1);
control.removeAsyncValidators(firstValidator);
expect(originalValidators.length).toBe(1);
});
it('should return false when checking presence of a validator not identical by reference',
() => {
const minValidator = Validators.min(5);
const c = new FormControl('1', minValidator);
expect(c.hasValidator(minValidator)).toEqual(true);
expect(c.hasValidator(Validators.min(5))).toEqual(false);
});
});
describe('asyncValidator', () => {
it('should run validator with the initial value', fakeAsync(() => {
const c = new FormControl('value', null!, asyncValidator('expected'));
tick();
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({'async': true});
}));
it('should support validators returning observables', fakeAsync(() => {
const c = new FormControl('value', null!, asyncValidatorReturningObservable);
tick();
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({'async': true});
}));
it('should rerun the validator when the value changes', fakeAsync(() => {
const c = new FormControl('value', null!, asyncValidator('expected'));
c.setValue('expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should run the async validator only when the sync validator passes', fakeAsync(() => {
const c = new FormControl('', Validators.required, asyncValidator('expected'));
tick();
expect(c.errors).toEqual({'required': true});
c.setValue('some value');
tick();
expect(c.errors).toEqual({'async': true});
}));
it('should mark the control as pending while running the async validation', fakeAsync(() => {
const c = new FormControl('', null!, asyncValidator('expected'));
expect(c.pending).toEqual(true);
tick();
expect(c.pending).toEqual(false);
}));
it('should only use the latest async validation run', fakeAsync(() => {
const c =
new FormControl('', null!, asyncValidator('expected', {'long': 200, 'expected': 100}));
c.setValue('long');
c.setValue('expected');
tick(300);
expect(c.valid).toEqual(true);
}));
it('should support arrays of async validator functions if passed', fakeAsync(() => {
const c =
new FormControl('value', null!, [asyncValidator('expected'), otherAsyncValidator]);
tick();
expect(c.errors).toEqual({'async': true, 'other': true});
}));
it('should support a single async validator from options obj', fakeAsync(() => {
const c = new FormControl('value', {asyncValidators: asyncValidator('expected')});
expect(c.pending).toEqual(true);
tick();
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({'async': true});
}));
it('should support multiple async validators from options obj', fakeAsync(() => {
const c = new FormControl(
'value', {asyncValidators: [asyncValidator('expected'), otherAsyncValidator]});
expect(c.pending).toEqual(true);
tick();
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({'async': true, 'other': true});
}));
it('should support a mix of validators from options obj', fakeAsync(() => {
const c = new FormControl(
'', {validators: Validators.required, asyncValidators: asyncValidator('expected')});
tick();
expect(c.errors).toEqual({required: true});
c.setValue('value');
expect(c.pending).toBe(true);
tick();
expect(c.valid).toEqual(false);
expect(c.errors).toEqual({'async': true});
}));
it('should add single async validator', fakeAsync(() => {
const c = new FormControl('value', null!);
c.setAsyncValidators(asyncValidator('expected'));
expect(c.asyncValidator).not.toEqual(null);
c.setValue('expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should add async validator from array', fakeAsync(() => {
const c = new FormControl('value', null!);
c.setAsyncValidators([asyncValidator('expected')]);
expect(c.asyncValidator).not.toEqual(null);
c.setValue('expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should override validators using `setAsyncValidators` function', fakeAsync(() => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
c.setAsyncValidators([asyncValidator('expected')]);
c.setValue('');
tick();
expect(c.valid).toEqual(false);
c.setValue('expected');
tick();
expect(c.valid).toEqual(true);
// Define new set of validators, overriding previously applied ones.
c.setAsyncValidators([asyncValidator('new expected')]);
c.setValue('expected');
tick();
expect(c.valid).toEqual(false);
c.setValue('new expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should not mutate the validators array when overriding using setValidators', () => {
const control = new FormControl('');
const originalValidators = [asyncValidator('one')];
control.setAsyncValidators(originalValidators);
control.addAsyncValidators(asyncValidator('two'));
expect(originalValidators.length).toBe(1);
});
it('should override validators by setting `control.asyncValidator` field value',
fakeAsync(() => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
c.asyncValidator = Validators.composeAsync([asyncValidator('expected')]);
c.setValue('');
tick();
expect(c.valid).toEqual(false);
c.setValue('expected');
tick();
expect(c.valid).toEqual(true);
// Define new set of validators, overriding previously applied ones.
c.asyncValidator = Validators.composeAsync([asyncValidator('new expected')]);
c.setValue('expected');
tick();
expect(c.valid).toEqual(false);
c.setValue('new expected');
tick();
expect(c.valid).toEqual(true);
}));
it('should clear async validators', fakeAsync(() => {
const c = new FormControl('value', [asyncValidator('expected'), otherAsyncValidator]);
c.clearValidators();
expect(c.asyncValidator).toEqual(null);
}));
it('should not change validity state if control is disabled while async validating',
fakeAsync(() => {
const c = new FormControl('value', [asyncValidator('expected')]);
c.disable();
tick();
expect(c.status).toEqual('DISABLED');
}));
it('should check presence of and remove a validator set in the control constructor', () => {
const asyncVal = asyncValidator('expected');
const c = new FormControl('', null, asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(true);
c.removeAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(false);
c.addAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(true);
});
it('should check presence of and remove a validator set with addValidators', () => {
const c = new FormControl('');
const asyncVal = asyncValidator('expected');
c.addAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(true);
c.removeAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(false);
});
it('should check presence of and remove multiple validators set at the same time', () => {
const c = new FormControl('3');
const asyncVal1 = asyncValidator('expected1');
const asyncVal2 = asyncValidator('expected2');
c.addAsyncValidators([asyncVal1, asyncVal2]);
expect(c.hasAsyncValidator(asyncVal1)).toEqual(true);
expect(c.hasAsyncValidator(asyncVal2)).toEqual(true);
c.removeAsyncValidators([asyncVal1, asyncVal2]);
expect(c.hasAsyncValidator(asyncVal1)).toEqual(false);
expect(c.hasAsyncValidator(asyncVal2)).toEqual(false);
});
it('should be able to remove a validator added multiple times', () => {
const asyncVal = asyncValidator('expected');
const c = new FormControl('', null, asyncVal);
c.addAsyncValidators(asyncVal);
c.addAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(true);
c.removeAsyncValidators(asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(false);
});
it('should return false when checking presence of a validator not identical by reference',
() => {
const asyncVal = asyncValidator('expected');
const asyncValDifferentFn = asyncValidator('expected');
const c = new FormControl('1', null, asyncVal);
expect(c.hasAsyncValidator(asyncVal)).toEqual(true);
expect(c.hasAsyncValidator(asyncValDifferentFn)).toEqual(false);
});
});
describe('dirty', () => {
it('should be false after creating a control', () => {
const c = new FormControl('value');
expect(c.dirty).toEqual(false);
});
it('should be true after changing the value of the control', () => {
const c = new FormControl('value');
c.markAsDirty();
expect(c.dirty).toEqual(true);
});
});
describe('touched', () => {
it('should be false after creating a control', () => {
const c = new FormControl('value');
expect(c.touched).toEqual(false);
});
it('should be true after markAsTouched runs', () => {
const c = new FormControl('value');
c.markAsTouched();
expect(c.touched).toEqual(true);
});
});
describe('setValue', () => {
let g: FormGroup, c: FormControl;
beforeEach(() => {
c = new FormControl('oldValue');
g = new FormGroup({'one': c});
});
it('should set the value of the control', () => {
c.setValue('newValue');
expect(c.value).toEqual('newValue');
});
it('should invoke ngOnChanges if it is present', () => {
let ngOnChanges: any;
c.registerOnChange((v: any) => ngOnChanges = ['invoked', v]);
c.setValue('newValue');
expect(ngOnChanges).toEqual(['invoked', 'newValue']);
});
it('should not invoke on change when explicitly specified', () => {
let onChange: any = null;
c.registerOnChange((v: any) => onChange = ['invoked', v]);
c.setValue('newValue', {emitModelToViewChange: false});
expect(onChange).toBeNull();
});
it('should set the parent', () => {
c.setValue('newValue');
expect(g.value).toEqual({'one': 'newValue'});
});
it('should not set the parent when explicitly specified', () => {
c.setValue('newValue', {onlySelf: true});
expect(g.value).toEqual({'one': 'oldValue'});
});
it('should fire an event', fakeAsync(() => {
c.valueChanges.subscribe((value) => {
expect(value).toEqual('newValue');
});
c.setValue('newValue');
tick();
}));
it('should not fire an event when explicitly specified', fakeAsync(() => {
c.valueChanges.subscribe((value) => {
throw 'Should not happen';
});
c.setValue('newValue', {emitEvent: false});
tick();
}));
it('should work on a disabled control', () => {
g.addControl('two', new FormControl('two'));
c.disable();
c.setValue('new value');
expect(c.value).toEqual('new value');
expect(g.value).toEqual({'two': 'two'});
});
});
describe('patchValue', () => {
let g: FormGroup, c: FormControl;
beforeEach(() => {
c = new FormControl('oldValue');
g = new FormGroup({'one': c});
});
it('should set the value of the control', () => {
c.patchValue('newValue');
expect(c.value).toEqual('newValue');
});
it('should invoke ngOnChanges if it is present', () => {
let ngOnChanges: any;
c.registerOnChange((v: any) => ngOnChanges = ['invoked', v]);
c.patchValue('newValue');
expect(ngOnChanges).toEqual(['invoked', 'newValue']);
});
it('should not invoke on change when explicitly specified', () => {
let onChange: any = null;
c.registerOnChange((v: any) => onChange = ['invoked', v]);
c.patchValue('newValue', {emitModelToViewChange: false});
expect(onChange).toBeNull();
});
it('should set the parent', () => {
c.patchValue('newValue');
expect(g.value).toEqual({'one': 'newValue'});
});
it('should not set the parent when explicitly specified', () => {
c.patchValue('newValue', {onlySelf: true});
expect(g.value).toEqual({'one': 'oldValue'});
});
it('should fire an event', fakeAsync(() => {
c.valueChanges.subscribe((value) => {
expect(value).toEqual('newValue');
});
c.patchValue('newValue');
tick();
}));
it('should not fire an event when explicitly specified', fakeAsync(() => {
c.valueChanges.subscribe((value) => {
throw 'Should not happen';
});
c.patchValue('newValue', {emitEvent: false});
tick();
}));
it('should patch value on a disabled control', () => {
g.addControl('two', new FormControl('two'));
c.disable();
c.patchValue('new value');
expect(c.value).toEqual('new value');
expect(g.value).toEqual({'two': 'two'});
});
});
describe('reset()', () => {
let c: FormControl;
beforeEach(() => {
c = new FormControl('initial value');
});
it('should reset to a specific value if passed', () => {
c.setValue('new value');
expect(c.value).toBe('new value');
c.reset('initial value');
expect(c.value).toBe('initial value');
});
it('should not set the parent when explicitly specified', () => {
const g = new FormGroup({'one': c});
c.patchValue('newValue', {onlySelf: true});
expect(g.value).toEqual({'one': 'initial value'});
});
it('should reset to a specific value if passed with boxed value', () => {
c.setValue('new value');
expect(c.value).toBe('new value');
c.reset({value: 'initial value', disabled: false});
expect(c.value).toBe('initial value');
});
it('should clear the control value if no value is passed', () => {
c.setValue('new value');
expect(c.value).toBe('new value');
c.reset();
expect(c.value).toBe(null);
});
it('should reset to the initial value if specified in FormControlOptions', () => {
const c2 = new FormControl('foo', {nonNullable: true});
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
c2.setValue('bar');
expect(c2.value).toBe('bar');
expect(c2.defaultValue).toBe('foo');
c2.reset();
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
const c3 = new FormControl('foo', {nonNullable: false});
expect(c3.value).toBe('foo');
expect(c3.defaultValue).toBe(null);
c3.setValue('bar');
expect(c3.value).toBe('bar');
expect(c3.defaultValue).toBe(null);
c3.reset();
expect(c3.value).toBe(null);
expect(c3.defaultValue).toBe(null);
});
it('should look inside FormState objects for a default value', () => {
const c2 = new FormControl({value: 'foo', disabled: false}, {initialValueIsDefault: true});
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
c2.setValue('bar');
expect(c2.value).toBe('bar');
expect(c2.defaultValue).toBe('foo');
c2.reset();
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
});
it('should not alter the disabled state when resetting, even if a default value is provided',
() => {
const c2 = new FormControl({value: 'foo', disabled: true}, {nonNullable: true});
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
expect(c2.disabled).toBe(true);
c2.setValue('bar');
c2.enable();
c2.reset();
expect(c2.value).toBe('foo');
expect(c2.defaultValue).toBe('foo');
expect(c2.disabled).toBe(false);
});
it('should update the value of any parent controls with passed value', () => {
const g = new FormGroup({'one': c});
c.setValue('new value');
expect(g.value).toEqual({'one': 'new value'});
c.reset('initial value');
expect(g.value).toEqual({'one': 'initial value'});
});
it('should update the value of any parent controls with null value', () => {
const g = new FormGroup({'one': c});
c.setValue('new value');
expect(g.value).toEqual({'one': 'new value'});
c.reset();
expect(g.value).toEqual({'one': null});
});
it('should mark the control as pristine', () => {
c.markAsDirty();
expect(c.pristine).toBe(false);
c.reset();
expect(c.pristine).toBe(true);
});
it('should set the parent pristine state if all pristine', () => {
const g = new FormGroup({'one': c});
c.markAsDirty();
expect(g.pristine).toBe(false);
c.reset();
expect(g.pristine).toBe(true);
});
it('should not set the parent pristine state if it has other dirty controls', () => {
const c2 = new FormControl('two');
const g = new FormGroup({'one': c, 'two': c2});
c.markAsDirty();
c2.markAsDirty();
c.reset();
expect(g.pristine).toBe(false);
});
it('should mark the control as untouched', () => {
c.markAsTouched();
expect(c.untouched).toBe(false);
c.reset();
expect(c.untouched).toBe(true);
});
it('should set the parent untouched state if all untouched', () => {
const g = new FormGroup({'one': c});
c.markAsTouched();
expect(g.untouched).toBe(false);
c.reset();
expect(g.untouched).toBe(true);
});
it('should not set the parent untouched state if other touched controls', () => {
const c2 = new FormControl('two');
const g = new FormGroup({'one': c, 'two': c2});
c.markAsTouched();
c2.markAsTouched();
c.reset();
expect(g.untouched).toBe(false);
});
it('should retain the disabled state of the control', () => {
c.disable();
c.reset();
expect(c.disabled).toBe(true);
});
it('should set disabled state based on boxed value if passed', () => {
c.disable();
c.reset({value: null, disabled: false});