1 import { AbstractControl, FormControl, FormGroup, NgForm } from '@angular/forms';
3 import { CdFormGroup } from './cd-form-group';
5 describe('CdFormGroup', () => {
8 const createTestForm = (controlName: string, value: any): FormGroup =>
10 [controlName]: new FormControl(value)
13 describe('test get and getValue in nested forms', () => {
19 formA = createTestForm('a', 'a');
20 formB = createTestForm('b', 'b');
21 formC = createTestForm('c', 'c');
22 form = new CdFormGroup({
29 it('should find controls out of every form', () => {
30 expect(form.get('a')).toBe(formA.get('a'));
31 expect(form.get('b')).toBe(formB.get('b'));
32 expect(form.get('c')).toBe(formC.get('c'));
35 it('should throw an error if element could be found', () => {
36 expect(() => form.get('d')).toThrowError(`Control 'd' could not be found!`);
37 expect(() => form.get('sth')).toThrowError(`Control 'sth' could not be found!`);
41 describe('CdFormGroup tests', () => {
45 a = new FormControl('a');
49 nested = new CdFormGroup({
50 lev1: new CdFormGroup({
51 lev2: new FormControl('lev2')
54 c = createTestForm('c', 'c');
55 form = new CdFormGroup({
58 b: new FormControl('b'),
63 it('should return single value from "a" control in not nested form "x"', () => {
64 expect(x.get('a')).toBe(a);
65 expect(x.getValue('a')).toBe('a');
68 it('should return control "a" out of form "x" in nested form', () => {
69 expect(form.get('a')).toBe(a);
70 expect(form.getValue('a')).toBe('a');
73 it('should return value "b" that is not nested in nested form', () => {
74 expect(form.getValue('b')).toBe('b');
77 it('return value "c" out of normal form group "c" in nested form', () => {
78 expect(form.getValue('c')).toBe('c');
81 it('should return "lev2" value', () => {
82 expect(form.getValue('lev2')).toBe('lev2');
85 it('should nested throw an error if control could not be found', () => {
86 expect(() => form.get('d')).toThrowError(`Control 'd' could not be found!`);
87 expect(() => form.getValue('sth')).toThrowError(`Control 'sth' could not be found!`);
91 describe('test different values for getValue', () => {
93 form = new CdFormGroup({
94 form_undefined: createTestForm('undefined', undefined),
95 form_null: createTestForm('null', null),
96 form_emptyObject: createTestForm('emptyObject', {}),
97 form_filledObject: createTestForm('filledObject', { notEmpty: 1 }),
98 form_number0: createTestForm('number0', 0),
99 form_number1: createTestForm('number1', 1),
100 form_emptyString: createTestForm('emptyString', ''),
101 form_someString1: createTestForm('someString1', 's'),
102 form_someString2: createTestForm('someString2', 'sth'),
103 form_floating: createTestForm('floating', 0.1),
104 form_false: createTestForm('false', false),
105 form_true: createTestForm('true', true)
109 it('returns objects', () => {
110 expect(form.getValue('null')).toBe(null);
111 expect(form.getValue('emptyObject')).toEqual({});
112 expect(form.getValue('filledObject')).toEqual({ notEmpty: 1 });
115 it('returns set numbers', () => {
116 expect(form.getValue('number0')).toBe(0);
117 expect(form.getValue('number1')).toBe(1);
118 expect(form.getValue('floating')).toBe(0.1);
121 it('returns strings', () => {
122 expect(form.getValue('emptyString')).toBe('');
123 expect(form.getValue('someString1')).toBe('s');
124 expect(form.getValue('someString2')).toBe('sth');
127 it('returns booleans', () => {
128 expect(form.getValue('true')).toBe(true);
129 expect(form.getValue('false')).toBe(false);
132 it('returns null if control was set as undefined', () => {
133 expect(form.getValue('undefined')).toBe(null);
136 it('returns a falsy value for null, undefined, false and 0', () => {
137 expect(form.getValue('false')).toBeFalsy();
138 expect(form.getValue('null')).toBeFalsy();
139 expect(form.getValue('number0')).toBeFalsy();
143 describe('should test showError', () => {
145 let test: AbstractControl;
148 formDir = new NgForm([], []);
149 form = new CdFormGroup({
150 test_form: createTestForm('test', '')
152 test = form.get('test');
153 test.setErrors({ someError: 'failed' });
156 it('should not show an error if not dirty and not submitted', () => {
157 expect(form.showError('test', formDir)).toBe(false);
160 it('should show an error if dirty', () => {
162 expect(form.showError('test', formDir)).toBe(true);
165 it('should show an error if submitted', () => {
166 expect(form.showError('test', <NgForm>{ submitted: true })).toBe(true);
169 it('should not show an error if no error exits', () => {
170 test.setErrors(null);
171 expect(form.showError('test', <NgForm>{ submitted: true })).toBe(false);
173 expect(form.showError('test', formDir)).toBe(false);
176 it('should not show error if the given error is not there', () => {
177 expect(form.showError('test', <NgForm>{ submitted: true }, 'someOtherError')).toBe(false);
180 it('should show error if the given error is there', () => {
181 expect(form.showError('test', <NgForm>{ submitted: true }, 'someError')).toBe(true);