mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
test(forms): Add more tests for FormBuilder method argument shapes. (#44452)
It is possible to pass arguments to `FormBuilder` using four different formats: value-only, boxed value, control config, and value-array. Currently, these different methods are not well-tested, especially as they interact. This PR will add tests for the variety of different argument shapes. This was originally inspired by typed forms: when `FormBuilder` becomes typed, all these argument shapes should just work, with correct inferred types. PR Close #44452
This commit is contained in:
committed by
Alex Rickabaugh
parent
199f7a2ba9
commit
f6f7a375a8
@@ -55,6 +55,59 @@ describe('Form Builder', () => {
|
||||
expect(g.controls['login'].asyncValidator).toBe(asyncValidator);
|
||||
});
|
||||
|
||||
it('should create controls from FormStates', () => {
|
||||
const c = b.control({value: 'one', disabled: false});
|
||||
expect(c.value).toEqual('one');
|
||||
c.reset();
|
||||
expect(c.value).toEqual(null);
|
||||
});
|
||||
|
||||
it('should work on a directly constructed FormBuilder object', () => {
|
||||
const g = b.group({'login': 'some value'});
|
||||
expect(g.controls['login'].value).toEqual('some value');
|
||||
});
|
||||
|
||||
it('should create homogenous control arrays', () => {
|
||||
const a = b.array(['one', 'two', 'three']);
|
||||
expect(a.value).toEqual(['one', 'two', 'three']);
|
||||
});
|
||||
|
||||
it('should create control arrays with FormStates and ControlConfigs', () => {
|
||||
const a = b.array(['one', 'two', {value: 'three', disabled: false}]);
|
||||
expect(a.value).toEqual(['one', 'two', 'three']);
|
||||
});
|
||||
|
||||
it('should create control arrays with ControlConfigs', () => {
|
||||
const a = b.array([['one', syncValidator, asyncValidator]]);
|
||||
expect(a.value).toEqual(['one']);
|
||||
expect(a.controls[0].validator).toBe(syncValidator);
|
||||
expect(a.controls[0].asyncValidator).toBe(asyncValidator);
|
||||
});
|
||||
|
||||
it('should create nested control arrays with ControlConfigs', () => {
|
||||
const a = b.array(['one', ['two', syncValidator, asyncValidator]]);
|
||||
expect(a.value).toEqual(['one', 'two']);
|
||||
expect(a.controls[1].validator).toBe(syncValidator);
|
||||
expect(a.controls[1].asyncValidator).toBe(asyncValidator);
|
||||
});
|
||||
|
||||
it('should create control arrays with AbstractControls', () => {
|
||||
const ctrl = b.control('one');
|
||||
const a = b.array([ctrl], syncValidator, asyncValidator);
|
||||
expect(a.value).toEqual(['one']);
|
||||
expect(a.validator).toBe(syncValidator);
|
||||
expect(a.asyncValidator).toBe(asyncValidator);
|
||||
});
|
||||
|
||||
it('should create control arrays with mixed value representations', () => {
|
||||
const a = b.array([
|
||||
'one', ['two', syncValidator, asyncValidator], {value: 'three', disabled: false},
|
||||
[{value: 'four', disabled: false}, syncValidator, asyncValidator], ['five'], b.control('six'),
|
||||
b.control({value: 'seven', disabled: false})
|
||||
]);
|
||||
expect(a.value).toEqual(['one', 'two', 'three', 'four', 'five', 'six', 'seven']);
|
||||
});
|
||||
|
||||
it('should support controls with no validators and whose form state is null', () => {
|
||||
const g = b.group({'login': b.control(null)});
|
||||
expect(g.controls['login'].value).toBeNull();
|
||||
|
||||
Reference in New Issue
Block a user