]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
53e89e53f9296b0202d0aa2a53ef22508169f7bd
[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     expect(component._convertValue).toBeTruthy();
29     expect(component._makePairs).toBeTruthy();
30     expect(component._makePairsFromObject).toBeTruthy();
31     expect(component._makePairsFromArray).toBeTruthy();
32     expect(component._insertFlattenObjects).toBeTruthy();
33   });
34
35   it('should make key value object pairs out of arrays with length two', () => {
36     component.data = [['someKey', 0], [3, 'something']];
37     component.ngOnInit();
38     expect(component.tableData.length).toBe(2);
39     expect(component.tableData[0].key).toBe('someKey');
40     expect(component.tableData[1].value).toBe('something');
41   });
42
43   it('should transform arrays', () => {
44     component.data = [['someKey', [1, 2, 3]], [3, 'something']];
45     component.ngOnInit();
46     expect(component.tableData.length).toBe(2);
47     expect(component.tableData[0].key).toBe('someKey');
48     expect(component.tableData[0].value).toBe('1, 2, 3');
49     expect(component.tableData[1].value).toBe('something');
50   });
51
52   it('should remove pure object values', () => {
53     component.data = [[3, 'something'], ['will be removed', { a: 3, b: 4, c: 5 }]];
54     component.ngOnInit();
55     expect(component.tableData.length).toBe(1);
56     expect(component.tableData[0].value).toBe('something');
57   });
58
59   it('makes key value object pairs out of an object', () => {
60     component.data = {
61       3: 'something',
62       someKey: 0
63     };
64     component.ngOnInit();
65     expect(component.tableData.length).toBe(2);
66     expect(component.tableData[0].value).toBe('something');
67     expect(component.tableData[1].key).toBe('someKey');
68   });
69
70   it('does nothing if data is correct', () => {
71     component.data = [
72       {
73         key: 3,
74         value: 'something'
75       },
76       {
77         key: 'someKey',
78         value: 0
79       }
80     ];
81     component.ngOnInit();
82     expect(component.tableData.length).toBe(2);
83     expect(component.tableData[0].value).toBe('something');
84     expect(component.tableData[1].key).toBe('someKey');
85   });
86
87   it('throws errors if miss match', () => {
88     component.data = 38;
89     expect(() => component.ngOnInit()).toThrowError('Wrong data format');
90     component.data = [['someKey', 0, 3]];
91     expect(() => component.ngOnInit()).toThrowError('Wrong array format: [string, any][]');
92     component.data = [{ somekey: 939, somethingElse: 'test' }];
93     expect(() => component.ngOnInit()).toThrowError(
94       'Wrong object array format: {key: string, value: any}[]'
95     );
96   });
97
98   it('tests _makePairs', () => {
99     expect(component._makePairs([['dash', 'board']])).toEqual([{ key: 'dash', value: 'board' }]);
100     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
101     expect(component._makePairs(pair)).toEqual(pair);
102     expect(component._makePairs({ dash: 'board' })).toEqual([{ key: 'dash', value: 'board' }]);
103     expect(component._makePairs({ dash: 'board', ceph: 'mimic' })).toEqual(pair);
104   });
105
106   it('tests _makePairsFromArray', () => {
107     expect(component._makePairsFromArray([['dash', 'board']])).toEqual([
108       { key: 'dash', value: 'board' }
109     ]);
110     const pair = [{ key: 'dash', value: 'board' }, { key: 'ceph', value: 'mimic' }];
111     expect(component._makePairsFromArray(pair)).toEqual(pair);
112   });
113
114   it('tests _makePairsFromObject', () => {
115     expect(component._makePairsFromObject({ dash: 'board' })).toEqual([
116       { key: 'dash', value: 'board' }
117     ]);
118     expect(component._makePairsFromObject({ dash: 'board', ceph: 'mimic' })).toEqual([
119       { key: 'dash', value: 'board' },
120       { key: 'ceph', value: 'mimic' }
121     ]);
122   });
123
124   it('tests _convertValue', () => {
125     const v = (value) => ({ key: 'sth', value: value });
126     expect(component._convertValue(v('something'))).toEqual(v('something'));
127     expect(component._convertValue(v([1, 2, 3]))).toEqual(v('1, 2, 3'));
128     expect(component._convertValue(v({ sth: 'something' }))).toBe(undefined);
129     component.renderObjects = true;
130     expect(component._convertValue(v({ sth: 'something' }))).toEqual(v({ sth: 'something' }));
131   });
132
133   it('tests _insertFlattenObjects', () => {
134     component.renderObjects = true;
135     const v = [
136       {
137         key: 'no',
138         value: 'change'
139       },
140       {
141         key: 'first',
142         value: {
143           second: {
144             l3_1: 33,
145             l3_2: 44
146           },
147           layer: 'something'
148         }
149       }
150     ];
151     expect(component._insertFlattenObjects(v)).toEqual([
152       { key: 'no', value: 'change' },
153       { key: 'first second l3_1', value: 33 },
154       { key: 'first second l3_2', value: 44 },
155       { key: 'first layer', value: 'something' }
156     ]);
157   });
158
159   describe('render objects', () => {
160     beforeEach(() => {
161       component.data = {
162         options: {
163           someSetting1: 38,
164           anotherSetting2: 'somethingElse',
165           suboptions: {
166             sub1: 12,
167             sub2: 34,
168             sub3: 56
169           }
170         },
171         someKey: 0
172       };
173       component.renderObjects = true;
174     });
175
176     it('with parent key', () => {
177       component.ngOnInit();
178       expect(component.tableData).toEqual([
179         { key: 'options someSetting1', value: 38 },
180         { key: 'options anotherSetting2', value: 'somethingElse' },
181         { key: 'options suboptions sub1', value: 12 },
182         { key: 'options suboptions sub2', value: 34 },
183         { key: 'options suboptions sub3', value: 56 },
184         { key: 'someKey', value: 0 }
185       ]);
186     });
187
188     it('without parent key', () => {
189       component.appendParentKey = false;
190       component.ngOnInit();
191       expect(component.tableData).toEqual([
192         { key: 'someSetting1', value: 38 },
193         { key: 'anotherSetting2', value: 'somethingElse' },
194         { key: 'sub1', value: 12 },
195         { key: 'sub2', value: 34 },
196         { key: 'sub3', value: 56 },
197         { key: 'someKey', value: 0 }
198       ]);
199     });
200   });
201 });