]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
af493513eec099b6f49e3e440cecd93b8fff8354
[ceph.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 { NgbDropdownModule, NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap';
6 import { NgxDatatableModule } from '@swimlane/ngx-datatable';
7 import { NgxPipeFunctionModule } from 'ngx-pipe-function';
8
9 import { ComponentsModule } from '~/app/shared/components/components.module';
10 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
11 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
12 import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
13 import { PipesModule } from '~/app/shared/pipes/pipes.module';
14 import { configureTestBed } from '~/testing/unit-test-helper';
15 import { TablePaginationComponent } from '../table-pagination/table-pagination.component';
16 import { TableComponent } from '../table/table.component';
17 import { TableKeyValueComponent } from './table-key-value.component';
18
19 describe('TableKeyValueComponent', () => {
20   let component: TableKeyValueComponent;
21   let fixture: ComponentFixture<TableKeyValueComponent>;
22
23   configureTestBed({
24     declarations: [TableComponent, TableKeyValueComponent, TablePaginationComponent],
25     imports: [
26       FormsModule,
27       NgxDatatableModule,
28       ComponentsModule,
29       RouterTestingModule,
30       NgbDropdownModule,
31       PipesModule,
32       NgbTooltipModule,
33       NgxPipeFunctionModule
34     ]
35   });
36
37   beforeEach(() => {
38     fixture = TestBed.createComponent(TableKeyValueComponent);
39     component = fixture.componentInstance;
40   });
41
42   it('should create', () => {
43     fixture.detectChanges();
44     expect(component).toBeTruthy();
45   });
46
47   it('should make key value object pairs out of arrays with length two', () => {
48     component.data = [
49       ['someKey', 0],
50       ['arrayKey', [1, 2, 3]],
51       [3, 'something']
52     ];
53     component.ngOnInit();
54     const expected: any = [
55       { key: 'arrayKey', value: '1, 2, 3' },
56       { key: 'someKey', value: 0 },
57       { key: 3, value: 'something' }
58     ];
59     expect(component.tableData).toEqual(expected);
60   });
61
62   it('should not show data supposed to be have hidden by key', () => {
63     component.data = [
64       ['a', 1],
65       ['b', 2]
66     ];
67     component.hideKeys = ['a'];
68     component.ngOnInit();
69     expect(component.tableData).toEqual([{ key: 'b', value: 2 }]);
70   });
71
72   it('should remove items with objects as values', () => {
73     component.data = [
74       [3, 'something'],
75       ['will be removed', { a: 3, b: 4, c: 5 }]
76     ];
77     component.ngOnInit();
78     expect(component.tableData).toEqual(<any>[{ key: 3, value: 'something' }]);
79   });
80
81   it('makes key value object pairs out of an object', () => {
82     component.data = { 3: 'something', someKey: 0 };
83     component.ngOnInit();
84     expect(component.tableData).toEqual([
85       { key: '3', value: 'something' },
86       { key: 'someKey', value: 0 }
87     ]);
88   });
89
90   it('does nothing if data does not need to be converted', () => {
91     component.data = [
92       { key: 3, value: 'something' },
93       { key: 'someKey', value: 0 }
94     ];
95     component.ngOnInit();
96     expect(component.tableData).toEqual(component.data);
97   });
98
99   it('throws errors if data cannot be converted', () => {
100     component.data = 38;
101     expect(() => component.ngOnInit()).toThrowError('Wrong data format');
102     component.data = [['someKey', 0, 3]];
103     expect(() => component.ngOnInit()).toThrowError(
104       'Array contains too many elements (3). Needs to be of type [string, any][]'
105     );
106   });
107
108   it('tests makePairs()', () => {
109     const makePairs = (data: any) => component['makePairs'](data);
110     expect(makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
111     const pair = [
112       { key: 'dash', value: 'board' },
113       { key: 'ceph', value: 'mimic' }
114     ];
115     const pairInverse = [
116       { key: 'ceph', value: 'mimic' },
117       { key: 'dash', value: 'board' }
118     ];
119     expect(makePairs(pair)).toEqual(pairInverse);
120     expect(makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
121     expect(makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
122   });
123
124   it('tests makePairsFromArray()', () => {
125     const makePairsFromArray = (data: any[]) => component['makePairsFromArray'](data);
126     expect(makePairsFromArray([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
127     const pair = [
128       { key: 'dash', value: 'board' },
129       { key: 'ceph', value: 'mimic' }
130     ];
131     expect(makePairsFromArray(pair)).toEqual(pair);
132   });
133
134   it('tests makePairsFromObject()', () => {
135     const makePairsFromObject = (data: object) => component['makePairsFromObject'](data);
136     expect(makePairsFromObject({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
137     expect(makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
138       { key: 'dash', value: 'board' },
139       { key: 'ceph', value: 'mimic' }
140     ]);
141   });
142
143   describe('tests convertValue()', () => {
144     const convertValue = (data: any) => component['convertValue'](data);
145     const expectConvertValue = (value: any, expectation: any) =>
146       expect(convertValue(value)).toBe(expectation);
147
148     it('should not convert strings', () => {
149       expectConvertValue('something', 'something');
150     });
151
152     it('should not convert integers', () => {
153       expectConvertValue(29, 29);
154     });
155
156     it('should convert arrays with any type to strings', () => {
157       expectConvertValue([1, 2, 3], '1, 2, 3');
158       expectConvertValue([{ sth: 'something' }], '{"sth":"something"}');
159       expectConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
160     });
161
162     it('should only convert objects if renderObjects is set to true', () => {
163       expect(convertValue({ sth: 'something' })).toBe(null);
164       component.renderObjects = true;
165       expect(convertValue({ sth: 'something' })).toEqual({ sth: 'something' });
166     });
167   });
168
169   describe('automatically pipe UTC dates through cdDate', () => {
170     let datePipe: CdDatePipe;
171
172     beforeEach(() => {
173       datePipe = TestBed.inject(CdDatePipe);
174       spyOn(datePipe, 'transform').and.callThrough();
175     });
176
177     const expectTimeConversion = (date: string) => {
178       component.data = { dateKey: date };
179       component.ngOnInit();
180       expect(datePipe.transform).toHaveBeenCalledWith(date);
181       expect(component.tableData[0].key).not.toBe(date);
182     };
183
184     it('converts some date', () => {
185       expectTimeConversion('2019-04-15 12:26:52.305285');
186     });
187
188     it('converts UTC date', () => {
189       expectTimeConversion('2019-04-16T12:35:46.646300974Z');
190     });
191   });
192
193   describe('render objects', () => {
194     beforeEach(() => {
195       component.data = {
196         options: {
197           numberKey: 38,
198           stringKey: 'somethingElse',
199           objectKey: {
200             sub1: 12,
201             sub2: 34,
202             sub3: 56
203           }
204         },
205         otherOptions: {
206           sub1: {
207             x: 42
208           },
209           sub2: {
210             y: 555
211           }
212         },
213         additionalKeyContainingObject: { type: 'none' },
214         keyWithEmptyObject: {}
215       };
216       component.renderObjects = true;
217     });
218
219     it('with parent key', () => {
220       component.ngOnInit();
221       expect(component.tableData).toEqual([
222         { key: 'additionalKeyContainingObject type', value: 'none' },
223         { key: 'keyWithEmptyObject', value: '' },
224         { key: 'options numberKey', value: 38 },
225         { key: 'options objectKey sub1', value: 12 },
226         { key: 'options objectKey sub2', value: 34 },
227         { key: 'options objectKey sub3', value: 56 },
228         { key: 'options stringKey', value: 'somethingElse' },
229         { key: 'otherOptions sub1 x', value: 42 },
230         { key: 'otherOptions sub2 y', value: 555 }
231       ]);
232     });
233
234     it('without parent key', () => {
235       component.appendParentKey = false;
236       component.ngOnInit();
237       expect(component.tableData).toEqual([
238         { key: 'keyWithEmptyObject', value: '' },
239         { key: 'numberKey', value: 38 },
240         { key: 'stringKey', value: 'somethingElse' },
241         { key: 'sub1', value: 12 },
242         { key: 'sub2', value: 34 },
243         { key: 'sub3', value: 56 },
244         { key: 'type', value: 'none' },
245         { key: 'x', value: 42 },
246         { key: 'y', value: 555 }
247       ]);
248     });
249   });
250
251   describe('subscribe fetchData', () => {
252     it('should not subscribe fetchData of table', () => {
253       component.ngOnInit();
254       expect(component.table.fetchData.observers.length).toBe(0);
255     });
256
257     it('should call fetchData', () => {
258       let called = false;
259       component.fetchData.subscribe(() => {
260         called = true;
261       });
262       component.ngOnInit();
263       expect(component.table.fetchData.observers.length).toBe(1);
264       component.table.fetchData.emit();
265       expect(called).toBeTruthy();
266     });
267   });
268
269   describe('hide empty items', () => {
270     beforeEach(() => {
271       component.data = {
272         booleanFalse: false,
273         booleanTrue: true,
274         string: '',
275         array: [],
276         object: {},
277         emptyObject: {
278           string: '',
279           array: [],
280           object: {}
281         },
282         someNumber: 0,
283         someDifferentNumber: 1,
284         someArray: [0, 1],
285         someString: '0',
286         someObject: {
287           empty: {},
288           something: 0.1
289         }
290       };
291       component.renderObjects = true;
292     });
293
294     it('should show all items as default', () => {
295       expect(component.hideEmpty).toBe(false);
296       component.ngOnInit();
297       expect(component.tableData).toEqual([
298         { key: 'array', value: '' },
299         { key: 'booleanFalse', value: false },
300         { key: 'booleanTrue', value: true },
301         { key: 'emptyObject array', value: '' },
302         { key: 'emptyObject object', value: '' },
303         { key: 'emptyObject string', value: '' },
304         { key: 'object', value: '' },
305         { key: 'someArray', value: '0, 1' },
306         { key: 'someDifferentNumber', value: 1 },
307         { key: 'someNumber', value: 0 },
308         { key: 'someObject empty', value: '' },
309         { key: 'someObject something', value: 0.1 },
310         { key: 'someString', value: '0' },
311         { key: 'string', value: '' }
312       ]);
313     });
314
315     it('should hide all empty items', () => {
316       component.hideEmpty = true;
317       component.ngOnInit();
318       expect(component.tableData).toEqual([
319         { key: 'booleanFalse', value: false },
320         { key: 'booleanTrue', value: true },
321         { key: 'someArray', value: '0, 1' },
322         { key: 'someDifferentNumber', value: 1 },
323         { key: 'someNumber', value: 0 },
324         { key: 'someObject something', value: 0.1 },
325         { key: 'someString', value: '0' }
326       ]);
327     });
328   });
329
330   describe('columns set up', () => {
331     let columns: CdTableColumn[];
332
333     beforeEach(() => {
334       columns = [
335         { prop: 'key', flexGrow: 1, cellTransformation: CellTemplate.bold },
336         { prop: 'value', flexGrow: 3 }
337       ];
338     });
339
340     it('should have the following default column set up', () => {
341       component.ngOnInit();
342       expect(component.columns).toEqual(columns);
343     });
344
345     it('should have the following column set up if customCss is defined', () => {
346       component.customCss = { 'class-name': 42 };
347       component.ngOnInit();
348       columns[1].cellTransformation = CellTemplate.classAdding;
349       expect(component.columns).toEqual(columns);
350     });
351   });
352 });