|
| 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 | +}); |
0 commit comments