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