]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table/table.component.spec.ts
udpdate/drop patches for 15.2.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / datatable / table / table.component.spec.ts
CommitLineData
11fdf7f2
TL
1import { ComponentFixture, TestBed } from '@angular/core/testing';
2import { FormsModule } from '@angular/forms';
3import { RouterTestingModule } from '@angular/router/testing';
4
5import { NgxDatatableModule } from '@swimlane/ngx-datatable';
6import * as _ from 'lodash';
9f95a23c 7import { BsDropdownModule } from 'ngx-bootstrap/dropdown';
11fdf7f2
TL
8
9import { configureTestBed } from '../../../../testing/unit-test-helper';
10import { ComponentsModule } from '../../components/components.module';
9f95a23c 11import { CdTableColumnFilter } from '../../models/cd-table-column-filter';
11fdf7f2 12import { CdTableFetchDataContext } from '../../models/cd-table-fetch-data-context';
9f95a23c 13import { PipesModule } from '../../pipes/pipes.module';
11fdf7f2
TL
14import { TableComponent } from './table.component';
15
16describe('TableComponent', () => {
17 let component: TableComponent;
18 let fixture: ComponentFixture<TableComponent>;
19
9f95a23c 20 const createFakeData = (n: number) => {
11fdf7f2
TL
21 const data = [];
22 for (let i = 0; i < n; i++) {
23 data.push({
24 a: i,
9f95a23c
TL
25 b: i * 10,
26 c: !!(i % 2)
11fdf7f2
TL
27 });
28 }
29 return data;
30 };
31
32 const clearLocalStorage = () => {
33 component.localStorage.clear();
34 };
35
36 configureTestBed({
37 declarations: [TableComponent],
9f95a23c
TL
38 imports: [
39 NgxDatatableModule,
40 FormsModule,
41 ComponentsModule,
42 RouterTestingModule,
43 BsDropdownModule.forRoot(),
44 PipesModule
45 ]
11fdf7f2
TL
46 });
47
48 beforeEach(() => {
49 fixture = TestBed.createComponent(TableComponent);
50 component = fixture.componentInstance;
11fdf7f2 51
9f95a23c 52 component.data = createFakeData(10);
11fdf7f2 53 component.columns = [
9f95a23c
TL
54 { prop: 'a', name: 'Index', filterable: true },
55 { prop: 'b', name: 'Index times ten' },
56 { prop: 'c', name: 'Odd?', filterable: true }
11fdf7f2
TL
57 ];
58 });
59
60 it('should create', () => {
61 expect(component).toBeTruthy();
62 });
63
9f95a23c
TL
64 it('should force an identifier', () => {
65 component.identifier = 'x';
66 component.forceIdentifier = true;
67 component.ngOnInit();
68 expect(component.identifier).toBe('x');
69 expect(component.sorts[0].prop).toBe('a');
70 expect(component.sorts).toEqual(component.createSortingDefinition('a'));
71 });
11fdf7f2 72
9f95a23c
TL
73 it('should have rows', () => {
74 component.useData();
75 expect(component.data.length).toBe(10);
76 expect(component.rows.length).toBe(component.data.length);
77 });
11fdf7f2 78
9f95a23c
TL
79 it('should have an int in setLimit parsing a string', () => {
80 expect(component.limit).toBe(10);
81 expect(component.limit).toEqual(jasmine.any(Number));
82
83 const e = { target: { value: '1' } };
84 component.setLimit(e);
85 expect(component.userConfig.limit).toBe(1);
86 expect(component.userConfig.limit).toEqual(jasmine.any(Number));
87 e.target.value = '-20';
88 component.setLimit(e);
89 expect(component.userConfig.limit).toBe(1);
90 });
11fdf7f2 91
9f95a23c
TL
92 it('should prevent propagation of mouseenter event', (done) => {
93 let wasCalled = false;
94 const mouseEvent = new MouseEvent('mouseenter');
95 mouseEvent.stopPropagation = () => {
96 wasCalled = true;
97 };
98 spyOn(component.table.element, 'addEventListener').and.callFake((eventName, fn) => {
99 fn(mouseEvent);
100 expect(eventName).toBe('mouseenter');
101 expect(wasCalled).toBe(true);
102 done();
11fdf7f2 103 });
9f95a23c
TL
104 component.ngOnInit();
105 });
11fdf7f2 106
9f95a23c
TL
107 describe('test column filtering', () => {
108 let filterIndex: CdTableColumnFilter;
109 let filterOdd: CdTableColumnFilter;
110 let filterCustom: CdTableColumnFilter;
111
112 const expectColumnFilterCreated = (
113 filter: CdTableColumnFilter,
114 prop: string,
115 options: string[],
116 value?: { raw: string; formatted: string }
117 ) => {
118 expect(filter.column.prop).toBe(prop);
119 expect(_.map(filter.options, 'raw')).toEqual(options);
120 expect(filter.value).toEqual(value);
121 };
122
123 const expectColumnFiltered = (
124 changes: { filter: CdTableColumnFilter; value?: string }[],
125 results: any[],
126 search: string = ''
127 ) => {
128 component.search = search;
129 _.forEach(changes, (change) => {
130 component.onChangeFilter(
131 change.filter,
132 change.value ? { raw: change.value, formatted: change.value } : undefined
133 );
494da23a 134 });
9f95a23c
TL
135 expect(component.rows).toEqual(results);
136 component.onClearSearch();
137 component.onClearFilters();
138 };
81eedcae 139
9f95a23c
TL
140 describe('with visible columns', () => {
141 beforeEach(() => {
142 component.initColumnFilters();
143 component.updateColumnFilterOptions();
144 filterIndex = component.columnFilters[0];
145 filterOdd = component.columnFilters[1];
146 });
11fdf7f2 147
9f95a23c
TL
148 it('should have filters initialized', () => {
149 expect(component.columnFilters.length).toBe(2);
150 expectColumnFilterCreated(
151 filterIndex,
152 'a',
153 _.map(component.data, (row) => _.toString(row.a))
154 );
155 expectColumnFilterCreated(filterOdd, 'c', ['false', 'true']);
11fdf7f2
TL
156 });
157
9f95a23c
TL
158 it('should add filters', () => {
159 // single
160 expectColumnFiltered([{ filter: filterIndex, value: '1' }], [{ a: 1, b: 10, c: true }]);
161
162 // multiple
163 expectColumnFiltered(
164 [{ filter: filterOdd, value: 'false' }, { filter: filterIndex, value: '2' }],
165 [{ a: 2, b: 20, c: false }]
166 );
167
168 // Clear should work
169 expect(component.rows).toEqual(component.data);
11fdf7f2
TL
170 });
171
9f95a23c
TL
172 it('should remove filters', () => {
173 // single
174 expectColumnFiltered(
175 [
176 { filter: filterOdd, value: 'true' },
177 { filter: filterIndex, value: '1' },
178 { filter: filterIndex, value: undefined }
179 ],
180 [
181 { a: 1, b: 10, c: true },
182 { a: 3, b: 30, c: true },
183 { a: 5, b: 50, c: true },
184 { a: 7, b: 70, c: true },
185 { a: 9, b: 90, c: true }
186 ]
187 );
188
189 // multiple
190 expectColumnFiltered(
191 [
192 { filter: filterOdd, value: 'true' },
193 { filter: filterIndex, value: '1' },
194 { filter: filterIndex, value: undefined },
195 { filter: filterOdd, value: undefined }
196 ],
197 component.data
198 );
199
200 // a selected filter should be removed if it's selected again
201 expectColumnFiltered(
202 [
203 { filter: filterOdd, value: 'true' },
204 { filter: filterIndex, value: '1' },
205 { filter: filterIndex, value: '1' }
206 ],
207 [
208 { a: 1, b: 10, c: true },
209 { a: 3, b: 30, c: true },
210 { a: 5, b: 50, c: true },
211 { a: 7, b: 70, c: true },
212 { a: 9, b: 90, c: true }
213 ]
214 );
11fdf7f2
TL
215 });
216
9f95a23c
TL
217 it('should search from filtered rows', () => {
218 expectColumnFiltered(
219 [{ filter: filterOdd, value: 'true' }],
220 [{ a: 9, b: 90, c: true }],
221 '9'
222 );
223
224 // Clear should work
225 expect(component.rows).toEqual(component.data);
11fdf7f2 226 });
9f95a23c 227 });
11fdf7f2 228
9f95a23c
TL
229 describe('with custom columns', () => {
230 beforeEach(() => {
231 // create a new additional column in data
232 for (let i = 0; i < component.data.length; i++) {
233 const row = component.data[i];
234 row['d'] = row.a;
235 }
236 // create a custom column filter
237 component.extraFilterableColumns = [
238 {
239 name: 'd less than 5',
240 prop: 'd',
241 filterOptions: ['yes', 'no'],
242 filterInitValue: 'yes',
243 filterPredicate: (row, value) => {
244 if (value === 'yes') {
245 return row.d < 5;
246 } else {
247 return row.d >= 5;
248 }
249 }
250 }
251 ];
252 component.initColumnFilters();
253 component.updateColumnFilterOptions();
254 filterIndex = component.columnFilters[0];
255 filterOdd = component.columnFilters[1];
256 filterCustom = component.columnFilters[2];
11fdf7f2
TL
257 });
258
9f95a23c
TL
259 it('should have filters initialized', () => {
260 expect(component.columnFilters.length).toBe(3);
261 expectColumnFilterCreated(filterCustom, 'd', ['yes', 'no'], {
262 raw: 'yes',
263 formatted: 'yes'
264 });
265 component.useData();
266 expect(component.rows).toEqual(_.slice(component.data, 0, 5));
11fdf7f2
TL
267 });
268
9f95a23c
TL
269 it('should remove filters', () => {
270 expectColumnFiltered([{ filter: filterCustom, value: 'no' }], _.slice(component.data, 5));
11fdf7f2 271 });
9f95a23c
TL
272 });
273 });
274
275 describe('test search', () => {
276 const expectSearch = (keyword: string, expectedResult: object[]) => {
277 component.search = keyword;
278 component.updateFilter();
279 expect(component.rows).toEqual(expectedResult);
280 component.onClearSearch();
281 };
282
283 describe('searchableObjects', () => {
284 const testObject = {
285 obj: {
286 min: 8,
287 max: 123
288 }
289 };
11fdf7f2 290
9f95a23c
TL
291 beforeEach(() => {
292 component.data = [testObject];
293 component.columns = [{ prop: 'obj', name: 'Object' }];
11fdf7f2
TL
294 });
295
9f95a23c
TL
296 it('should not search through objects as default case', () => {
297 expect(component.searchableObjects).toBe(false);
298 expectSearch('8', []);
11fdf7f2
TL
299 });
300
9f95a23c
TL
301 it('should search through objects if searchableObjects is set to true', () => {
302 component.searchableObjects = true;
303 expectSearch('28', []);
304 expectSearch('8', [testObject]);
305 expectSearch('123', [testObject]);
306 expectSearch('max', [testObject]);
11fdf7f2
TL
307 });
308 });
9f95a23c
TL
309
310 it('should find a particular number', () => {
311 expectSearch('5', [{ a: 5, b: 50, c: true }]);
312 expectSearch('9', [{ a: 9, b: 90, c: true }]);
313 });
314
315 it('should find boolean values', () => {
316 expectSearch('true', [
317 { a: 1, b: 10, c: true },
318 { a: 3, b: 30, c: true },
319 { a: 5, b: 50, c: true },
320 { a: 7, b: 70, c: true },
321 { a: 9, b: 90, c: true }
322 ]);
323 expectSearch('false', [
324 { a: 0, b: 0, c: false },
325 { a: 2, b: 20, c: false },
326 { a: 4, b: 40, c: false },
327 { a: 6, b: 60, c: false },
328 { a: 8, b: 80, c: false }
329 ]);
330 });
331
332 it('should test search keyword preparation', () => {
333 const prepare = TableComponent.prepareSearch;
334 const expected = ['a', 'b', 'c'];
335 expect(prepare('a b c')).toEqual(expected);
336 expect(prepare('a,, b,, c')).toEqual(expected);
337 expect(prepare('a,,,, b,,, c')).toEqual(expected);
338 expect(prepare('a+b c')).toEqual(['a+b', 'c']);
339 expect(prepare('a,,,+++b,,, c')).toEqual(['a+++b', 'c']);
340 expect(prepare('"a b c" "d e f", "g, h i"')).toEqual(['a+b+c', 'd+e++f', 'g+h+i']);
341 });
342
343 it('should search for multiple values', () => {
344 expectSearch('2 20 false', [{ a: 2, b: 20, c: false }]);
345 expectSearch('false 2', [{ a: 2, b: 20, c: false }]);
346 });
347
348 it('should filter by column', () => {
349 expectSearch('index:5', [{ a: 5, b: 50, c: true }]);
350 expectSearch('times:50', [{ a: 5, b: 50, c: true }]);
351 expectSearch('times:50 index:5', [{ a: 5, b: 50, c: true }]);
352 expectSearch('Odd?:true', [
353 { a: 1, b: 10, c: true },
354 { a: 3, b: 30, c: true },
355 { a: 5, b: 50, c: true },
356 { a: 7, b: 70, c: true },
357 { a: 9, b: 90, c: true }
358 ]);
359 component.data = createFakeData(100);
360 expectSearch('index:1 odd:true times:110', [{ a: 11, b: 110, c: true }]);
361 });
362
363 it('should search through arrays', () => {
364 component.columns = [{ prop: 'a', name: 'Index' }, { prop: 'b', name: 'ArrayColumn' }];
365
366 component.data = [{ a: 1, b: ['foo', 'bar'] }, { a: 2, b: ['baz', 'bazinga'] }];
367 expectSearch('bar', [{ a: 1, b: ['foo', 'bar'] }]);
368 expectSearch('arraycolumn:bar arraycolumn:foo', [{ a: 1, b: ['foo', 'bar'] }]);
369 expectSearch('arraycolumn:baz arraycolumn:inga', [{ a: 2, b: ['baz', 'bazinga'] }]);
370
371 component.data = [{ a: 1, b: [1, 2] }, { a: 2, b: [3, 4] }];
372 expectSearch('arraycolumn:1 arraycolumn:2', [{ a: 1, b: [1, 2] }]);
373 });
374
375 it('should search with spaces', () => {
376 const expectedResult = [{ a: 2, b: 20, c: false }];
377 expectSearch(`'Index times ten':20`, expectedResult);
378 expectSearch('index+times+ten:20', expectedResult);
379 });
380
381 it('should filter results although column name is incomplete', () => {
382 component.data = createFakeData(3);
383 expectSearch(`'Index times ten'`, []);
384 expectSearch(`'Ind'`, []);
385 expectSearch(`'Ind:'`, [
386 { a: 0, b: 0, c: false },
387 { a: 1, b: 10, c: true },
388 { a: 2, b: 20, c: false }
389 ]);
390 });
391
392 it('should search if column name is incomplete', () => {
393 const expectedData = [
394 { a: 0, b: 0, c: false },
395 { a: 1, b: 10, c: true },
396 { a: 2, b: 20, c: false }
397 ];
398 component.data = _.clone(expectedData);
399 expectSearch('inde', []);
400 expectSearch('index:', expectedData);
401 expectSearch('index times te', []);
402 });
403
404 it('should restore full table after search', () => {
405 component.useData();
406 expect(component.rows.length).toBe(10);
407 component.search = '3';
408 component.updateFilter();
409 expect(component.rows.length).toBe(1);
410 component.onClearSearch();
411 expect(component.rows.length).toBe(10);
412 });
11fdf7f2
TL
413 });
414
415 describe('after ngInit', () => {
9f95a23c 416 const toggleColumn = (prop: string, checked: boolean) => {
11fdf7f2
TL
417 component.toggleColumn({
418 target: {
419 name: prop,
420 checked: checked
421 }
422 });
423 };
424
425 const equalStorageConfig = () => {
426 expect(JSON.stringify(component.userConfig)).toBe(
427 component.localStorage.getItem(component.tableName)
428 );
429 };
430
431 beforeEach(() => {
432 component.ngOnInit();
433 });
434
435 it('should have updated the column definitions', () => {
436 expect(component.columns[0].flexGrow).toBe(1);
437 expect(component.columns[1].flexGrow).toBe(2);
438 expect(component.columns[2].flexGrow).toBe(2);
439 expect(component.columns[2].resizeable).toBe(false);
440 });
441
442 it('should have table columns', () => {
9f95a23c 443 expect(component.tableColumns.length).toBe(3);
11fdf7f2
TL
444 expect(component.tableColumns).toEqual(component.columns);
445 });
446
447 it('should have a unique identifier which it searches for', () => {
448 expect(component.identifier).toBe('a');
449 expect(component.userConfig.sorts[0].prop).toBe('a');
450 expect(component.userConfig.sorts).toEqual(component.createSortingDefinition('a'));
451 equalStorageConfig();
452 });
453
454 it('should remove column "a"', () => {
455 expect(component.userConfig.sorts[0].prop).toBe('a');
456 toggleColumn('a', false);
457 expect(component.userConfig.sorts[0].prop).toBe('b');
9f95a23c 458 expect(component.tableColumns.length).toBe(2);
11fdf7f2
TL
459 equalStorageConfig();
460 });
461
462 it('should not be able to remove all columns', () => {
463 expect(component.userConfig.sorts[0].prop).toBe('a');
464 toggleColumn('a', false);
465 toggleColumn('b', false);
466 toggleColumn('c', false);
9f95a23c 467 expect(component.userConfig.sorts[0].prop).toBe('c');
11fdf7f2
TL
468 expect(component.tableColumns.length).toBe(1);
469 equalStorageConfig();
470 });
471
472 it('should enable column "a" again', () => {
473 expect(component.userConfig.sorts[0].prop).toBe('a');
474 toggleColumn('a', false);
475 toggleColumn('a', true);
476 expect(component.userConfig.sorts[0].prop).toBe('b');
9f95a23c 477 expect(component.tableColumns.length).toBe(3);
11fdf7f2
TL
478 equalStorageConfig();
479 });
480
481 afterEach(() => {
482 clearLocalStorage();
483 });
484 });
485
486 describe('reload data', () => {
487 beforeEach(() => {
488 component.ngOnInit();
489 component.data = [];
490 component['updating'] = false;
491 });
492
493 it('should call fetchData callback function', () => {
9f95a23c 494 component.fetchData.subscribe((context: any) => {
11fdf7f2
TL
495 expect(context instanceof CdTableFetchDataContext).toBeTruthy();
496 });
497 component.reloadData();
498 });
499
500 it('should call error function', () => {
501 component.data = createFakeData(5);
9f95a23c 502 component.fetchData.subscribe((context: any) => {
11fdf7f2
TL
503 context.error();
504 expect(component.loadingError).toBeTruthy();
505 expect(component.data.length).toBe(0);
506 expect(component.loadingIndicator).toBeFalsy();
507 expect(component['updating']).toBeFalsy();
508 });
509 component.reloadData();
510 });
511
512 it('should call error function with custom config', () => {
513 component.data = createFakeData(10);
9f95a23c 514 component.fetchData.subscribe((context: any) => {
11fdf7f2
TL
515 context.errorConfig.resetData = false;
516 context.errorConfig.displayError = false;
517 context.error();
518 expect(component.loadingError).toBeFalsy();
519 expect(component.data.length).toBe(10);
520 expect(component.loadingIndicator).toBeFalsy();
521 expect(component['updating']).toBeFalsy();
522 });
523 component.reloadData();
524 });
525
526 it('should update selection on refresh - "onChange"', () => {
527 spyOn(component, 'onSelect').and.callThrough();
528 component.data = createFakeData(10);
529 component.selection.selected = [_.clone(component.data[1])];
530 component.updateSelectionOnRefresh = 'onChange';
531 component.updateSelected();
532 expect(component.onSelect).toHaveBeenCalledTimes(0);
533 component.data[1].d = !component.data[1].d;
534 component.updateSelected();
535 expect(component.onSelect).toHaveBeenCalled();
536 });
537
538 it('should update selection on refresh - "always"', () => {
539 spyOn(component, 'onSelect').and.callThrough();
540 component.data = createFakeData(10);
541 component.selection.selected = [_.clone(component.data[1])];
542 component.updateSelectionOnRefresh = 'always';
543 component.updateSelected();
544 expect(component.onSelect).toHaveBeenCalled();
545 component.data[1].d = !component.data[1].d;
546 component.updateSelected();
547 expect(component.onSelect).toHaveBeenCalled();
548 });
549
550 it('should update selection on refresh - "never"', () => {
551 spyOn(component, 'onSelect').and.callThrough();
552 component.data = createFakeData(10);
553 component.selection.selected = [_.clone(component.data[1])];
554 component.updateSelectionOnRefresh = 'never';
555 component.updateSelected();
556 expect(component.onSelect).toHaveBeenCalledTimes(0);
557 component.data[1].d = !component.data[1].d;
558 component.updateSelected();
559 expect(component.onSelect).toHaveBeenCalledTimes(0);
560 });
561
562 afterEach(() => {
563 clearLocalStorage();
564 });
565 });
566
567 describe('useCustomClass', () => {
568 beforeEach(() => {
569 component.customCss = {
9f95a23c 570 'badge badge-danger': 'active',
11fdf7f2 571 'secret secret-number': 123.456,
9f95a23c 572 btn: (v) => _.isString(v) && v.startsWith('http'),
11fdf7f2
TL
573 secure: (v) => _.isString(v) && v.startsWith('https')
574 };
575 });
576
577 it('should throw an error if custom classes are not set', () => {
578 component.customCss = undefined;
579 expect(() => component.useCustomClass('active')).toThrowError('Custom classes are not set!');
580 });
581
582 it('should not return any class', () => {
583 ['', 'something', 123, { complex: 1 }, [1, 2, 3]].forEach((value) =>
584 expect(component.useCustomClass(value)).toBe(undefined)
585 );
586 });
587
588 it('should match a string and return the corresponding class', () => {
9f95a23c 589 expect(component.useCustomClass('active')).toBe('badge badge-danger');
11fdf7f2
TL
590 });
591
592 it('should match a number and return the corresponding class', () => {
593 expect(component.useCustomClass(123.456)).toBe('secret secret-number');
594 });
595
596 it('should match against a function and return the corresponding class', () => {
9f95a23c 597 expect(component.useCustomClass('http://no.ssl')).toBe('btn');
11fdf7f2
TL
598 });
599
600 it('should match against multiple functions and return the corresponding classes', () => {
9f95a23c 601 expect(component.useCustomClass('https://secure.it')).toBe('btn secure');
11fdf7f2
TL
602 });
603 });
604});