]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
f697b2c1b5368c3c5b4fc1810b0baf84d6d2d4fe
[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
7 import { configureTestBed } from '../../../../testing/unit-test-helper';
8 import { ComponentsModule } from '../../components/components.module';
9 import { TableComponent } from '../table/table.component';
10 import { TableKeyValueComponent } from './table-key-value.component';
11
12 describe('TableKeyValueComponent', () => {
13   let component: TableKeyValueComponent;
14   let fixture: ComponentFixture<TableKeyValueComponent>;
15
16   configureTestBed({
17     declarations: [TableComponent, TableKeyValueComponent],
18     imports: [FormsModule, NgxDatatableModule, ComponentsModule, RouterTestingModule]
19   });
20
21   beforeEach(() => {
22     fixture = TestBed.createComponent(TableKeyValueComponent);
23     component = fixture.componentInstance;
24   });
25
26   it('should create', () => {
27     expect(component).toBeTruthy();
28   });
29
30   it('should make key value object pairs out of arrays with length two', () => {
31     component.data = [['someKey', 0], [3, 'something']];
32     component.ngOnInit();
33     expect(component.tableData.length).toBe(2);
34     expect(component.tableData[0].key).toBe('someKey');
35     expect(component.tableData[1].value).toBe('something');
36   });
37
38   it('should transform arrays', () => {
39     component.data = [['someKey', [1, 2, 3]], [3, 'something']];
40     component.ngOnInit();
41     expect(component.tableData.length).toBe(2);
42     expect(component.tableData[0].key).toBe('someKey');
43     expect(component.tableData[0].value).toBe('1, 2, 3');
44     expect(component.tableData[1].value).toBe('something');
45   });
46
47   it('should remove pure object values', () => {
48     component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
49     component.ngOnInit();
50     expect(component.tableData.length).toBe(1);
51     expect(component.tableData[0].value).toBe('something');
52   });
53
54   it('makes key value object pairs out of an object', () => {
55     component.data = {
56       3: 'something',
57       someKey: 0
58     };
59     component.ngOnInit();
60     expect(component.tableData.length).toBe(2);
61     expect(component.tableData[0].value).toBe('something');
62     expect(component.tableData[1].key).toBe('someKey');
63   });
64
65   it('does nothing if data is correct', () => {
66     component.data = [
67       {
68         key: 3,
69         value: 'something'
70       },
71       {
72         key: 'someKey',
73         value: 0
74       }
75     ];
76     component.ngOnInit();
77     expect(component.tableData.length).toBe(2);
78     expect(component.tableData[0].value).toBe('something');
79     expect(component.tableData[1].key).toBe('someKey');
80   });
81
82   it('throws errors if miss match', () => {
83     component.data = 38;
84     expect(() => component.ngOnInit()).toThrowError('Wrong data format');
85     component.data = [['someKey', 0, 3]];
86     expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
87     component.data = [{ somekey: 939, somethingElse: 'test' }];
88   });
89
90   describe('Class objects equal plain objects', () => {
91     class Example {
92       sth = 'something';
93       deep?: Example;
94       constructor(deep: boolean) {
95         if (deep) {
96           this.deep = new Example(false);
97         }
98       }
99     }
100
101     const classExample = new Example(true);
102     const objectExample = {
103       sth: 'something',
104       deep: {
105         sth: 'something'
106       }
107     };
108
109     const getTableData = (data) => {
110       component.data = data;
111       expect(() => component.ngOnInit()).not.toThrow();
112       return component.tableData;
113     };
114
115     const doesClassEqualsObject = (classData, objectData, dataLength) => {
116       const classTableData = getTableData(classData);
117       expect(classTableData).toEqual(getTableData(objectData));
118       expect(classTableData.length).toBe(dataLength);
119     };
120
121     it('should convert class objects the same way as plain objects', () => {
122       doesClassEqualsObject(classExample, objectExample, 1);
123       doesClassEqualsObject([classExample], [objectExample], 1);
124       component.renderObjects = true;
125       doesClassEqualsObject(classExample, objectExample, 2);
126       doesClassEqualsObject([classExample], [objectExample], 2);
127     });
128   });
129
130   it('tests _makePairs', () => {
131     expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
132     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
133     const pairInverse = [{ key: 'ceph', value: 'mimic' }, { key: 'dash', value: 'board' }];
134     expect(component._makePairs(pair)).toEqual(pairInverse);
135     expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
136     expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pairInverse);
137   });
138
139   it('tests _makePairsFromArray', () => {
140     expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
141       { key: 'dash', value: 'board' }
142     ]);
143     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
144     expect(component._makePairsFromArray(pair)).toEqual(pair);
145   });
146
147   it('tests _makePairsFromObject', () => {
148     expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
149       { key: 'dash', value: 'board' }
150     ]);
151     expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
152       { key: 'dash', value: 'board' },
153       { key: 'ceph', value: 'mimic' }
154     ]);
155   });
156
157   describe('tests _convertValue', () => {
158     const v = (value) => ({ key: 'sth', value: value });
159     const testConvertValue = (value, result) =>
160       expect(component._convertValue(v(value)).value).toBe(result);
161
162     it('should leave a string as it is', () => {
163       testConvertValue('something', 'something');
164     });
165
166     it('should leave an int as it is', () => {
167       testConvertValue(29, 29);
168     });
169
170     it('should convert arrays with any type to string', () => {
171       testConvertValue([1, 2, 3], '1, 2, 3');
172       testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
173       testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
174     });
175
176     it('should convert only allow objects if renderObjects is set to true', () => {
177       expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
178       component.renderObjects = true;
179       expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
180     });
181   });
182
183   describe('render objects', () => {
184     beforeEach(() => {
185       component.data = {
186         options: {
187           someSetting1: 38,
188           anotherSetting2: 'somethingElse',
189           suboptions: {
190             sub1: 12,
191             sub2: 34,
192             sub3: 56
193           }
194         },
195         someKey: 0,
196         o2: {
197           sub1: {
198             x: 42
199           },
200           sub2: {
201             y: 555
202           }
203         },
204         additionalKeyContainingObject: { type: 'none' },
205         keyWithEmptyObject: {}
206       };
207       component.renderObjects = true;
208     });
209
210     it('with parent key', () => {
211       component.ngOnInit();
212       expect(component.tableData).toEqual([
213         { key: 'additionalKeyContainingObject type', value: 'none' },
214         { key: 'keyWithEmptyObject', value: '' },
215         { key: 'o2 sub1 x', value: 42 },
216         { key: 'o2 sub2 y', value: 555 },
217         { key: 'options anotherSetting2', value: 'somethingElse' },
218         { key: 'options someSetting1', value: 38 },
219         { key: 'options suboptions sub1', value: 12 },
220         { key: 'options suboptions sub2', value: 34 },
221         { key: 'options suboptions sub3', value: 56 },
222         { key: 'someKey', value: 0 }
223       ]);
224     });
225
226     it('without parent key', () => {
227       component.appendParentKey = false;
228       component.ngOnInit();
229       expect(component.tableData).toEqual([
230         { key: 'anotherSetting2', value: 'somethingElse' },
231         { key: 'keyWithEmptyObject', value: '' },
232         { key: 'someKey', value: 0 },
233         { key: 'someSetting1', value: 38 },
234         { key: 'sub1', value: 12 },
235         { key: 'sub2', value: 34 },
236         { key: 'sub3', value: 56 },
237         { key: 'type', value: 'none' },
238         { key: 'x', value: 42 },
239         { key: 'y', value: 555 }
240       ]);
241     });
242   });
243
244   describe('subscribe fetchData', () => {
245     it('should not subscribe fetchData of table', () => {
246       component.ngOnInit();
247       expect(component.table.fetchData.observers.length).toBe(0);
248     });
249
250     it('should call fetchData', () => {
251       let called = false;
252       component.fetchData.subscribe(() => {
253         called = true;
254       });
255       component.ngOnInit();
256       expect(component.table.fetchData.observers.length).toBe(1);
257       component.table.fetchData.emit();
258       expect(called).toBeTruthy();
259     });
260   });
261
262   describe('hide empty items', () => {
263     beforeEach(() => {
264       component.data = {
265         string: '',
266         array: [],
267         object: {},
268         emptyObject: {
269           string: '',
270           array: [],
271           object: {}
272         },
273         someNumber: 0,
274         someDifferentNumber: 1,
275         someArray: [0, 1],
276         someString: '0',
277         someObject: {
278           empty: {},
279           something: 0.1
280         }
281       };
282       component.renderObjects = true;
283     });
284
285     it('should show all items as default', () => {
286       expect(component.hideEmpty).toBe(false);
287       component.ngOnInit();
288       expect(component.tableData).toEqual([
289         { key: 'array', value: '' },
290         { key: 'emptyObject array', value: '' },
291         { key: 'emptyObject object', value: '' },
292         { key: 'emptyObject string', value: '' },
293         { key: 'object', value: '' },
294         { key: 'someArray', value: '0, 1' },
295         { key: 'someDifferentNumber', value: 1 },
296         { key: 'someNumber', value: 0 },
297         { key: 'someObject empty', value: '' },
298         { key: 'someObject something', value: 0.1 },
299         { key: 'someString', value: '0' },
300         { key: 'string', value: '' }
301       ]);
302     });
303
304     it('should hide all empty items', () => {
305       component.hideEmpty = true;
306       component.ngOnInit();
307       expect(component.tableData).toEqual([
308         { key: 'someArray', value: '0, 1' },
309         { key: 'someDifferentNumber', value: 1 },
310         { key: 'someNumber', value: 0 },
311         { key: 'someObject something', value: 0.1 },
312         { key: 'someString', value: '0' }
313       ]);
314     });
315   });
316 });