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