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