|
| 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 { DataGridModel } from '@ts/grids/data_grid/__tests__/__mock__/model/data_grid'; |
| 9 | + |
| 10 | +const GRID_CONTAINER_ID = 'gridContainer'; |
| 11 | + |
| 12 | +const SELECTORS = { |
| 13 | + gridContainer: `#${GRID_CONTAINER_ID}`, |
| 14 | +}; |
| 15 | + |
| 16 | +const dataSource = [{ |
| 17 | + ID: 1, |
| 18 | + FirstName: 'John', |
| 19 | + LastName: 'Heart', |
| 20 | + Prefix: 'Mr.', |
| 21 | + Position: 'CEO', |
| 22 | + BirthDate: '1964/03/16', |
| 23 | + HireDate: '1995/01/15', |
| 24 | + Notes: 'John has been in the Audio/Video industry since 1990. He has led DevAv as its CEO since 2003.\r\n\r\nWhen not working hard as the CEO, John loves to golf and bowl. He once bowled a perfect game of 300.', |
| 25 | + Address: '351 S Hill St.', |
| 26 | +}, { |
| 27 | + ID: 2, |
| 28 | + FirstName: 'Olivia', |
| 29 | + LastName: 'Peyton', |
| 30 | + Prefix: 'Mrs.', |
| 31 | + Position: 'Sales Assistant', |
| 32 | + BirthDate: '1981/06/03', |
| 33 | + HireDate: '2012/05/14', |
| 34 | + Notes: 'Olivia loves to sell. She has been selling DevAV products since 2012. \r\n\r\nOlivia was homecoming queen in high school. She is expecting her first child in 6 months. Good Luck Olivia.', |
| 35 | + Address: '807 W Paseo Del Mar', |
| 36 | +}, { |
| 37 | + ID: 3, |
| 38 | + FirstName: 'Robert', |
| 39 | + LastName: 'Reagan', |
| 40 | + Prefix: 'Mr.', |
| 41 | + Position: 'CMO', |
| 42 | + BirthDate: '1974/09/07', |
| 43 | + HireDate: '2002/11/08', |
| 44 | + Notes: 'Robert was recently voted the CMO of the year by CMO Magazine. He is a proud member of the DevAV Management Team.\r\n\r\nRobert is a championship BBQ chef, so when you get the chance ask him for his secret recipe.', |
| 45 | + Address: '4 Westmoreland Pl.', |
| 46 | +}]; |
| 47 | + |
| 48 | +const flushAsync = async (): Promise<void> => { |
| 49 | + jest.runOnlyPendingTimers(); |
| 50 | + await Promise.resolve(); |
| 51 | +}; |
| 52 | + |
| 53 | +const createDataGrid = async ( |
| 54 | + options: DataGridProperties = {}, |
| 55 | +): Promise<{ |
| 56 | + $container: dxElementWrapper; |
| 57 | + component: DataGridModel; |
| 58 | + instance: DataGrid; |
| 59 | +}> => new Promise((resolve) => { |
| 60 | + const $container = $('<div>') |
| 61 | + .attr('id', GRID_CONTAINER_ID) |
| 62 | + .appendTo(document.body); |
| 63 | + |
| 64 | + const instance = new DataGrid($container.get(0) as HTMLDivElement, options); |
| 65 | + const component = new DataGridModel($container.get(0) as HTMLElement); |
| 66 | + |
| 67 | + jest.runAllTimers(); |
| 68 | + resolve({ |
| 69 | + $container, |
| 70 | + component, |
| 71 | + instance, |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +const beforeTest = (): void => { |
| 76 | + jest.useFakeTimers(); |
| 77 | +}; |
| 78 | + |
| 79 | +const afterTest = (): void => { |
| 80 | + const $container = $(SELECTORS.gridContainer); |
| 81 | + const dataGrid = ( |
| 82 | + $container as dxElementWrapper & { dxDataGrid: (command: string) => DataGrid } |
| 83 | + ).dxDataGrid('instance'); |
| 84 | + |
| 85 | + dataGrid.dispose(); |
| 86 | + $container.remove(); |
| 87 | + jest.clearAllMocks(); |
| 88 | + jest.useRealTimers(); |
| 89 | +}; |
| 90 | + |
| 91 | +describe('DataGrid editing', () => { |
| 92 | + beforeEach(beforeTest); |
| 93 | + afterEach(afterTest); |
| 94 | + |
| 95 | + // T1293181 |
| 96 | + describe('Recovered (undeleted) row', () => { |
| 97 | + it('should have correct data when placed before inserted row in batch editing', async () => { |
| 98 | + const recoveringRowIndex = dataSource.length - 1; |
| 99 | + const { component, instance } = await createDataGrid({ |
| 100 | + keyExpr: 'ID', |
| 101 | + dataSource, |
| 102 | + columns: [ |
| 103 | + { |
| 104 | + dataField: 'Prefix', |
| 105 | + caption: 'Title', |
| 106 | + width: 70, |
| 107 | + }, |
| 108 | + 'FirstName', |
| 109 | + 'LastName', { |
| 110 | + dataField: 'Position', |
| 111 | + width: 170, |
| 112 | + }, { |
| 113 | + dataField: 'BirthDate', |
| 114 | + dataType: 'date' as const, |
| 115 | + }, |
| 116 | + ], |
| 117 | + editing: { |
| 118 | + mode: 'batch', |
| 119 | + allowDeleting: true, |
| 120 | + allowAdding: true, |
| 121 | + newRowPosition: 'pageBottom', |
| 122 | + texts: { |
| 123 | + deleteRow: 'Delete', |
| 124 | + undeleteRow: 'Undelete', |
| 125 | + }, |
| 126 | + }, |
| 127 | + }); |
| 128 | + |
| 129 | + await flushAsync(); |
| 130 | + |
| 131 | + await instance.addRow(); |
| 132 | + await flushAsync(); |
| 133 | + |
| 134 | + const rowDeleteButton = component.getDataRow(recoveringRowIndex).getDeleteButton(); |
| 135 | + rowDeleteButton.click(); |
| 136 | + await flushAsync(); |
| 137 | + |
| 138 | + const rowRecoverButton = component.getDataRow(recoveringRowIndex).getRecoverButton(); |
| 139 | + rowRecoverButton.click(); |
| 140 | + await flushAsync(); |
| 141 | + |
| 142 | + const rows = instance.getVisibleRows(); |
| 143 | + expect(rows).toHaveLength(dataSource.length + 1); |
| 144 | + expect(rows[recoveringRowIndex].data).toEqual(dataSource[recoveringRowIndex]); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments