|
| 1 | +import { JQueryUiSliderOption } from '../../models/jQueryUiSliderOption.interface'; |
| 2 | +import { GridOption, FilterArguments, Column } from '../../models'; |
| 3 | +import { Filters } from '..'; |
| 4 | +import { SliderFilter } from '../sliderFilter'; |
| 5 | + |
| 6 | +const containerId = 'demo-container'; |
| 7 | + |
| 8 | +// define a <div> container to simulate the grid container |
| 9 | +const template = `<div id="${containerId}"></div>`; |
| 10 | + |
| 11 | +const gridOptionMock = { |
| 12 | + enableFiltering: true, |
| 13 | + enableFilterTrimWhiteSpace: true, |
| 14 | +} as GridOption; |
| 15 | + |
| 16 | +const gridStub = { |
| 17 | + getOptions: () => gridOptionMock, |
| 18 | + getColumns: jest.fn(), |
| 19 | + getHeaderRowColumn: jest.fn(), |
| 20 | + render: jest.fn(), |
| 21 | +}; |
| 22 | + |
| 23 | +describe('SliderFilter', () => { |
| 24 | + let divContainer: HTMLDivElement; |
| 25 | + let filter: SliderFilter; |
| 26 | + let filterArguments: FilterArguments; |
| 27 | + let spyGetHeaderRow; |
| 28 | + let mockColumn: Column; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + divContainer = document.createElement('div'); |
| 32 | + divContainer.innerHTML = template; |
| 33 | + document.body.appendChild(divContainer); |
| 34 | + spyGetHeaderRow = jest.spyOn(gridStub, 'getHeaderRowColumn').mockReturnValue(divContainer); |
| 35 | + |
| 36 | + mockColumn = { id: 'duration', field: 'duration', filterable: true, filter: { model: Filters.slider } }; |
| 37 | + filterArguments = { |
| 38 | + grid: gridStub, |
| 39 | + columnDef: mockColumn, |
| 40 | + callback: jest.fn() |
| 41 | + }; |
| 42 | + |
| 43 | + filter = new SliderFilter(); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + filter.destroy(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw an error when trying to call init without any arguments', () => { |
| 51 | + expect(() => filter.init(null)).toThrowError('[Angular-SlickGrid] A filter must always have an "init()" with valid arguments.'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should initialize the filter', () => { |
| 55 | + filter.init(filterArguments); |
| 56 | + const filterCount = divContainer.querySelectorAll('.search-filter.slider-container.filter-duration').length; |
| 57 | + |
| 58 | + expect(spyGetHeaderRow).toHaveBeenCalled(); |
| 59 | + expect(filterCount).toBe(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should call "setValues" and expect that value to be in the callback when triggered', () => { |
| 63 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 64 | + |
| 65 | + filter.init(filterArguments); |
| 66 | + filter.setValues(['2']); |
| 67 | + const filterElm = divContainer.querySelector('.search-filter.slider-container.filter-duration'); |
| 68 | + filterElm.dispatchEvent(new CustomEvent('change')); |
| 69 | + |
| 70 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, operator: 'EQ', searchTerms: ['2'], shouldTriggerQuery: true }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should call "setValues" and expect that value, converted as a string, to be in the callback when triggered', () => { |
| 74 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 75 | + |
| 76 | + filter.init(filterArguments); |
| 77 | + filter.setValues(3); |
| 78 | + const filterElm = divContainer.querySelector('.search-filter.slider-container.filter-duration'); |
| 79 | + filterElm.dispatchEvent(new CustomEvent('change')); |
| 80 | + const filterFilledElms = divContainer.querySelectorAll('.search-filter.slider-container.filter-duration.filled'); |
| 81 | + |
| 82 | + expect(filterFilledElms.length).toBe(1); |
| 83 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, operator: 'EQ', searchTerms: ['3'], shouldTriggerQuery: true }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should call "setValues" with an empty string and not expect "filled" css class since it is empty', () => { |
| 87 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 88 | + |
| 89 | + filter.init(filterArguments); |
| 90 | + filter.setValues(''); |
| 91 | + const filterElm = divContainer.querySelector('.search-filter.slider-container.filter-duration'); |
| 92 | + const filterFilledElms = divContainer.querySelectorAll('.search-filter.slider-container.filter-duration.filled'); |
| 93 | + filterElm.dispatchEvent(new CustomEvent('change')); |
| 94 | + |
| 95 | + expect(filterFilledElms.length).toBe(0); |
| 96 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, operator: 'EQ', searchTerms: [''], shouldTriggerQuery: true }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should create the input filter with default search terms range when passed as a filter argument', () => { |
| 100 | + filterArguments.searchTerms = [3]; |
| 101 | + |
| 102 | + filter.init(filterArguments); |
| 103 | + const filterNumberElm = divContainer.querySelector<HTMLInputElement>('.input-group-text'); |
| 104 | + const filterFilledElms = divContainer.querySelectorAll('.search-filter.slider-container.filter-duration.filled'); |
| 105 | + |
| 106 | + expect(filterFilledElms.length).toBe(1); |
| 107 | + expect(filterNumberElm.textContent).toBe('3'); |
| 108 | + expect(filter.getValues()).toEqual(3); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should create the input filter with default search terms and a different step size when "valueStep" is provided', () => { |
| 112 | + filterArguments.searchTerms = [15]; |
| 113 | + mockColumn.filter.valueStep = 5; |
| 114 | + |
| 115 | + filter.init(filterArguments); |
| 116 | + const filterNumberElm = divContainer.querySelector<HTMLInputElement>('.input-group-text'); |
| 117 | + const filterInputElm = divContainer.querySelector<HTMLInputElement>('.search-filter.slider-container.filter-duration input'); |
| 118 | + |
| 119 | + expect(filterInputElm.step).toBe('5'); |
| 120 | + expect(filterNumberElm.textContent).toBe('15'); |
| 121 | + expect(filter.getValues()).toEqual(15); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should create the input filter with min slider values being set by filter "minValue"', () => { |
| 125 | + mockColumn.filter = { |
| 126 | + minValue: 4, |
| 127 | + maxValue: 69, |
| 128 | + }; |
| 129 | + |
| 130 | + filter.init(filterArguments); |
| 131 | + |
| 132 | + const filterNumberElm = divContainer.querySelector<HTMLInputElement>('.input-group-text'); |
| 133 | + |
| 134 | + expect(filterNumberElm.textContent).toBe('4'); |
| 135 | + expect(filter.getValues()).toEqual(4); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should create the input filter with min/max slider values being set by filter "sliderStartValue" and "sliderEndValue" through the filter params', () => { |
| 139 | + mockColumn.filter = { |
| 140 | + params: { |
| 141 | + sliderStartValue: 4, |
| 142 | + sliderEndValue: 69, |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + filter.init(filterArguments); |
| 147 | + |
| 148 | + const filterNumberElm = divContainer.querySelector<HTMLInputElement>('.input-group-text'); |
| 149 | + |
| 150 | + expect(filterNumberElm.textContent).toBe('4'); |
| 151 | + expect(filter.getValues()).toEqual(4); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should create the input filter with default search terms range but without showing side numbers when "hideSliderNumber" is set in params', () => { |
| 155 | + filterArguments.searchTerms = [3]; |
| 156 | + mockColumn.filter.params = { hideSliderNumber: true }; |
| 157 | + |
| 158 | + filter.init(filterArguments); |
| 159 | + |
| 160 | + const filterNumberElms = divContainer.querySelectorAll<HTMLInputElement>('.input-group-text'); |
| 161 | + |
| 162 | + expect(filterNumberElms.length).toBe(0); |
| 163 | + expect(filter.getValues()).toEqual(3); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should trigger a callback with the clear filter set when calling the "clear" method', () => { |
| 167 | + filterArguments.searchTerms = [3]; |
| 168 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 169 | + |
| 170 | + filter.init(filterArguments); |
| 171 | + filter.clear(); |
| 172 | + |
| 173 | + expect(filter.getValues()).toBe(0); |
| 174 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, clearFilterTriggered: true, shouldTriggerQuery: true }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should trigger a callback with the clear filter but without querying when when calling the "clear" method with False as argument', () => { |
| 178 | + filterArguments.searchTerms = [3]; |
| 179 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 180 | + |
| 181 | + filter.init(filterArguments); |
| 182 | + filter.clear(false); |
| 183 | + |
| 184 | + expect(filter.getValues()).toBe(0); |
| 185 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, clearFilterTriggered: true, shouldTriggerQuery: false }); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should trigger a callback with the clear filter set when calling the "clear" method and expect min slider values being with values of "sliderStartValue" when defined through the filter params', () => { |
| 189 | + const spyCallback = jest.spyOn(filterArguments, 'callback'); |
| 190 | + mockColumn.filter = { |
| 191 | + params: { |
| 192 | + sliderStartValue: 4, |
| 193 | + sliderEndValue: 69, |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + filter.init(filterArguments); |
| 198 | + filter.clear(false); |
| 199 | + |
| 200 | + expect(filter.getValues()).toEqual(4); |
| 201 | + expect(spyCallback).toHaveBeenLastCalledWith(expect.anything(), { columnDef: mockColumn, clearFilterTriggered: true, shouldTriggerQuery: false }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments