Skip to content

Commit b35b7dc

Browse files
Add getFormControlFromContainer tests
1 parent 9ec3f4a commit b35b7dc

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

angular-reactive-validation/src/get-control-path.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TestBed, async, inject } from '@angular/core/testing';
1+
import { TestBed, inject } from '@angular/core/testing';
22
import { FormBuilder } from '@angular/forms';
33

44
import { getControlPath } from './get-control-path';
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { TestBed, async, inject } from '@angular/core/testing';
2+
import { FormBuilder } from '@angular/forms';
3+
4+
import { getFormControlFromContainer } from './get-form-control-from-container';
5+
6+
describe('getFormControlFromContainer', () => {
7+
beforeEach(() => {
8+
TestBed.configureTestingModule({
9+
providers: [
10+
FormBuilder
11+
]
12+
});
13+
});
14+
15+
it(`gets a FormControl from the FormGroup`, inject([FormBuilder], (fb: FormBuilder) => {
16+
const firstName = fb.control('');
17+
const group = fb.group({
18+
firstName: firstName
19+
});
20+
21+
const container: any = {
22+
control: group
23+
};
24+
25+
expect(getFormControlFromContainer('firstName', container)).toBe(firstName);
26+
}));
27+
28+
it(`throws an Error when no container is provided`, inject([FormBuilder], (fb: FormBuilder) => {
29+
expect(() => getFormControlFromContainer('firstName', undefined)).toThrow(new Error(
30+
`You can't pass a string to arv-validation-messages's for attribute, when the ` +
31+
`arv-validation-messages element is not a child of an element with a formGroupName or formGroup declaration.`));
32+
}));
33+
34+
it(`throws an Error when there is no FormControl with the given name`, inject([FormBuilder], (fb: FormBuilder) => {
35+
const group = fb.group({});
36+
37+
const container: any = {
38+
control: group,
39+
path: ['the', 'path']
40+
};
41+
42+
expect(() => getFormControlFromContainer('lastName', container)).toThrow(new Error(
43+
`There is no control named 'lastName' within 'the.path'.`
44+
));
45+
}));
46+
47+
it(`throws an Error when there is a FormGroup with the given name`, inject([FormBuilder], (fb: FormBuilder) => {
48+
const group = fb.group({
49+
name: fb.group({})
50+
});
51+
52+
const container: any = {
53+
control: group,
54+
path: ['the', 'path']
55+
};
56+
57+
expect(() => getFormControlFromContainer('name', container)).toThrow(new Error(
58+
`The control named 'name' within 'the.path' is not a FormControl. Maybe you accidentally referenced a FormGroup or FormArray?`
59+
));
60+
}));
61+
62+
it(`throws an Error when there is a FormArray with the given name`, inject([FormBuilder], (fb: FormBuilder) => {
63+
const group = fb.group({
64+
name: fb.array([])
65+
});
66+
67+
const container: any = {
68+
control: group,
69+
path: ['the', 'path']
70+
};
71+
72+
expect(() => getFormControlFromContainer('name', container)).toThrow(new Error(
73+
`The control named 'name' within 'the.path' is not a FormControl. Maybe you accidentally referenced a FormGroup or FormArray?`
74+
));
75+
}));
76+
});

angular-reactive-validation/src/get-form-control-from-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function getFormControlFromContainer(name: string, controlContainer: Cont
1010
if (!(control instanceof FormControl)) {
1111
throw new Error(`The control named '${name}' ` +
1212
(controlContainer.path.length > 0 ? `within '${controlContainer.path.join('.')}' ` : '') +
13-
`is not a FormControl. Maybe you accidentally referenced a FormGroup?`);
13+
`is not a FormControl. Maybe you accidentally referenced a FormGroup or FormArray?`);
1414
}
1515

1616
return control;

0 commit comments

Comments
 (0)