Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 8c1f835

Browse files
authored
Merge pull request #273 from ghiscoding/feat/multiple-select-filter-tests
feat(tests): add unit tests for SingleSelect & MultipleSelect Filters
2 parents d357148 + 8569970 commit 8c1f835

19 files changed

+1362
-237
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ You like and use this great library `Angular-Slickgrid`? Please upvote :star: an
3131
- version `2.x.x` for Angular 7+
3232

3333
#### Angular 8
34-
When running `ng update` to upgrade to Angular 8, one of the biggest change that is noticeable is that they change the target to `ES2015`, which does not play well with SlickGrid core library (which is all written in plain ES5 javascript). So for that reason, you need to switch back the `target` to `ES5` in your `tsconfig.json` file (`"target": "es5"`). This might be fixeable in the future, but for now that is the quick fix to get Angular 8 to work.
34+
When running `ng update` to upgrade to Angular 8, one of the biggest change that is noticeable is that they change the target to `ES2015`, which does not play well with SlickGrid core library (which is all written in plain ES5 javascript). So for that reason, you need to switch back the `target` to `ES5` in your `tsconfig.json` file (`"target": "es5"`). This might be fixable in the future, but for now that is the quick fix to get Angular 8 to work.
3535

3636
### Installation
3737
Refer to the [Wiki - HOWTO Step by Step](https://github.com/ghiscoding/angular-slickgrid/wiki/HOWTO---Step-by-Step)

src/app/modules/angular-slickgrid/editors/selectEditor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,9 @@ export class SelectEditor implements Editor {
432432
}
433433

434434
protected renderDomElement(collection: any[]) {
435-
if (!Array.isArray(collection) && this.collectionOptions && this.collectionOptions.collectionInObjectProperty) {
436-
collection = getDescendantProperty(collection, this.collectionOptions.collectionInObjectProperty);
435+
if (!Array.isArray(collection) && this.collectionOptions && (this.collectionOptions.collectionInsideObjectProperty || this.collectionOptions.collectionInObjectProperty)) {
436+
const collectionInsideObjectProperty = this.collectionOptions.collectionInsideObjectProperty || this.collectionOptions.collectionInObjectProperty;
437+
collection = getDescendantProperty(collection, collectionInsideObjectProperty);
437438
}
438439
if (!Array.isArray(collection)) {
439440
throw new Error('The "collection" passed to the Select Editor is not a valid array');
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// import 3rd party lib multiple-select for the tests
2+
import '../../../../../assets/lib/multiple-select/multiple-select';
3+
4+
import { TestBed } from '@angular/core/testing';
5+
import { TranslateService, TranslateModule } from '@ngx-translate/core';
6+
import { Column, FilterArguments, GridOption } from '../../models';
7+
import { CollectionService } from '../../services/collection.service';
8+
import { Filters } from '..';
9+
import { MultipleSelectFilter } from '../multipleSelectFilter';
10+
import { of, Subject } from 'rxjs';
11+
12+
const containerId = 'demo-container';
13+
14+
// define a <div> container to simulate the grid container
15+
const template = `<div id="${containerId}"></div>`;
16+
17+
const gridOptionMock = {
18+
enableFiltering: true,
19+
enableFilterTrimWhiteSpace: true,
20+
} as GridOption;
21+
22+
const gridStub = {
23+
getOptions: () => gridOptionMock,
24+
getColumns: jest.fn(),
25+
getHeaderRowColumn: jest.fn(),
26+
render: jest.fn(),
27+
};
28+
29+
describe('MultipleSelectFilter', () => {
30+
let divContainer: HTMLDivElement;
31+
let filter: MultipleSelectFilter;
32+
let filterArguments: FilterArguments;
33+
let spyGetHeaderRow;
34+
let mockColumn: Column;
35+
let collectionService: CollectionService;
36+
let translate: TranslateService;
37+
38+
beforeEach(() => {
39+
divContainer = document.createElement('div');
40+
divContainer.innerHTML = template;
41+
document.body.appendChild(divContainer);
42+
spyGetHeaderRow = jest.spyOn(gridStub, 'getHeaderRowColumn').mockReturnValue(divContainer);
43+
44+
mockColumn = {
45+
id: 'gender', field: 'gender', filterable: true,
46+
filter: {
47+
model: Filters.multipleSelect,
48+
}
49+
};
50+
filterArguments = {
51+
grid: gridStub,
52+
columnDef: mockColumn,
53+
callback: jest.fn()
54+
};
55+
56+
TestBed.configureTestingModule({
57+
providers: [CollectionService],
58+
imports: [TranslateModule.forRoot()]
59+
});
60+
collectionService = TestBed.get(CollectionService);
61+
translate = TestBed.get(TranslateService);
62+
filter = new MultipleSelectFilter(translate, collectionService);
63+
});
64+
65+
afterEach(() => {
66+
filter.destroy();
67+
});
68+
69+
it('should be a multiple-select filter', () => {
70+
mockColumn.filter.collection = [{ value: 'male', label: 'male' }, { value: 'female', label: 'female' }];
71+
filter = new MultipleSelectFilter(translate, collectionService);
72+
filter.init(filterArguments, true);
73+
const filterCount = divContainer.querySelectorAll('select.ms-filter.search-filter.filter-gender').length;
74+
75+
expect(spyGetHeaderRow).toHaveBeenCalled();
76+
expect(filterCount).toBe(1);
77+
expect(filter.isMultipleSelect).toBe(true);
78+
});
79+
});

0 commit comments

Comments
 (0)