|
| 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