]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
7ff6dd7e175cb1d1222e73431aa694369281bcea
[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     expect(component._makePairs(pair)).toEqual(pair);
134     expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
135     expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
136   });
137
138   it('tests _makePairsFromArray', () => {
139     expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
140       { key: 'dash', value: 'board' }
141     ]);
142     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
143     expect(component._makePairsFromArray(pair)).toEqual(pair);
144   });
145
146   it('tests _makePairsFromObject', () => {
147     expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
148       { key: 'dash', value: 'board' }
149     ]);
150     expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
151       { key: 'dash', value: 'board' },
152       { key: 'ceph', value: 'mimic' }
153     ]);
154   });
155
156   describe('tests _convertValue', () => {
157     const v = (value) => ({ key: 'sth', value: value });
158     const testConvertValue = (value, result) =>
159       expect(component._convertValue(v(value)).value).toBe(result);
160
161     it('should leave a string as it is', () => {
162       testConvertValue('something', 'something');
163     });
164
165     it('should leave an int as it is', () => {
166       testConvertValue(29, 29);
167     });
168
169     it('should convert arrays with any type to string', () => {
170       testConvertValue([1, 2, 3], '1, 2, 3');
171       testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
172       testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
173     });
174
175     it('should convert only allow objects if renderObjects is set to true', () => {
176       expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
177       component.renderObjects = true;
178       expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
179     });
180   });
181
182   it('tests _insertFlattenObjects', () => {
183     component.renderObjects = true;
184     const v = [
185       {
186         key: 'no',
187         value: 'change'
188       },
189       {
190         key: 'first',
191         value: {
192           second: {
193             l3_1: 33,
194             l3_2: 44
195           },
196           layer: 'something'
197         }
198       }
199     ];
200     expect(component._insertFlattenObjects(v)).toEqual([
201       { key: 'no', value: 'change' },
202       { key: 'first layer', value: 'something' },
203       { key: 'first second l3_1', value: 33 },
204       { key: 'first second l3_2', value: 44 }
205     ]);
206   });
207
208   describe('render objects', () => {
209     beforeEach(() => {
210       component.data = {
211         options: {
212           someSetting1: 38,
213           anotherSetting2: 'somethingElse',
214           suboptions: {
215             sub1: 12,
216             sub2: 34,
217             sub3: 56
218           }
219         },
220         someKey: 0,
221         additionalKeyContainingObject: { type: 'none' },
222         keyWithEmptyObject: {}
223       };
224       component.renderObjects = true;
225     });
226
227     it('with parent key', () => {
228       component.ngOnInit();
229       expect(component.tableData).toEqual([
230         { key: 'someKey', value: 0 },
231         { key: 'keyWithEmptyObject', value: '' },
232         { key: 'options someSetting1', value: 38 },
233         { key: 'options anotherSetting2', value: 'somethingElse' },
234         { key: 'options suboptions sub1', value: 12 },
235         { key: 'options suboptions sub2', value: 34 },
236         { key: 'options suboptions sub3', value: 56 },
237         { key: 'additionalKeyContainingObject type', value: 'none' }
238       ]);
239     });
240
241     it('without parent key', () => {
242       component.appendParentKey = false;
243       component.ngOnInit();
244       expect(component.tableData).toEqual([
245         { key: 'someKey', value: 0 },
246         { key: 'keyWithEmptyObject', value: '' },
247         { key: 'someSetting1', value: 38 },
248         { key: 'anotherSetting2', value: 'somethingElse' },
249         { key: 'sub1', value: 12 },
250         { key: 'sub2', value: 34 },
251         { key: 'sub3', value: 56 },
252         { key: 'type', value: 'none' }
253       ]);
254     });
255   });
256
257   describe('subscribe fetchData', () => {
258     it('should not subscribe fetchData of table', () => {
259       component.ngOnInit();
260       expect(component.table.fetchData.observers.length).toBe(0);
261     });
262
263     it('should call fetchData', () => {
264       let called = false;
265       component.fetchData.subscribe(() => {
266         called = true;
267       });
268       component.ngOnInit();
269       expect(component.table.fetchData.observers.length).toBe(1);
270       component.table.fetchData.emit();
271       expect(called).toBeTruthy();
272     });
273   });
274 });