]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-form-group.spec.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / forms / cd-form-group.spec.ts
1 import { AbstractControl, FormControl, FormGroup, NgForm } from '@angular/forms';
2
3 import { CdFormGroup } from './cd-form-group';
4
5 describe('CdFormGroup', () => {
6 let form: CdFormGroup;
7
8 const createTestForm = (controlName: string, value: any): FormGroup =>
9 new FormGroup({
10 [controlName]: new FormControl(value)
11 });
12
13 describe('test get and getValue in nested forms', () => {
14 let formA: FormGroup;
15 let formB: FormGroup;
16 let formC: FormGroup;
17
18 beforeEach(() => {
19 formA = createTestForm('a', 'a');
20 formB = createTestForm('b', 'b');
21 formC = createTestForm('c', 'c');
22 form = new CdFormGroup({
23 formA: formA,
24 formB: formB,
25 formC: formC
26 });
27 });
28
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'));
33 });
34
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!`);
38 });
39 });
40
41 describe('CdFormGroup tests', () => {
42 let x, nested, a, c;
43
44 beforeEach(() => {
45 a = new FormControl('a');
46 x = new CdFormGroup({
47 a: a
48 });
49 nested = new CdFormGroup({
50 lev1: new CdFormGroup({
51 lev2: new FormControl('lev2')
52 })
53 });
54 c = createTestForm('c', 'c');
55 form = new CdFormGroup({
56 nested: nested,
57 cdform: x,
58 b: new FormControl('b'),
59 formC: c
60 });
61 });
62
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');
66 });
67
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');
71 });
72
73 it('should return value "b" that is not nested in nested form', () => {
74 expect(form.getValue('b')).toBe('b');
75 });
76
77 it('return value "c" out of normal form group "c" in nested form', () => {
78 expect(form.getValue('c')).toBe('c');
79 });
80
81 it('should return "lev2" value', () => {
82 expect(form.getValue('lev2')).toBe('lev2');
83 });
84
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!`);
88 });
89 });
90
91 describe('test different values for getValue', () => {
92 beforeEach(() => {
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)
106 });
107 });
108
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 });
113 });
114
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);
119 });
120
121 it('returns strings', () => {
122 expect(form.getValue('emptyString')).toBe('');
123 expect(form.getValue('someString1')).toBe('s');
124 expect(form.getValue('someString2')).toBe('sth');
125 });
126
127 it('returns booleans', () => {
128 expect(form.getValue('true')).toBe(true);
129 expect(form.getValue('false')).toBe(false);
130 });
131
132 it('returns null if control was set as undefined', () => {
133 expect(form.getValue('undefined')).toBe(null);
134 });
135
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();
140 });
141 });
142
143 describe('should test showError', () => {
144 let formDir: NgForm;
145 let test: AbstractControl;
146
147 beforeEach(() => {
148 formDir = new NgForm([], []);
149 form = new CdFormGroup({
150 test_form: createTestForm('test', '')
151 });
152 test = form.get('test');
153 test.setErrors({ someError: 'failed' });
154 });
155
156 it('should not show an error if not dirty and not submitted', () => {
157 expect(form.showError('test', formDir)).toBe(false);
158 });
159
160 it('should show an error if dirty', () => {
161 test.markAsDirty();
162 expect(form.showError('test', formDir)).toBe(true);
163 });
164
165 it('should show an error if submitted', () => {
166 expect(form.showError('test', <NgForm>{ submitted: true })).toBe(true);
167 });
168
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);
172 test.markAsDirty();
173 expect(form.showError('test', formDir)).toBe(false);
174 });
175
176 it('should not show error if the given error is not there', () => {
177 expect(form.showError('test', <NgForm>{ submitted: true }, 'someOtherError')).toBe(false);
178 });
179
180 it('should show error if the given error is there', () => {
181 expect(form.showError('test', <NgForm>{ submitted: true }, 'someError')).toBe(true);
182 });
183 });
184 });