|
| 1 | +import { |
| 2 | + afterEach, beforeEach, describe, expect, it, jest, |
| 3 | +} from '@jest/globals'; |
| 4 | +import type { dxElementWrapper } from '@js/core/renderer'; |
| 5 | +import $ from '@js/core/renderer'; |
| 6 | +import type { Properties as DataGridProperties } from '@js/ui/data_grid'; |
| 7 | +import DataGrid from '@js/ui/data_grid'; |
| 8 | +import errors from '@js/ui/widget/ui.errors'; |
| 9 | + |
| 10 | +import { DataGridModel } from '../../../data_grid/__tests__/__mock__/model/data_grid'; |
| 11 | + |
| 12 | +const SELECTORS = { |
| 13 | + gridContainer: '#gridContainer', |
| 14 | +}; |
| 15 | + |
| 16 | +const GRID_CONTAINER_ID = 'gridContainer'; |
| 17 | + |
| 18 | +const createDataGrid = async ( |
| 19 | + options: DataGridProperties = {}, |
| 20 | +): Promise<{ |
| 21 | + $container: dxElementWrapper; |
| 22 | + component: DataGridModel; |
| 23 | + instance: DataGrid; |
| 24 | +}> => new Promise((resolve) => { |
| 25 | + const $container = $('<div>') |
| 26 | + .attr('id', GRID_CONTAINER_ID) |
| 27 | + .appendTo(document.body); |
| 28 | + |
| 29 | + const dataGridOptions: DataGridProperties = { |
| 30 | + keyExpr: 'id', |
| 31 | + ...options, |
| 32 | + }; |
| 33 | + |
| 34 | + const instance = new DataGrid($container.get(0) as HTMLDivElement, dataGridOptions); |
| 35 | + const component = new DataGridModel($container.get(0) as HTMLElement); |
| 36 | + |
| 37 | + jest.runAllTimers(); |
| 38 | + |
| 39 | + resolve({ |
| 40 | + $container, |
| 41 | + component, |
| 42 | + instance, |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +const beforeTest = (): void => { |
| 47 | + jest.useFakeTimers(); |
| 48 | + jest.spyOn(errors, 'log').mockImplementation(jest.fn()); |
| 49 | + jest.spyOn(errors, 'Error').mockImplementation(() => ({})); |
| 50 | +}; |
| 51 | + |
| 52 | +const afterTest = (): void => { |
| 53 | + const $container = $(SELECTORS.gridContainer); |
| 54 | + const dataGrid = ($container as any).dxDataGrid('instance') as DataGrid; |
| 55 | + |
| 56 | + dataGrid.dispose(); |
| 57 | + $container.remove(); |
| 58 | + jest.clearAllMocks(); |
| 59 | + jest.useRealTimers(); |
| 60 | +}; |
| 61 | + |
| 62 | +describe('Bugs', () => { |
| 63 | + beforeEach(beforeTest); |
| 64 | + afterEach(afterTest); |
| 65 | + |
| 66 | + describe('T1319739 - DataGrid - Columns are misaligned after adding a column at runtime', () => { |
| 67 | + const data = [ |
| 68 | + { |
| 69 | + id: 1, |
| 70 | + field_1: 'Value 1', |
| 71 | + field_2: 'Value 2', |
| 72 | + }, |
| 73 | + ]; |
| 74 | + |
| 75 | + it('should add column with data cell if repaintChangesOnly=true', async () => { |
| 76 | + const { instance, component } = await createDataGrid({ |
| 77 | + dataSource: data, |
| 78 | + repaintChangesOnly: true, |
| 79 | + columns: [ |
| 80 | + { |
| 81 | + dataField: 'field_1', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }); |
| 85 | + |
| 86 | + let visibleColumns = instance.getVisibleColumns(); |
| 87 | + let headerCellsArray = Array.from(component.getHeaderCells()); |
| 88 | + let dataCellsArray = Array.from(component.getDataCells(0)); |
| 89 | + |
| 90 | + expect(visibleColumns.length).toBe(1); |
| 91 | + expect(headerCellsArray.length).toBe(1); |
| 92 | + expect(dataCellsArray.length).toBe(1); |
| 93 | + |
| 94 | + instance.addColumn({ |
| 95 | + dataField: 'field_2', |
| 96 | + }); |
| 97 | + |
| 98 | + jest.runAllTimers(); |
| 99 | + |
| 100 | + visibleColumns = instance.getVisibleColumns(); |
| 101 | + headerCellsArray = Array.from(component.getHeaderCells()); |
| 102 | + dataCellsArray = Array.from(component.getDataCells(0)); |
| 103 | + |
| 104 | + expect(visibleColumns.length).toBe(2); |
| 105 | + expect(visibleColumns[0].dataField).toBe('field_1'); |
| 106 | + expect(visibleColumns[1].dataField).toBe('field_2'); |
| 107 | + |
| 108 | + expect(headerCellsArray.length).toBe(2); |
| 109 | + expect(dataCellsArray.length).toBe(2); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should remove column with data cell if repaintChangesOnly=true', async () => { |
| 113 | + const { instance, component } = await createDataGrid({ |
| 114 | + dataSource: data, |
| 115 | + repaintChangesOnly: true, |
| 116 | + columns: [ |
| 117 | + { |
| 118 | + dataField: 'field_1', |
| 119 | + }, |
| 120 | + { |
| 121 | + dataField: 'field_2', |
| 122 | + }, |
| 123 | + ], |
| 124 | + }); |
| 125 | + |
| 126 | + let visibleColumns = instance.getVisibleColumns(); |
| 127 | + let headerCellsArray = Array.from(component.getHeaderCells()); |
| 128 | + let dataCellsArray = Array.from(component.getDataCells(0)); |
| 129 | + |
| 130 | + expect(visibleColumns.length).toBe(2); |
| 131 | + expect(headerCellsArray.length).toBe(2); |
| 132 | + expect(dataCellsArray.length).toBe(2); |
| 133 | + |
| 134 | + instance.deleteColumn('field_2'); |
| 135 | + jest.runAllTimers(); |
| 136 | + |
| 137 | + visibleColumns = instance.getVisibleColumns(); |
| 138 | + headerCellsArray = Array.from(component.getHeaderCells()); |
| 139 | + dataCellsArray = Array.from(component.getDataCells(0)); |
| 140 | + |
| 141 | + expect(visibleColumns.length).toBe(1); |
| 142 | + expect(visibleColumns[0].dataField).toBe('field_1'); |
| 143 | + |
| 144 | + expect(headerCellsArray.length).toBe(1); |
| 145 | + expect(dataCellsArray.length).toBe(1); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments