|
| 1 | +import { |
| 2 | + afterEach, describe, expect, it, |
| 3 | +} from '@jest/globals'; |
| 4 | +import { CustomStore } from '@js/common/data'; |
| 5 | +import type { dxElementWrapper } from '@js/core/renderer'; |
| 6 | +import $ from '@js/core/renderer'; |
| 7 | + |
| 8 | +import type { GridsEditRefreshMode, Properties as DataGridProperties } from '../../../../ui/data_grid'; |
| 9 | +import DataGrid from '../../../../ui/data_grid'; |
| 10 | + |
| 11 | +const SELECTORS = { |
| 12 | + gridContainer: '#gridContainer', |
| 13 | + detailCell: 'dx-master-detail-cell', |
| 14 | + detailContainer: 'dx-datagrid-master-detail-container', |
| 15 | +}; |
| 16 | + |
| 17 | +const GRID_CONTAINER_ID = 'gridContainer'; |
| 18 | + |
| 19 | +const createDataGrid = async ( |
| 20 | + options: DataGridProperties = {}, |
| 21 | +): Promise<{ $container: dxElementWrapper; instance: DataGrid }> => new Promise((resolve) => { |
| 22 | + const $container = $('<div>') |
| 23 | + .attr('id', GRID_CONTAINER_ID) |
| 24 | + .appendTo(document.body); |
| 25 | + |
| 26 | + const instance = new DataGrid($container.get(0) as HTMLDivElement, options); |
| 27 | + |
| 28 | + const contentReadyHandler = (): void => { |
| 29 | + resolve({ $container, instance }); |
| 30 | + instance.off('contentReady', contentReadyHandler); |
| 31 | + }; |
| 32 | + |
| 33 | + instance.on('contentReady', contentReadyHandler); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('GridCore selection', () => { |
| 37 | + afterEach(() => { |
| 38 | + const $container = $(SELECTORS.gridContainer); |
| 39 | + |
| 40 | + const dataGrid = ($container as any).dxDataGrid('instance') as DataGrid; |
| 41 | + |
| 42 | + dataGrid.dispose(); |
| 43 | + $container.remove(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('selectionChanged handler', () => { |
| 47 | + [true, false].forEach((repaintChangesOnly) => { |
| 48 | + it(`selectRowKeys are updated after refresh if selectedItem is not in dataSource anymore with repaintChangesOnly=${repaintChangesOnly}`, async () => { |
| 49 | + const dataSource = [ |
| 50 | + { id: 1, name: 'Item 1' }, |
| 51 | + { id: 2, name: 'Item 2' }, |
| 52 | + ]; |
| 53 | + |
| 54 | + const { instance } = await createDataGrid({ |
| 55 | + dataSource, |
| 56 | + columns: ['id', 'name'], |
| 57 | + keyExpr: 'id', |
| 58 | + selection: { |
| 59 | + mode: 'single', |
| 60 | + }, |
| 61 | + repaintChangesOnly, |
| 62 | + }); |
| 63 | + |
| 64 | + await instance.selectRows([2], false); |
| 65 | + expect(instance.getSelectedRowKeys()).toEqual([2]); |
| 66 | + |
| 67 | + dataSource.splice(1, 1); // Remove the item with id 2 |
| 68 | + |
| 69 | + await instance.refresh(repaintChangesOnly); |
| 70 | + |
| 71 | + expect(instance.getSelectedRowKeys()).toEqual([]); |
| 72 | + }); |
| 73 | + |
| 74 | + it(`selectionChanged handler is not called after refresh if selectedItem still present in dataSource with repaintChangesOnly=${repaintChangesOnly}`, async () => { |
| 75 | + const dataSource = [ |
| 76 | + { id: 1, name: 'Item 1' }, |
| 77 | + { id: 2, name: 'Item 2' }, |
| 78 | + ]; |
| 79 | + |
| 80 | + let selectionChangedCount = 0; |
| 81 | + |
| 82 | + const { instance } = await createDataGrid({ |
| 83 | + dataSource, |
| 84 | + columns: ['id', 'name'], |
| 85 | + keyExpr: 'id', |
| 86 | + selection: { |
| 87 | + mode: 'single', |
| 88 | + }, |
| 89 | + repaintChangesOnly, |
| 90 | + onSelectionChanged: () => { |
| 91 | + selectionChangedCount += 1; |
| 92 | + }, |
| 93 | + }); |
| 94 | + |
| 95 | + await instance.selectRows([1], false); |
| 96 | + expect(instance.getSelectedRowKeys()).toEqual([1]); |
| 97 | + expect(selectionChangedCount).toBe(1); |
| 98 | + |
| 99 | + dataSource.splice(1, 1); // Remove the item with id 2 |
| 100 | + await instance.refresh(repaintChangesOnly); |
| 101 | + |
| 102 | + expect(instance.getSelectedRowKeys()).toEqual([1]); |
| 103 | + expect(selectionChangedCount).toBe(1); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('remote dataSource', () => { |
| 109 | + ([ |
| 110 | + { refreshMode: 'full', expectedCallCount: 2 }, |
| 111 | + { refreshMode: 'reshape', expectedCallCount: 1 }, |
| 112 | + { refreshMode: 'repaint', expectedCallCount: 0 }, |
| 113 | + ] as { refreshMode: GridsEditRefreshMode; expectedCallCount: number }[]) |
| 114 | + .forEach(({ refreshMode, expectedCallCount }) => { |
| 115 | + it(`dataSource.load is not called to load selectedRow after data save with editing.refreshMode=${refreshMode}`, async () => { |
| 116 | + let data = [ |
| 117 | + { id: 1, name: 'Item 1' }, |
| 118 | + { id: 2, name: 'Item 2' }, |
| 119 | + { id: 3, name: 'Item 3' }, |
| 120 | + { id: 4, name: 'Item 4' }, |
| 121 | + ]; |
| 122 | + |
| 123 | + const store = new CustomStore({ |
| 124 | + key: 'id', |
| 125 | + load: (e) => { |
| 126 | + const skip = e.skip ?? 0; |
| 127 | + const take = e.take ?? data.length; |
| 128 | + const pageData = data.slice(skip, skip + take); |
| 129 | + return Promise.resolve({ |
| 130 | + data: pageData, |
| 131 | + totalCount: data.length, |
| 132 | + }); |
| 133 | + }, |
| 134 | + remove(key) { |
| 135 | + data = data.filter((item) => item.id !== key); |
| 136 | + return Promise.resolve(); |
| 137 | + }, |
| 138 | + }); |
| 139 | + |
| 140 | + const { instance } = await createDataGrid({ |
| 141 | + dataSource: store, |
| 142 | + editing: { |
| 143 | + mode: 'batch', |
| 144 | + refreshMode, |
| 145 | + allowDeleting: true, |
| 146 | + }, |
| 147 | + remoteOperations: true, |
| 148 | + paging: { |
| 149 | + pageSize: 2, |
| 150 | + }, |
| 151 | + columns: ['id', 'name'], |
| 152 | + keyExpr: 'id', |
| 153 | + selection: { |
| 154 | + mode: 'multiple', |
| 155 | + showCheckBoxesMode: 'always', |
| 156 | + }, |
| 157 | + }); |
| 158 | + |
| 159 | + await instance.selectRows([4], false); |
| 160 | + |
| 161 | + let callCount = 0; |
| 162 | + store.on('loading', () => { |
| 163 | + callCount += 1; |
| 164 | + }); |
| 165 | + |
| 166 | + instance.option('editing.changes', [{ |
| 167 | + type: 'remove', |
| 168 | + key: 1, |
| 169 | + }]); |
| 170 | + await instance.saveEditData(); |
| 171 | + |
| 172 | + expect(callCount).toBe(expectedCallCount); |
| 173 | + }); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments