summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/components/config-option/config-option.types.spec.ts
blob: 8c34111b957c5fae846db719e4b16f29b5eeb85a (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { ConfigFormModel } from './config-option.model';
import { ConfigOptionTypes } from './config-option.types';

describe('ConfigOptionTypes', () => {
  describe('getType', () => {
    it('should return uint type', () => {
      const ret = ConfigOptionTypes.getType('uint');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('uint');
      expect(ret.inputType).toBe('number');
      expect(ret.humanReadable).toBe('Unsigned integer value');
      expect(ret.defaultMin).toBe(0);
      expect(ret.patternHelpText).toBe('The entered value needs to be an unsigned number.');
      expect(ret.isNumberType).toBe(true);
      expect(ret.allowsNegative).toBe(false);
    });

    it('should return int type', () => {
      const ret = ConfigOptionTypes.getType('int');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('int');
      expect(ret.inputType).toBe('number');
      expect(ret.humanReadable).toBe('Integer value');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBe('The entered value needs to be a number.');
      expect(ret.isNumberType).toBe(true);
      expect(ret.allowsNegative).toBe(true);
    });

    it('should return size type', () => {
      const ret = ConfigOptionTypes.getType('size');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('size');
      expect(ret.inputType).toBe('number');
      expect(ret.humanReadable).toBe('Unsigned integer value (>=16bit)');
      expect(ret.defaultMin).toBe(0);
      expect(ret.patternHelpText).toBe('The entered value needs to be a unsigned number.');
      expect(ret.isNumberType).toBe(true);
      expect(ret.allowsNegative).toBe(false);
    });

    it('should return secs type', () => {
      const ret = ConfigOptionTypes.getType('secs');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('secs');
      expect(ret.inputType).toBe('number');
      expect(ret.humanReadable).toBe('Number of seconds');
      expect(ret.defaultMin).toBe(1);
      expect(ret.patternHelpText).toBe('The entered value needs to be a number >= 1.');
      expect(ret.isNumberType).toBe(true);
      expect(ret.allowsNegative).toBe(false);
    });

    it('should return float type', () => {
      const ret = ConfigOptionTypes.getType('float');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('float');
      expect(ret.inputType).toBe('number');
      expect(ret.humanReadable).toBe('Double value');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBe('The entered value needs to be a number or decimal.');
      expect(ret.isNumberType).toBe(true);
      expect(ret.allowsNegative).toBe(true);
    });

    it('should return str type', () => {
      const ret = ConfigOptionTypes.getType('str');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('str');
      expect(ret.inputType).toBe('text');
      expect(ret.humanReadable).toBe('Text');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBeUndefined();
      expect(ret.isNumberType).toBe(false);
      expect(ret.allowsNegative).toBeUndefined();
    });

    it('should return addr type', () => {
      const ret = ConfigOptionTypes.getType('addr');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('addr');
      expect(ret.inputType).toBe('text');
      expect(ret.humanReadable).toBe('IPv4 or IPv6 address');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBe('The entered value needs to be a valid IP address.');
      expect(ret.isNumberType).toBe(false);
      expect(ret.allowsNegative).toBeUndefined();
    });

    it('should return uuid type', () => {
      const ret = ConfigOptionTypes.getType('uuid');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('uuid');
      expect(ret.inputType).toBe('text');
      expect(ret.humanReadable).toBe('UUID');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBe(
        'The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8'
      );
      expect(ret.isNumberType).toBe(false);
      expect(ret.allowsNegative).toBeUndefined();
    });

    it('should return bool type', () => {
      const ret = ConfigOptionTypes.getType('bool');
      expect(ret).toBeTruthy();
      expect(ret.name).toBe('bool');
      expect(ret.inputType).toBe('checkbox');
      expect(ret.humanReadable).toBe('Boolean value');
      expect(ret.defaultMin).toBeUndefined();
      expect(ret.patternHelpText).toBeUndefined();
      expect(ret.isNumberType).toBe(false);
      expect(ret.allowsNegative).toBeUndefined();
    });

    it('should throw an error for unknown type', () => {
      expect(() => ConfigOptionTypes.getType('unknown')).toThrowError(
        'Found unknown type "unknown" for config option.'
      );
    });
  });

  describe('getTypeValidators', () => {
    it('should return two validators for type uint, secs and size', () => {
      const types = ['uint', 'size', 'secs'];

      types.forEach((valType) => {
        const configOption = new ConfigFormModel();
        configOption.type = valType;

        const ret = ConfigOptionTypes.getTypeValidators(configOption);
        expect(ret).toBeTruthy();
        expect(ret.validators.length).toBe(2);
      });
    });

    it('should return a validator for types float, int, addr and uuid', () => {
      const types = ['float', 'int', 'addr', 'uuid'];

      types.forEach((valType) => {
        const configOption = new ConfigFormModel();
        configOption.type = valType;

        const ret = ConfigOptionTypes.getTypeValidators(configOption);
        expect(ret).toBeTruthy();
        expect(ret.validators.length).toBe(1);
      });
    });

    it('should return undefined for type bool and str', () => {
      const types = ['str', 'bool'];

      types.forEach((valType) => {
        const configOption = new ConfigFormModel();
        configOption.type = valType;

        const ret = ConfigOptionTypes.getTypeValidators(configOption);
        expect(ret).toBeUndefined();
      });
    });

    it('should return a pattern and a min validator', () => {
      const configOption = new ConfigFormModel();
      configOption.type = 'int';
      configOption.min = 2;

      const ret = ConfigOptionTypes.getTypeValidators(configOption);
      expect(ret).toBeTruthy();
      expect(ret.validators.length).toBe(2);
      expect(ret.min).toBe(2);
      expect(ret.max).toBeUndefined();
    });

    it('should return a pattern and a max validator', () => {
      const configOption = new ConfigFormModel();
      configOption.type = 'int';
      configOption.max = 5;

      const ret = ConfigOptionTypes.getTypeValidators(configOption);
      expect(ret).toBeTruthy();
      expect(ret.validators.length).toBe(2);
      expect(ret.min).toBeUndefined();
      expect(ret.max).toBe(5);
    });

    it('should return multiple validators', () => {
      const configOption = new ConfigFormModel();
      configOption.type = 'float';
      configOption.max = 5.2;
      configOption.min = 1.5;

      const ret = ConfigOptionTypes.getTypeValidators(configOption);
      expect(ret).toBeTruthy();
      expect(ret.validators.length).toBe(3);
      expect(ret.min).toBe(1.5);
      expect(ret.max).toBe(5.2);
    });

    it(
      'should return a pattern help text for type uint, int, size, secs, ' + 'float, addr and uuid',
      () => {
        const types = ['uint', 'int', 'size', 'secs', 'float', 'addr', 'uuid'];

        types.forEach((valType) => {
          const configOption = new ConfigFormModel();
          configOption.type = valType;

          const ret = ConfigOptionTypes.getTypeValidators(configOption);
          expect(ret).toBeTruthy();
          expect(ret.patternHelpText).toBeDefined();
        });
      }
    );
  });

  describe('getTypeStep', () => {
    it('should return the correct step for type uint and value 0', () => {
      const ret = ConfigOptionTypes.getTypeStep('uint', 0);
      expect(ret).toBe(1);
    });

    it('should return the correct step for type int and value 1', () => {
      const ret = ConfigOptionTypes.getTypeStep('int', 1);
      expect(ret).toBe(1);
    });

    it('should return the correct step for type int and value null', () => {
      const ret = ConfigOptionTypes.getTypeStep('int', null);
      expect(ret).toBe(1);
    });

    it('should return the correct step for type size and value 2', () => {
      const ret = ConfigOptionTypes.getTypeStep('size', 2);
      expect(ret).toBe(1);
    });

    it('should return the correct step for type secs and value 3', () => {
      const ret = ConfigOptionTypes.getTypeStep('secs', 3);
      expect(ret).toBe(1);
    });

    it('should return the correct step for type float and value 1', () => {
      const ret = ConfigOptionTypes.getTypeStep('float', 1);
      expect(ret).toBe(0.1);
    });

    it('should return the correct step for type float and value 0.1', () => {
      const ret = ConfigOptionTypes.getTypeStep('float', 0.1);
      expect(ret).toBe(0.1);
    });

    it('should return the correct step for type float and value 0.02', () => {
      const ret = ConfigOptionTypes.getTypeStep('float', 0.02);
      expect(ret).toBe(0.01);
    });

    it('should return the correct step for type float and value 0.003', () => {
      const ret = ConfigOptionTypes.getTypeStep('float', 0.003);
      expect(ret).toBe(0.001);
    });

    it('should return the correct step for type float and value null', () => {
      const ret = ConfigOptionTypes.getTypeStep('float', null);
      expect(ret).toBe(0.1);
    });

    it('should return undefined for unknown type', () => {
      const ret = ConfigOptionTypes.getTypeStep('unknown', 1);
      expect(ret).toBeUndefined();
    });
  });
});