forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdirectives_spec.ts
689 lines (556 loc) · 27.8 KB
/
directives_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
/**
* @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 {SimpleChange} from '@angular/core';
import {fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
import {AbstractControl, CheckboxControlValueAccessor, ControlValueAccessor, DefaultValueAccessor, FormArray, FormArrayName, FormControl, FormControlDirective, FormControlName, FormGroup, FormGroupDirective, FormGroupName, NgControl, NgForm, NgModel, NgModelGroup, SelectControlValueAccessor, SelectMultipleControlValueAccessor, ValidationErrors, Validator, Validators} from '@angular/forms';
import {composeValidators, selectValueAccessor} from '@angular/forms/src/directives/shared';
import {SpyNgControl, SpyValueAccessor} from './spies';
import {asyncValidator} from './util';
class DummyControlValueAccessor implements ControlValueAccessor {
writtenValue: any;
registerOnChange(fn: any) {}
registerOnTouched(fn: any) {}
writeValue(obj: any): void {
this.writtenValue = obj;
}
}
class CustomValidatorDirective implements Validator {
validate(c: FormControl): ValidationErrors {
return {'custom': true};
}
}
{
describe('Form Directives', () => {
let defaultAccessor: DefaultValueAccessor;
beforeEach(() => {
defaultAccessor = new DefaultValueAccessor(null!, null!, null!);
});
describe('shared', () => {
describe('selectValueAccessor', () => {
let dir: NgControl;
beforeEach(() => {
dir = <any>new SpyNgControl();
});
it('should throw when given an empty array', () => {
expect(() => selectValueAccessor(dir, [])).toThrowError();
});
it('should throw when accessor is not provided as array', () => {
expect(() => selectValueAccessor(dir, {} as any[]))
.toThrowError(
`Value accessor was not provided as an array for form control with unspecified name attribute`);
});
it('should return the default value accessor when no other provided', () => {
expect(selectValueAccessor(dir, [defaultAccessor])).toEqual(defaultAccessor);
});
it('should return checkbox accessor when provided', () => {
const checkboxAccessor = new CheckboxControlValueAccessor(null!, null!);
expect(selectValueAccessor(dir, [
defaultAccessor, checkboxAccessor
])).toEqual(checkboxAccessor);
});
it('should return select accessor when provided', () => {
const selectAccessor = new SelectControlValueAccessor(null!, null!);
expect(selectValueAccessor(dir, [
defaultAccessor, selectAccessor
])).toEqual(selectAccessor);
});
it('should return select multiple accessor when provided', () => {
const selectMultipleAccessor = new SelectMultipleControlValueAccessor(null!, null!);
expect(selectValueAccessor(dir, [
defaultAccessor, selectMultipleAccessor
])).toEqual(selectMultipleAccessor);
});
it('should throw when more than one build-in accessor is provided', () => {
const checkboxAccessor = new CheckboxControlValueAccessor(null!, null!);
const selectAccessor = new SelectControlValueAccessor(null!, null!);
expect(() => selectValueAccessor(dir, [checkboxAccessor, selectAccessor])).toThrowError();
});
it('should return custom accessor when provided', () => {
const customAccessor: ControlValueAccessor = new SpyValueAccessor() as any;
const checkboxAccessor = new CheckboxControlValueAccessor(null!, null!);
expect(selectValueAccessor(dir, <any>[
defaultAccessor, customAccessor, checkboxAccessor
])).toEqual(customAccessor);
});
it('should return custom accessor when provided with select multiple', () => {
const customAccessor: ControlValueAccessor = new SpyValueAccessor() as any;
const selectMultipleAccessor = new SelectMultipleControlValueAccessor(null!, null!);
expect(selectValueAccessor(dir, <any>[
defaultAccessor, customAccessor, selectMultipleAccessor
])).toEqual(customAccessor);
});
it('should throw when more than one custom accessor is provided', () => {
const customAccessor: ControlValueAccessor = <any>new SpyValueAccessor();
expect(() => selectValueAccessor(dir, [customAccessor, customAccessor])).toThrowError();
});
});
describe('composeValidators', () => {
it('should compose functions', () => {
const dummy1 = (_: any /** TODO #9100 */) => ({'dummy1': true});
const dummy2 = (_: any /** TODO #9100 */) => ({'dummy2': true});
const v = composeValidators([dummy1, dummy2])!;
expect(v(new FormControl(''))).toEqual({'dummy1': true, 'dummy2': true});
});
it('should compose validator directives', () => {
const dummy1 = (_: any /** TODO #9100 */) => ({'dummy1': true});
const v = composeValidators([dummy1, new CustomValidatorDirective()])!;
expect(v(new FormControl(''))).toEqual({'dummy1': true, 'custom': true});
});
});
});
describe('formGroup', () => {
let form: FormGroupDirective;
let formModel: FormGroup;
let loginControlDir: FormControlName;
beforeEach(() => {
form = new FormGroupDirective([], []);
formModel = new FormGroup({
'login': new FormControl(),
'passwords':
new FormGroup({'password': new FormControl(), 'passwordConfirm': new FormControl()})
});
form.form = formModel;
loginControlDir = new FormControlName(
form, [Validators.required], [asyncValidator('expected')], [defaultAccessor], null);
loginControlDir.name = 'login';
loginControlDir.valueAccessor = new DummyControlValueAccessor();
});
it('should reexport control properties', () => {
expect(form.control).toBe(formModel);
expect(form.value).toBe(formModel.value);
expect(form.valid).toBe(formModel.valid);
expect(form.invalid).toBe(formModel.invalid);
expect(form.pending).toBe(formModel.pending);
expect(form.errors).toBe(formModel.errors);
expect(form.pristine).toBe(formModel.pristine);
expect(form.dirty).toBe(formModel.dirty);
expect(form.touched).toBe(formModel.touched);
expect(form.untouched).toBe(formModel.untouched);
expect(form.statusChanges).toBe(formModel.statusChanges);
expect(form.valueChanges).toBe(formModel.valueChanges);
});
it('should reexport control methods', () => {
expect(form.hasError('required')).toBe(formModel.hasError('required'));
expect(form.getError('required')).toBe(formModel.getError('required'));
formModel.setErrors({required: true});
expect(form.hasError('required')).toBe(formModel.hasError('required'));
expect(form.getError('required')).toBe(formModel.getError('required'));
});
describe('addControl', () => {
it('should throw when no control found', () => {
const dir = new FormControlName(form, null!, null!, [defaultAccessor], null);
dir.name = 'invalidName';
expect(() => form.addControl(dir))
.toThrowError(new RegExp(`Cannot find control with name: 'invalidName'`));
});
it('should throw for a named control when no value accessor', () => {
const dir = new FormControlName(form, null!, null!, null!, null);
dir.name = 'login';
expect(() => form.addControl(dir))
.toThrowError(new RegExp(`No value accessor for form control with name: 'login'`));
});
it('should throw when no value accessor with path', () => {
const group = new FormGroupName(form, null!, null!);
const dir = new FormControlName(group, null!, null!, null!, null);
group.name = 'passwords';
dir.name = 'password';
expect(() => form.addControl(dir))
.toThrowError(new RegExp(
`No value accessor for form control with path: 'passwords -> password'`));
});
it('should set up validators', fakeAsync(() => {
form.addControl(loginControlDir);
// sync validators are set
expect(formModel.hasError('required', ['login'])).toBe(true);
expect(formModel.hasError('async', ['login'])).toBe(false);
(<FormControl>formModel.get('login')).setValue('invalid value');
// sync validator passes, running async validators
expect(formModel.pending).toBe(true);
tick();
expect(formModel.hasError('required', ['login'])).toBe(false);
expect(formModel.hasError('async', ['login'])).toBe(true);
}));
it('should write value to the DOM', () => {
(<FormControl>formModel.get(['login'])).setValue('initValue');
form.addControl(loginControlDir);
expect((<any>loginControlDir.valueAccessor).writtenValue).toEqual('initValue');
});
it('should add the directive to the list of directives included in the form', () => {
form.addControl(loginControlDir);
expect(form.directives).toEqual([loginControlDir]);
});
});
describe('addFormGroup', () => {
const matchingPasswordsValidator = (g: FormGroup) => {
if (g.controls['password'].value != g.controls['passwordConfirm'].value) {
return {'differentPasswords': true};
} else {
return null;
}
};
it('should set up validator', fakeAsync(() => {
const group = new FormGroupName(
form, [matchingPasswordsValidator], [asyncValidator('expected')]);
group.name = 'passwords';
form.addFormGroup(group);
(<FormControl>formModel.get(['passwords', 'password'])).setValue('somePassword');
(<FormControl>formModel.get([
'passwords', 'passwordConfirm'
])).setValue('someOtherPassword');
// sync validators are set
expect(formModel.hasError('differentPasswords', ['passwords'])).toEqual(true);
(<FormControl>formModel.get([
'passwords', 'passwordConfirm'
])).setValue('somePassword');
// sync validators pass, running async validators
expect(formModel.pending).toBe(true);
tick();
expect(formModel.hasError('async', ['passwords'])).toBe(true);
}));
});
describe('removeControl', () => {
it('should remove the directive to the list of directives included in the form', () => {
form.addControl(loginControlDir);
form.removeControl(loginControlDir);
expect(form.directives).toEqual([]);
});
});
describe('ngOnChanges', () => {
it('should update dom values of all the directives', () => {
form.addControl(loginControlDir);
(<FormControl>formModel.get(['login'])).setValue('new value');
form.ngOnChanges({});
expect((<any>loginControlDir.valueAccessor).writtenValue).toEqual('new value');
});
it('should set up a sync validator', () => {
const formValidator = (c: AbstractControl) => ({'custom': true});
const f = new FormGroupDirective([formValidator], []);
f.form = formModel;
f.ngOnChanges({'form': new SimpleChange(null, null, false)});
expect(formModel.errors).toEqual({'custom': true});
});
it('should set up an async validator', fakeAsync(() => {
const f = new FormGroupDirective([], [asyncValidator('expected')]);
f.form = formModel;
f.ngOnChanges({'form': new SimpleChange(null, null, false)});
tick();
expect(formModel.errors).toEqual({'async': true});
}));
});
});
describe('NgForm', () => {
let form: any /** TODO #9100 */;
let formModel: FormGroup;
let loginControlDir: any /** TODO #9100 */;
let personControlGroupDir: any /** TODO #9100 */;
beforeEach(() => {
form = new NgForm([], []);
formModel = form.form;
personControlGroupDir = new NgModelGroup(form, [], []);
personControlGroupDir.name = 'person';
loginControlDir = new NgModel(personControlGroupDir, null!, null!, [defaultAccessor]);
loginControlDir.name = 'login';
loginControlDir.valueAccessor = new DummyControlValueAccessor();
});
it('should reexport control properties', () => {
expect(form.control).toBe(formModel);
expect(form.value).toBe(formModel.value);
expect(form.valid).toBe(formModel.valid);
expect(form.invalid).toBe(formModel.invalid);
expect(form.pending).toBe(formModel.pending);
expect(form.errors).toBe(formModel.errors);
expect(form.pristine).toBe(formModel.pristine);
expect(form.dirty).toBe(formModel.dirty);
expect(form.touched).toBe(formModel.touched);
expect(form.untouched).toBe(formModel.untouched);
expect(form.statusChanges).toBe(formModel.statusChanges);
expect(form.status).toBe(formModel.status);
expect(form.valueChanges).toBe(formModel.valueChanges);
expect(form.disabled).toBe(formModel.disabled);
expect(form.enabled).toBe(formModel.enabled);
});
it('should reexport control methods', () => {
expect(form.hasError('required')).toBe(formModel.hasError('required'));
expect(form.getError('required')).toBe(formModel.getError('required'));
formModel.setErrors({required: true});
expect(form.hasError('required')).toBe(formModel.hasError('required'));
expect(form.getError('required')).toBe(formModel.getError('required'));
});
describe('addControl & addFormGroup', () => {
it('should create a control with the given name', fakeAsync(() => {
form.addFormGroup(personControlGroupDir);
form.addControl(loginControlDir);
flushMicrotasks();
expect(formModel.get(['person', 'login'])).not.toBeNull;
}));
// should update the form's value and validity
});
describe('removeControl & removeFormGroup', () => {
it('should remove control', fakeAsync(() => {
form.addFormGroup(personControlGroupDir);
form.addControl(loginControlDir);
form.removeFormGroup(personControlGroupDir);
form.removeControl(loginControlDir);
flushMicrotasks();
expect(formModel.get(['person'])).toBeNull();
expect(formModel.get(['person', 'login'])).toBeNull();
}));
// should update the form's value and validity
});
it('should set up sync validator', fakeAsync(() => {
const formValidator = (c: any /** TODO #9100 */) => ({'custom': true});
const f = new NgForm([formValidator], []);
tick();
expect(f.form.errors).toEqual({'custom': true});
}));
it('should set up async validator', fakeAsync(() => {
const f = new NgForm([], [asyncValidator('expected')]);
tick();
expect(f.form.errors).toEqual({'async': true});
}));
});
describe('FormGroupName', () => {
let formModel: any /** TODO #9100 */;
let controlGroupDir: any /** TODO #9100 */;
beforeEach(() => {
formModel = new FormGroup({'login': new FormControl(null)});
const parent = new FormGroupDirective([], []);
parent.form = new FormGroup({'group': formModel});
controlGroupDir = new FormGroupName(parent, [], []);
controlGroupDir.name = 'group';
});
it('should reexport control properties', () => {
expect(controlGroupDir.control).toBe(formModel);
expect(controlGroupDir.value).toBe(formModel.value);
expect(controlGroupDir.valid).toBe(formModel.valid);
expect(controlGroupDir.invalid).toBe(formModel.invalid);
expect(controlGroupDir.pending).toBe(formModel.pending);
expect(controlGroupDir.errors).toBe(formModel.errors);
expect(controlGroupDir.pristine).toBe(formModel.pristine);
expect(controlGroupDir.dirty).toBe(formModel.dirty);
expect(controlGroupDir.touched).toBe(formModel.touched);
expect(controlGroupDir.untouched).toBe(formModel.untouched);
expect(controlGroupDir.statusChanges).toBe(formModel.statusChanges);
expect(controlGroupDir.status).toBe(formModel.status);
expect(controlGroupDir.valueChanges).toBe(formModel.valueChanges);
expect(controlGroupDir.disabled).toBe(formModel.disabled);
expect(controlGroupDir.enabled).toBe(formModel.enabled);
});
it('should reexport control methods', () => {
expect(controlGroupDir.hasError('required')).toBe(formModel.hasError('required'));
expect(controlGroupDir.getError('required')).toBe(formModel.getError('required'));
formModel.setErrors({required: true});
expect(controlGroupDir.hasError('required')).toBe(formModel.hasError('required'));
expect(controlGroupDir.getError('required')).toBe(formModel.getError('required'));
});
});
describe('FormArrayName', () => {
let formModel: FormArray;
let formArrayDir: FormArrayName;
beforeEach(() => {
const parent = new FormGroupDirective([], []);
formModel = new FormArray([new FormControl('')]);
parent.form = new FormGroup({'array': formModel});
formArrayDir = new FormArrayName(parent, [], []);
formArrayDir.name = 'array';
});
it('should reexport control properties', () => {
expect(formArrayDir.control).toBe(formModel);
expect(formArrayDir.value).toBe(formModel.value);
expect(formArrayDir.valid).toBe(formModel.valid);
expect(formArrayDir.invalid).toBe(formModel.invalid);
expect(formArrayDir.pending).toBe(formModel.pending);
expect(formArrayDir.errors).toBe(formModel.errors);
expect(formArrayDir.pristine).toBe(formModel.pristine);
expect(formArrayDir.dirty).toBe(formModel.dirty);
expect(formArrayDir.touched).toBe(formModel.touched);
expect(formArrayDir.status).toBe(formModel.status);
expect(formArrayDir.untouched).toBe(formModel.untouched);
expect(formArrayDir.disabled).toBe(formModel.disabled);
expect(formArrayDir.enabled).toBe(formModel.enabled);
});
it('should reexport control methods', () => {
expect(formArrayDir.hasError('required')).toBe(formModel.hasError('required'));
expect(formArrayDir.getError('required')).toBe(formModel.getError('required'));
formModel.setErrors({required: true});
expect(formArrayDir.hasError('required')).toBe(formModel.hasError('required'));
expect(formArrayDir.getError('required')).toBe(formModel.getError('required'));
});
});
describe('FormControlDirective', () => {
let controlDir: any /** TODO #9100 */;
let control: any /** TODO #9100 */;
const checkProperties = function(control: AbstractControl) {
expect(controlDir.control).toBe(control);
expect(controlDir.value).toBe(control.value);
expect(controlDir.valid).toBe(control.valid);
expect(controlDir.invalid).toBe(control.invalid);
expect(controlDir.pending).toBe(control.pending);
expect(controlDir.errors).toBe(control.errors);
expect(controlDir.pristine).toBe(control.pristine);
expect(controlDir.dirty).toBe(control.dirty);
expect(controlDir.touched).toBe(control.touched);
expect(controlDir.untouched).toBe(control.untouched);
expect(controlDir.statusChanges).toBe(control.statusChanges);
expect(controlDir.status).toBe(control.status);
expect(controlDir.valueChanges).toBe(control.valueChanges);
expect(controlDir.disabled).toBe(control.disabled);
expect(controlDir.enabled).toBe(control.enabled);
};
beforeEach(() => {
controlDir = new FormControlDirective([Validators.required], [], [defaultAccessor], null);
controlDir.valueAccessor = new DummyControlValueAccessor();
control = new FormControl(null);
controlDir.form = control;
});
it('should reexport control properties', () => {
checkProperties(control);
});
it('should reexport control methods', () => {
expect(controlDir.hasError('required')).toBe(control.hasError('required'));
expect(controlDir.getError('required')).toBe(control.getError('required'));
control.setErrors({required: true});
expect(controlDir.hasError('required')).toBe(control.hasError('required'));
expect(controlDir.getError('required')).toBe(control.getError('required'));
});
it('should reexport new control properties', () => {
const newControl = new FormControl(null);
controlDir.form = newControl;
controlDir.ngOnChanges({'form': new SimpleChange(control, newControl, false)});
checkProperties(newControl);
});
it('should set up validator', () => {
expect(control.valid).toBe(true);
// this will add the required validator and recalculate the validity
controlDir.ngOnChanges({'form': new SimpleChange(null, control, false)});
expect(control.valid).toBe(false);
});
});
describe('NgModel', () => {
let ngModel: NgModel;
let control: FormControl;
beforeEach(() => {
ngModel = new NgModel(
null!, [Validators.required], [asyncValidator('expected')], [defaultAccessor]);
ngModel.valueAccessor = new DummyControlValueAccessor();
control = ngModel.control;
});
it('should reexport control properties', () => {
expect(ngModel.control).toBe(control);
expect(ngModel.value).toBe(control.value);
expect(ngModel.valid).toBe(control.valid);
expect(ngModel.invalid).toBe(control.invalid);
expect(ngModel.pending).toBe(control.pending);
expect(ngModel.errors).toBe(control.errors);
expect(ngModel.pristine).toBe(control.pristine);
expect(ngModel.dirty).toBe(control.dirty);
expect(ngModel.touched).toBe(control.touched);
expect(ngModel.untouched).toBe(control.untouched);
expect(ngModel.statusChanges).toBe(control.statusChanges);
expect(ngModel.status).toBe(control.status);
expect(ngModel.valueChanges).toBe(control.valueChanges);
expect(ngModel.disabled).toBe(control.disabled);
expect(ngModel.enabled).toBe(control.enabled);
});
it('should reexport control methods', () => {
expect(ngModel.hasError('required')).toBe(control.hasError('required'));
expect(ngModel.getError('required')).toBe(control.getError('required'));
control.setErrors({required: true});
expect(ngModel.hasError('required')).toBe(control.hasError('required'));
expect(ngModel.getError('required')).toBe(control.getError('required'));
});
it('should throw when no value accessor with named control', () => {
const namedDir = new NgModel(null!, null!, null!, null!);
namedDir.name = 'one';
expect(() => namedDir.ngOnChanges({}))
.toThrowError(new RegExp(`No value accessor for form control with name: 'one'`));
});
it('should throw when no value accessor with unnamed control', () => {
const unnamedDir = new NgModel(null!, null!, null!, null!);
expect(() => unnamedDir.ngOnChanges({}))
.toThrowError(
new RegExp(`No value accessor for form control with unspecified name attribute`));
});
it('should set up validator', fakeAsync(() => {
// this will add the required validator and recalculate the validity
ngModel.ngOnChanges({});
tick();
expect(ngModel.control.errors).toEqual({'required': true});
ngModel.control.setValue('someValue');
tick();
expect(ngModel.control.errors).toEqual({'async': true});
}));
it('should mark as disabled properly', fakeAsync(() => {
ngModel.ngOnChanges({isDisabled: new SimpleChange('', undefined, false)});
tick();
expect(ngModel.control.disabled).toEqual(false);
ngModel.ngOnChanges({isDisabled: new SimpleChange('', null, false)});
tick();
expect(ngModel.control.disabled).toEqual(false);
ngModel.ngOnChanges({isDisabled: new SimpleChange('', false, false)});
tick();
expect(ngModel.control.disabled).toEqual(false);
ngModel.ngOnChanges({isDisabled: new SimpleChange('', 'false', false)});
tick();
expect(ngModel.control.disabled).toEqual(false);
ngModel.ngOnChanges({isDisabled: new SimpleChange('', 0, false)});
tick();
expect(ngModel.control.disabled).toEqual(false);
ngModel.ngOnChanges({isDisabled: new SimpleChange(null, '', false)});
tick();
expect(ngModel.control.disabled).toEqual(true);
ngModel.ngOnChanges({isDisabled: new SimpleChange(null, 'true', false)});
tick();
expect(ngModel.control.disabled).toEqual(true);
ngModel.ngOnChanges({isDisabled: new SimpleChange(null, true, false)});
tick();
expect(ngModel.control.disabled).toEqual(true);
ngModel.ngOnChanges({isDisabled: new SimpleChange(null, 'anything else', false)});
tick();
expect(ngModel.control.disabled).toEqual(true);
}));
});
describe('FormControlName', () => {
let formModel: any /** TODO #9100 */;
let controlNameDir: any /** TODO #9100 */;
beforeEach(() => {
formModel = new FormControl('name');
const parent = new FormGroupDirective([], []);
parent.form = new FormGroup({'name': formModel});
controlNameDir = new FormControlName(parent, [], [], [defaultAccessor], null);
controlNameDir.name = 'name';
(controlNameDir as {control: FormControl}).control = formModel;
});
it('should reexport control properties', () => {
expect(controlNameDir.control).toBe(formModel);
expect(controlNameDir.value).toBe(formModel.value);
expect(controlNameDir.valid).toBe(formModel.valid);
expect(controlNameDir.invalid).toBe(formModel.invalid);
expect(controlNameDir.pending).toBe(formModel.pending);
expect(controlNameDir.errors).toBe(formModel.errors);
expect(controlNameDir.pristine).toBe(formModel.pristine);
expect(controlNameDir.dirty).toBe(formModel.dirty);
expect(controlNameDir.touched).toBe(formModel.touched);
expect(controlNameDir.untouched).toBe(formModel.untouched);
expect(controlNameDir.statusChanges).toBe(formModel.statusChanges);
expect(controlNameDir.status).toBe(formModel.status);
expect(controlNameDir.valueChanges).toBe(formModel.valueChanges);
expect(controlNameDir.disabled).toBe(formModel.disabled);
expect(controlNameDir.enabled).toBe(formModel.enabled);
});
it('should reexport control methods', () => {
expect(controlNameDir.hasError('required')).toBe(formModel.hasError('required'));
expect(controlNameDir.getError('required')).toBe(formModel.getError('required'));
formModel.setErrors({required: true});
expect(controlNameDir.hasError('required')).toBe(formModel.hasError('required'));
expect(controlNameDir.getError('required')).toBe(formModel.getError('required'));
});
});
});
}