summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-form-group.spec.ts
blob: 240da3af84935f07c9488ec5dcab116d3ef2ccbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { AbstractControl, FormControl, FormGroup, NgForm } from '@angular/forms';

import { CdFormGroup } from './cd-form-group';

describe('CdFormGroup', () => {
  let form: CdFormGroup;

  const createTestForm = (controlName: string, value: any): FormGroup =>
    new FormGroup({
      [controlName]: new FormControl(value)
    });

  describe('test get and getValue in nested forms', () => {
    let formA: FormGroup;
    let formB: FormGroup;
    let formC: FormGroup;

    beforeEach(() => {
      formA = createTestForm('a', 'a');
      formB = createTestForm('b', 'b');
      formC = createTestForm('c', 'c');
      form = new CdFormGroup({
        formA: formA,
        formB: formB,
        formC: formC
      });
    });

    it('should find controls out of every form', () => {
      expect(form.get('a')).toBe(formA.get('a'));
      expect(form.get('b')).toBe(formB.get('b'));
      expect(form.get('c')).toBe(formC.get('c'));
    });

    it('should throw an error if element could be found', () => {
      expect(() => form.get('d')).toThrowError(`Control 'd' could not be found!`);
      expect(() => form.get('sth')).toThrowError(`Control 'sth' could not be found!`);
    });
  });

  describe('CdFormGroup tests', () => {
    let x: CdFormGroup, nested: CdFormGroup, a: FormControl, c: FormGroup;

    beforeEach(() => {
      a = new FormControl('a');
      x = new CdFormGroup({
        a: a
      });
      nested = new CdFormGroup({
        lev1: new CdFormGroup({
          lev2: new FormControl('lev2')
        })
      });
      c = createTestForm('c', 'c');
      form = new CdFormGroup({
        nested: nested,
        cdform: x,
        b: new FormControl('b'),
        formC: c
      });
    });

    it('should return single value from "a" control in not nested form "x"', () => {
      expect(x.get('a')).toBe(a);
      expect(x.getValue('a')).toBe('a');
    });

    it('should return control "a" out of form "x" in nested form', () => {
      expect(form.get('a')).toBe(a);
      expect(form.getValue('a')).toBe('a');
    });

    it('should return value "b" that is not nested in nested form', () => {
      expect(form.getValue('b')).toBe('b');
    });

    it('return value "c" out of normal form group "c" in nested form', () => {
      expect(form.getValue('c')).toBe('c');
    });

    it('should return "lev2" value', () => {
      expect(form.getValue('lev2')).toBe('lev2');
    });

    it('should nested throw an error if control could not be found', () => {
      expect(() => form.get('d')).toThrowError(`Control 'd' could not be found!`);
      expect(() => form.getValue('sth')).toThrowError(`Control 'sth' could not be found!`);
    });
  });

  describe('test different values for getValue', () => {
    beforeEach(() => {
      form = new CdFormGroup({
        form_undefined: createTestForm('undefined', undefined),
        form_null: createTestForm('null', null),
        form_emptyObject: createTestForm('emptyObject', {}),
        form_filledObject: createTestForm('filledObject', { notEmpty: 1 }),
        form_number0: createTestForm('number0', 0),
        form_number1: createTestForm('number1', 1),
        form_emptyString: createTestForm('emptyString', ''),
        form_someString1: createTestForm('someString1', 's'),
        form_someString2: createTestForm('someString2', 'sth'),
        form_floating: createTestForm('floating', 0.1),
        form_false: createTestForm('false', false),
        form_true: createTestForm('true', true)
      });
    });

    it('returns objects', () => {
      expect(form.getValue('null')).toBe(null);
      expect(form.getValue('emptyObject')).toEqual({});
      expect(form.getValue('filledObject')).toEqual({ notEmpty: 1 });
    });

    it('returns set numbers', () => {
      expect(form.getValue('number0')).toBe(0);
      expect(form.getValue('number1')).toBe(1);
      expect(form.getValue('floating')).toBe(0.1);
    });

    it('returns strings', () => {
      expect(form.getValue('emptyString')).toBe('');
      expect(form.getValue('someString1')).toBe('s');
      expect(form.getValue('someString2')).toBe('sth');
    });

    it('returns booleans', () => {
      expect(form.getValue('true')).toBe(true);
      expect(form.getValue('false')).toBe(false);
    });

    it('returns null if control was set as undefined', () => {
      expect(form.getValue('undefined')).toBe(null);
    });

    it('returns a falsy value for null, undefined, false and 0', () => {
      expect(form.getValue('false')).toBeFalsy();
      expect(form.getValue('null')).toBeFalsy();
      expect(form.getValue('number0')).toBeFalsy();
    });
  });

  describe('should test showError', () => {
    let formDir: NgForm;
    let test: AbstractControl;

    beforeEach(() => {
      formDir = new NgForm([], []);
      form = new CdFormGroup({
        test_form: createTestForm('test', '')
      });
      test = form.get('test');
      test.setErrors({ someError: 'failed' });
    });

    it('should not show an error if not dirty and not submitted', () => {
      expect(form.showError('test', formDir)).toBe(false);
    });

    it('should show an error if dirty', () => {
      test.markAsDirty();
      expect(form.showError('test', formDir)).toBe(true);
    });

    it('should show an error if submitted', () => {
      expect(form.showError('test', <NgForm>{ submitted: true })).toBe(true);
    });

    it('should not show an error if no error exits', () => {
      test.setErrors(null);
      expect(form.showError('test', <NgForm>{ submitted: true })).toBe(false);
      test.markAsDirty();
      expect(form.showError('test', formDir)).toBe(false);
    });

    it('should not show error if the given error is not there', () => {
      expect(form.showError('test', <NgForm>{ submitted: true }, 'someOtherError')).toBe(false);
    });

    it('should show error if the given error is there', () => {
      expect(form.showError('test', <NgForm>{ submitted: true }, 'someError')).toBe(true);
    });
  });
});