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