|
| 1 | +import { |
| 2 | + afterEach, beforeEach, describe, expect, it, |
| 3 | +} from '@jest/globals'; |
| 4 | + |
| 5 | +import { |
| 6 | + afterTest, beforeTest, createDataGrid, flushAsync, |
| 7 | +} from '../../__tests__/__mock__/helpers/utils'; |
| 8 | + |
| 9 | +describe('headerFilter.groupInterval as array - tree popup', () => { |
| 10 | + beforeEach(beforeTest); |
| 11 | + afterEach(afterTest); |
| 12 | + |
| 13 | + describe('number[] - automatic numeric buckets', () => { |
| 14 | + it('builds a tree of nested buckets and filters by the selected one', async () => { |
| 15 | + const { instance, component } = await createDataGrid({ |
| 16 | + dataSource: [ |
| 17 | + { id: 1, price: 5 }, |
| 18 | + { id: 2, price: 15 }, |
| 19 | + { id: 3, price: 115 }, |
| 20 | + ], |
| 21 | + headerFilter: { visible: true }, |
| 22 | + columns: [ |
| 23 | + { |
| 24 | + dataField: 'price', |
| 25 | + dataType: 'number', |
| 26 | + headerFilter: { groupInterval: [100, 10] }, |
| 27 | + }, |
| 28 | + ], |
| 29 | + }); |
| 30 | + |
| 31 | + const headerFilter = component.openHeaderFilter(0); |
| 32 | + |
| 33 | + await flushAsync(); |
| 34 | + await flushAsync(); |
| 35 | + const treeView = headerFilter.getTreeView(); |
| 36 | + treeView.expandAll(); |
| 37 | + |
| 38 | + expect(treeView.getStructure()).toEqual([ |
| 39 | + { |
| 40 | + text: '0 - 100', |
| 41 | + children: [ |
| 42 | + { text: '0 - 10', children: [] }, |
| 43 | + { text: '10 - 20', children: [] }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + text: '100 - 200', |
| 48 | + children: [{ text: '110 - 120', children: [] }], |
| 49 | + }, |
| 50 | + ]); |
| 51 | + |
| 52 | + treeView.selectItemByText('0 - 100'); |
| 53 | + await flushAsync(); |
| 54 | + headerFilter.getOKButton().click(); |
| 55 | + await flushAsync(); |
| 56 | + |
| 57 | + expect([...(instance.getCombinedFilter(true) as unknown[])]).toEqual([ |
| 58 | + ['price', '>=', 0], 'and', ['price', '<', 100], |
| 59 | + ]); |
| 60 | + expect(instance.getVisibleRows().map((row) => row.data.price)).toEqual([5, 15]); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('string[] - hierarchical dataSource', () => { |
| 65 | + const createGrid = (): ReturnType<typeof createDataGrid> => createDataGrid({ |
| 66 | + dataSource: [ |
| 67 | + { |
| 68 | + id: 1, city: 'Munich', state: 'Bavaria', country: 'Germany', |
| 69 | + }, |
| 70 | + { |
| 71 | + id: 2, city: 'Nuremberg', state: 'Bavaria', country: 'Germany', |
| 72 | + }, |
| 73 | + { |
| 74 | + id: 3, city: 'Berlin', state: 'Berlin', country: 'Germany', |
| 75 | + }, |
| 76 | + { |
| 77 | + id: 4, city: 'Lyon', state: 'Rhone', country: 'France', |
| 78 | + }, |
| 79 | + ], |
| 80 | + headerFilter: { visible: true }, |
| 81 | + columns: [ |
| 82 | + { |
| 83 | + dataField: 'city', |
| 84 | + dataType: 'string', |
| 85 | + headerFilter: { |
| 86 | + groupInterval: ['country', 'state', 'city'], |
| 87 | + dataSource: [ |
| 88 | + { |
| 89 | + text: 'Germany', |
| 90 | + value: 'Germany', |
| 91 | + items: [ |
| 92 | + { |
| 93 | + text: 'Bavaria', |
| 94 | + value: 'Bavaria', |
| 95 | + items: [ |
| 96 | + { text: 'Munich', value: 'Munich' }, |
| 97 | + { text: 'Nuremberg', value: 'Nuremberg' }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + { text: 'Berlin', value: 'Berlin', items: [{ text: 'Berlin', value: 'Berlin' }] }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + { |
| 104 | + text: 'France', |
| 105 | + value: 'France', |
| 106 | + items: [{ text: 'Rhone', value: 'Rhone', items: [{ text: 'Lyon', value: 'Lyon' }] }], |
| 107 | + }, |
| 108 | + ], |
| 109 | + }, |
| 110 | + calculateFilterExpression(filterValue, selectedFilterOperation, target) { |
| 111 | + if (target === 'headerFilter' && filterValue) { |
| 112 | + const operation = selectedFilterOperation ?? '='; |
| 113 | + return [ |
| 114 | + [['country', operation, filterValue], 'or', ['state', operation, filterValue]], |
| 115 | + 'or', |
| 116 | + ['city', operation, filterValue], |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + return this.defaultCalculateFilterExpression?.( |
| 121 | + filterValue, |
| 122 | + selectedFilterOperation, |
| 123 | + target, |
| 124 | + ) ?? ''; |
| 125 | + }, |
| 126 | + }, |
| 127 | + { dataField: 'country', dataType: 'string' }, |
| 128 | + ], |
| 129 | + }); |
| 130 | + |
| 131 | + it('builds the hierarchy tree from the dataSource', async () => { |
| 132 | + const { component } = await createGrid(); |
| 133 | + |
| 134 | + const headerFilter = component.openHeaderFilter(0); |
| 135 | + |
| 136 | + await flushAsync(); |
| 137 | + await flushAsync(); |
| 138 | + const treeView = headerFilter.getTreeView(); |
| 139 | + treeView.expandAll(); |
| 140 | + |
| 141 | + expect(treeView.getStructure()).toEqual([ |
| 142 | + { |
| 143 | + text: 'Germany', |
| 144 | + children: [ |
| 145 | + { |
| 146 | + text: 'Bavaria', |
| 147 | + children: [ |
| 148 | + { text: 'Munich', children: [] }, |
| 149 | + { text: 'Nuremberg', children: [] }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + { text: 'Berlin', children: [{ text: 'Berlin', children: [] }] }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + { |
| 156 | + text: 'France', |
| 157 | + children: [{ text: 'Rhone', children: [{ text: 'Lyon', children: [] }] }], |
| 158 | + }, |
| 159 | + ]); |
| 160 | + }); |
| 161 | + |
| 162 | + it.each([ |
| 163 | + { node: 'Germany', cities: ['Munich', 'Nuremberg', 'Berlin'] }, |
| 164 | + { node: 'Bavaria', cities: ['Munich', 'Nuremberg'] }, |
| 165 | + { node: 'Munich', cities: ['Munich'] }, |
| 166 | + ])('filters by the selected "$node" node', async ({ node, cities }) => { |
| 167 | + const { instance, component } = await createGrid(); |
| 168 | + |
| 169 | + const headerFilter = component.openHeaderFilter(0); |
| 170 | + |
| 171 | + await flushAsync(); |
| 172 | + await flushAsync(); |
| 173 | + const treeView = headerFilter.getTreeView(); |
| 174 | + treeView.expandAll(); |
| 175 | + treeView.selectItemByText(node); |
| 176 | + await flushAsync(); |
| 177 | + headerFilter.getOKButton().click(); |
| 178 | + await flushAsync(); |
| 179 | + |
| 180 | + expect([...(instance.getCombinedFilter(true) as unknown[])]).toEqual([ |
| 181 | + [['country', '=', node], 'or', ['state', '=', node]], |
| 182 | + 'or', |
| 183 | + ['city', '=', node], |
| 184 | + ]); |
| 185 | + expect(instance.getVisibleRows().map((row) => row.data.city)).toEqual(cities); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments