]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-key-value/table-key-value.component.spec.ts
import ceph nautilus 14.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / datatable / table-key-value / table-key-value.component.spec.ts
CommitLineData
11fdf7f2
TL
1import { ComponentFixture, TestBed } from '@angular/core/testing';
2import { FormsModule } from '@angular/forms';
3import { RouterTestingModule } from '@angular/router/testing';
4
5import { NgxDatatableModule } from '@swimlane/ngx-datatable';
6
7import { configureTestBed } from '../../../../testing/unit-test-helper';
8import { ComponentsModule } from '../../components/components.module';
9import { CellTemplate } from '../../enum/cell-template.enum';
10import { CdTableColumn } from '../../models/cd-table-column';
81eedcae 11import { CdDatePipe } from '../../pipes/cd-date.pipe';
11fdf7f2
TL
12import { TableComponent } from '../table/table.component';
13import { TableKeyValueComponent } from './table-key-value.component';
14
15describe('TableKeyValueComponent', () => {
16 let component: TableKeyValueComponent;
17 let fixture: ComponentFixture<TableKeyValueComponent>;
18
19 configureTestBed({
20 declarations: [TableComponent, TableKeyValueComponent],
21 imports: [FormsModule, NgxDatatableModule, ComponentsModule, RouterTestingModule]
22 });
23
24 beforeEach(() => {
25 fixture = TestBed.createComponent(TableKeyValueComponent);
26 component = fixture.componentInstance;
27 });
28
29 it('should create', () => {
30 fixture.detectChanges();
31 expect(component).toBeTruthy();
32 });
33
34 it('should make key value object pairs out of arrays with length two', () => {
35 component.data = [['someKey', 0], [3, 'something']];
36 component.ngOnInit();
37 expect(component.tableData.length).toBe(2);
38 expect(component.tableData[0].key).toBe('someKey');
39 expect(component.tableData[1].value).toBe('something');
40 });
41
42 it('should transform arrays', () => {
43 component.data = [['someKey', [1, 2, 3]], [3, 'something']];
44 component.ngOnInit();
45 expect(component.tableData.length).toBe(2);
46 expect(component.tableData[0].key).toBe('someKey');
47 expect(component.tableData[0].value).toBe('1, 2, 3');
48 expect(component.tableData[1].value).toBe('something');
49 });
50
51 it('should remove pure object values', () => {
52 component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
53 component.ngOnInit();
54 expect(component.tableData.length).toBe(1);
55 expect(component.tableData[0].value).toBe('something');
56 });
57
58 it('makes key value object pairs out of an object', () => {
59 component.data = {
60 3: 'something',
61 someKey: 0
62 };
63 component.ngOnInit();
64 expect(component.tableData.length).toBe(2);
65 expect(component.tableData[0].value).toBe('something');
66 expect(component.tableData[1].key).toBe('someKey');
67 });
68
69 it('does nothing if data is correct', () => {
70 component.data = [
71 {
72 key: 3,
73 value: 'something'
74 },
75 {
76 key: 'someKey',
77 value: 0
78 }
79 ];
80 component.ngOnInit();
81 expect(component.tableData.length).toBe(2);
82 expect(component.tableData[0].value).toBe('something');
83 expect(component.tableData[1].key).toBe('someKey');
84 });
85
86 it('throws errors if miss match', () => {
87 component.data = 38;
88 expect(() => component.ngOnInit()).toThrowError('Wrong data format');
89 component.data = [['someKey', 0, 3]];
90 expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
91 component.data = [{ somekey: 939, somethingElse: 'test' }];
92 });
93
94 describe('Class objects equal plain objects', () => {
95 class Example {
96 sth = 'something';
97 deep?: Example;
98 constructor(deep: boolean) {
99 if (deep) {
100 this.deep = new Example(false);
101 }
102 }
103 }
104
105 const classExample = new Example(true);
106 const objectExample = {
107 sth: 'something',
108 deep: {
109 sth: 'something'
110 }
111 };
112
113 const getTableData = (data) => {
114 component.data = data;
115 expect(() => component.ngOnInit()).not.toThrow();
116 return component.tableData;
117 };
118
119 const doesClassEqualsObject = (classData, objectData, dataLength) => {
120 const classTableData = getTableData(classData);
121 expect(classTableData).toEqual(getTableData(objectData));
122 expect(classTableData.length).toBe(dataLength);
123 };
124
125 it('should convert class objects the same way as plain objects', () => {
126 doesClassEqualsObject(classExample, objectExample, 1);
127 doesClassEqualsObject([classExample], [objectExample], 1);
128 component.renderObjects = true;
129 doesClassEqualsObject(classExample, objectExample, 2);
130 doesClassEqualsObject([classExample], [objectExample], 2);
131 });
132 });
133
134 it('tests _makePairs', () => {
135 expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
136 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
137 const pairInverse = [{ key: 'ceph', value: 'mimic' }, { key: 'dash', value: 'board' }];
138 expect(component._makePairs(pair)).toEqual(pairInverse);
139 expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
140 expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
141 });
142
143 it('tests _makePairsFromArray', () => {
144 expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
145 { key: 'dash', value: 'board' }
146 ]);
147 const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
148 expect(component._makePairsFromArray(pair)).toEqual(pair);
149 });
150
151 it('tests _makePairsFromObject', () => {
152 expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
153 { key: 'dash', value: 'board' }
154 ]);
155 expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
156 { key: 'dash', value: 'board' },
157 { key: 'ceph', value: 'mimic' }
158 ]);
159 });
160
161 describe('tests _convertValue', () => {
162 const v = (value) => ({ key: 'sth', value: value });
163 const testConvertValue = (value, result) =>
164 expect(component._convertValue(v(value)).value).toBe(result);
165
166 it('should leave a string as it is', () => {
167 testConvertValue('something', 'something');
168 });
169
170 it('should leave an int as it is', () => {
171 testConvertValue(29, 29);
172 });
173
174 it('should convert arrays with any type to string', () => {
175 testConvertValue([1, 2, 3], '1, 2, 3');
176 testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
177 testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
178 });
179
180 it('should convert only allow objects if renderObjects is set to true', () => {
181 expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
182 component.renderObjects = true;
183 expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
184 });
81eedcae
TL
185
186 describe('automatically pipe utc dates through cdDate', () => {
187 let datePipe: CdDatePipe;
188
189 beforeEach(() => {
190 datePipe = TestBed.get(CdDatePipe);
191 spyOn(datePipe, 'transform').and.callThrough();
192 });
193
194 const expectTimeConversion = (date: string) => {
195 component.data = {
196 3: 'some time',
197 someKey: date
198 };
199 component.ngOnInit();
200 expect(component.tableData.length).toBe(2);
201 expect(datePipe.transform).toHaveBeenCalledWith(date);
202 expect(component.tableData[1].key).not.toBe(date);
203 };
204
205 it('converts some date', () => {
206 expectTimeConversion('2019-04-15 12:26:52.305285');
207 });
208
209 it('converts utc date', () => {
210 expectTimeConversion('2019-04-16T12:35:46.646300974Z');
211 });
212 });
11fdf7f2
TL
213 });
214
215 describe('render objects', () => {
216 beforeEach(() => {
217 component.data = {
218 options: {
219 someSetting1: 38,
220 anotherSetting2: 'somethingElse',
221 suboptions: {
222 sub1: 12,
223 sub2: 34,
224 sub3: 56
225 }
226 },
227 someKey: 0,
228 o2: {
229 sub1: {
230 x: 42
231 },
232 sub2: {
233 y: 555
234 }
235 },
236 additionalKeyContainingObject: { type: 'none' },
237 keyWithEmptyObject: {}
238 };
239 component.renderObjects = true;
240 });
241
242 it('with parent key', () => {
243 component.ngOnInit();
244 expect(component.tableData).toEqual([
245 { key: 'additionalKeyContainingObject type', value: 'none' },
246 { key: 'keyWithEmptyObject', value: '' },
247 { key: 'o2 sub1 x', value: 42 },
248 { key: 'o2 sub2 y', value: 555 },
249 { key: 'options anotherSetting2', value: 'somethingElse' },
250 { key: 'options someSetting1', value: 38 },
251 { key: 'options suboptions sub1', value: 12 },
252 { key: 'options suboptions sub2', value: 34 },
253 { key: 'options suboptions sub3', value: 56 },
254 { key: 'someKey', value: 0 }
255 ]);
256 });
257
258 it('without parent key', () => {
259 component.appendParentKey = false;
260 component.ngOnInit();
261 expect(component.tableData).toEqual([
262 { key: 'anotherSetting2', value: 'somethingElse' },
263 { key: 'keyWithEmptyObject', value: '' },
264 { key: 'someKey', value: 0 },
265 { key: 'someSetting1', value: 38 },
266 { key: 'sub1', value: 12 },
267 { key: 'sub2', value: 34 },
268 { key: 'sub3', value: 56 },
269 { key: 'type', value: 'none' },
270 { key: 'x', value: 42 },
271 { key: 'y', value: 555 }
272 ]);
273 });
274 });
275
276 describe('subscribe fetchData', () => {
277 it('should not subscribe fetchData of table', () => {
278 component.ngOnInit();
279 expect(component.table.fetchData.observers.length).toBe(0);
280 });
281
282 it('should call fetchData', () => {
283 let called = false;
284 component.fetchData.subscribe(() => {
285 called = true;
286 });
287 component.ngOnInit();
288 expect(component.table.fetchData.observers.length).toBe(1);
289 component.table.fetchData.emit();
290 expect(called).toBeTruthy();
291 });
292 });
293
294 describe('hide empty items', () => {
295 beforeEach(() => {
296 component.data = {
297 string: '',
298 array: [],
299 object: {},
300 emptyObject: {
301 string: '',
302 array: [],
303 object: {}
304 },
305 someNumber: 0,
306 someDifferentNumber: 1,
307 someArray: [0, 1],
308 someString: '0',
309 someObject: {
310 empty: {},
311 something: 0.1
312 }
313 };
314 component.renderObjects = true;
315 });
316
317 it('should show all items as default', () => {
318 expect(component.hideEmpty).toBe(false);
319 component.ngOnInit();
320 expect(component.tableData).toEqual([
321 { key: 'array', value: '' },
322 { key: 'emptyObject array', value: '' },
323 { key: 'emptyObject object', value: '' },
324 { key: 'emptyObject string', value: '' },
325 { key: 'object', value: '' },
326 { key: 'someArray', value: '0, 1' },
327 { key: 'someDifferentNumber', value: 1 },
328 { key: 'someNumber', value: 0 },
329 { key: 'someObject empty', value: '' },
330 { key: 'someObject something', value: 0.1 },
331 { key: 'someString', value: '0' },
332 { key: 'string', value: '' }
333 ]);
334 });
335
336 it('should hide all empty items', () => {
337 component.hideEmpty = true;
338 component.ngOnInit();
339 expect(component.tableData).toEqual([
340 { key: 'someArray', value: '0, 1' },
341 { key: 'someDifferentNumber', value: 1 },
342 { key: 'someNumber', value: 0 },
343 { key: 'someObject something', value: 0.1 },
344 { key: 'someString', value: '0' }
345 ]);
346 });
347 });
348
349 describe('columns set up', () => {
350 let columns: CdTableColumn[];
351
352 beforeEach(() => {
353 columns = [
354 {
355 prop: 'key',
356 flexGrow: 1,
357 cellTransformation: CellTemplate.bold
358 },
359 {
360 prop: 'value',
361 flexGrow: 3
362 }
363 ];
364 });
365
366 it('should have the following default column set up', () => {
367 component.ngOnInit();
368 expect(component.columns).toEqual(columns);
369 });
370
371 it('should have the following column set up if customCss is defined', () => {
372 component.customCss = {
373 'answer-of-everything': 42
374 };
375 component.ngOnInit();
376 columns[1].cellTransformation = CellTemplate.classAdding;
377 expect(component.columns).toEqual(columns);
378 });
379 });
380});