]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
179bf3279fa53df7e7feb74d7ac99b0feb91020d
[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 { ComponentsModule } from '../../components/components.module';
8 import { configureTestBed } from '../../unit-test-helper';
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     expect(() => component.ngOnInit()).toThrowError(
89       'Wrong object array format: {key: string, value: any}[]'
90     );
91   });
92
93   it('tests _makePairs', () => {
94     expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
95     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
96     expect(component._makePairs(pair)).toEqual(pair);
97     expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
98     expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
99   });
100
101   it('tests _makePairsFromArray', () => {
102     expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
103       { key: 'dash', value: 'board' }
104     ]);
105     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
106     expect(component._makePairsFromArray(pair)).toEqual(pair);
107   });
108
109   it('tests _makePairsFromObject', () => {
110     expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
111       { key: 'dash', value: 'board' }
112     ]);
113     expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
114       { key: 'dash', value: 'board' },
115       { key: 'ceph', value: 'mimic' }
116     ]);
117   });
118
119   describe('tests _convertValue', () => {
120     const v = (value) => ({ key: 'sth', value: value });
121     const testConvertValue = (value, result) =>
122       expect(component._convertValue(v(value)).value).toBe(result);
123
124     it('should leave a string as it is', () => {
125       testConvertValue('something', 'something');
126     });
127
128     it('should leave an int as it is', () => {
129       testConvertValue(29, 29);
130     });
131
132     it('should convert arrays with any type to string', () => {
133       testConvertValue([1, 2, 3], '1, 2, 3');
134       testConvertValue([{ sth: 'something' }], '{"sth":"something"}');
135       testConvertValue([1, 'two', { 3: 'three' }], '1, two, {"3":"three"}');
136     });
137
138     it('should convert only allow objects if renderObjects is set to true', () => {
139       expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
140       component.renderObjects = true;
141       expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
142     });
143   });
144
145   it('tests _insertFlattenObjects', () => {
146     component.renderObjects = true;
147     const v = [
148       {
149         key: 'no',
150         value: 'change'
151       },
152       {
153         key: 'first',
154         value: {
155           second: {
156             l3_1: 33,
157             l3_2: 44
158           },
159           layer: 'something'
160         }
161       }
162     ];
163     expect(component._insertFlattenObjects(v)).toEqual([
164       { key: 'no', value: 'change' },
165       { key: 'first second l3_1', value: 33 },
166       { key: 'first second l3_2', value: 44 },
167       { key: 'first layer', value: 'something' }
168     ]);
169   });
170
171   describe('render objects', () => {
172     beforeEach(() => {
173       component.data = {
174         options: {
175           someSetting1: 38,
176           anotherSetting2: 'somethingElse',
177           suboptions: {
178             sub1: 12,
179             sub2: 34,
180             sub3: 56
181           }
182         },
183         someKey: 0
184       };
185       component.renderObjects = true;
186     });
187
188     it('with parent key', () => {
189       component.ngOnInit();
190       expect(component.tableData).toEqual([
191         { key: 'options someSetting1', value: 38 },
192         { key: 'options anotherSetting2', value: 'somethingElse' },
193         { key: 'options suboptions sub1', value: 12 },
194         { key: 'options suboptions sub2', value: 34 },
195         { key: 'options suboptions sub3', value: 56 },
196         { key: 'someKey', value: 0 }
197       ]);
198     });
199
200     it('without parent key', () => {
201       component.appendParentKey = false;
202       component.ngOnInit();
203       expect(component.tableData).toEqual([
204         { key: 'someSetting1', value: 38 },
205         { key: 'anotherSetting2', value: 'somethingElse' },
206         { key: 'sub1', value: 12 },
207         { key: 'sub2', value: 34 },
208         { key: 'sub3', value: 56 },
209         { key: 'someKey', value: 0 }
210       ]);
211     });
212   });
213 });