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