From 16fe8135b8053cdfb17c75281de41b1845b27d64 Mon Sep 17 00:00:00 2001 From: tongsonbarbs Date: Thu, 18 Jul 2024 15:44:33 +0800 Subject: [PATCH 1/2] T1233556_24_2_3 new PR --- .../dataGrid/stateStoring/stateStoring.ts | 34 +++++++++++++++++++ .../state_storing/m_state_storing.ts | 11 +++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts b/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts index 4fef2309d598..d57348d4770a 100644 --- a/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts +++ b/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts @@ -16,6 +16,20 @@ const makeLocalStorageJsonInvalid = ClientFunction(() => { window.localStorage.testStorageKey = '{]'; }); +const localConfig = (rows): any => ({ + dataSource: Array.from(new Array(rows).keys()).map((i) => ({ id: i })), + width: '100%', + keyExpr: 'id', + showBorders: true, + selectedRowKeys: [0], + selection: {mode: 'multiple'}, + stateStoring: { + enabled: true, + storageKey: 'storage_custom', + }, + columns: [{dataField: 'id'}, 'column2', 'column3', 'column4', 'column5'], +}) + test('The Grid should load if JSON in localStorage is invalid and stateStoring enabled', async (t) => { const dataGrid = new DataGrid(GRID_CONTAINER); const secondCell = dataGrid.getDataCell(1, 1); @@ -82,3 +96,23 @@ test('The rows should render correctly when cellTemplates are used and the selec // simulating async rendering in React await makeRowsViewTemplatesAsync(GRID_CONTAINER); }); + +// T1233556 +test('Should select selectedRowKeys value if state.selectedRowKeys is null, undefined, or empty array', async (t) => { + const dataGrid = new DataGrid(GRID_CONTAINER); + + await t + .click(dataGrid.getDataCell(0, 0).element()) + + await t + .eval(() => location.reload()) + + await createWidget('dxDataGrid', localConfig); + + await t + .expect(dataGrid.getDataRow(0).isSelected) + .ok(); + +}).before(async () => { + await createWidget('dxDataGrid', localConfig(10)); +}) diff --git a/packages/devextreme/js/__internal/grids/grid_core/state_storing/m_state_storing.ts b/packages/devextreme/js/__internal/grids/grid_core/state_storing/m_state_storing.ts index d2c1234c692d..926ee563a6e1 100644 --- a/packages/devextreme/js/__internal/grids/grid_core/state_storing/m_state_storing.ts +++ b/packages/devextreme/js/__internal/grids/grid_core/state_storing/m_state_storing.ts @@ -157,10 +157,9 @@ const stateStoring = (Base: ModuleType) => class StateSt * @extended: TreeList's state_storing */ protected applyState(state) { - const { allowedPageSizes } = state; - const { searchText } = state; - const { selectedRowKeys } = state; - const { selectionFilter } = state; + const { + allowedPageSizes, searchText, selectedRowKeys, selectionFilter, + } = state; const scrollingMode = this.option('scrolling.mode'); const isVirtualScrollingMode = scrollingMode === 'virtual' || scrollingMode === 'infinite'; const showPageSizeSelector = this.option('pager.visible') === true && this.option('pager.showPageSizeSelector'); @@ -178,7 +177,9 @@ const stateStoring = (Base: ModuleType) => class StateSt } if (!this.option('selection.deferred')) { - this.option('selectedRowKeys', selectedRowKeys || []); + if (selectedRowKeys && selectedRowKeys.length !== 0) { + this.option('selectedRowKeys', selectedRowKeys || []); + } } // @ts-expect-error From 60478d7abb73c856798770bdb9e9191942ddff80 Mon Sep 17 00:00:00 2001 From: tongsonbarbs Date: Thu, 18 Jul 2024 16:06:05 +0800 Subject: [PATCH 2/2] Fix lint issues --- .../dataGrid/stateStoring/stateStoring.ts | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts b/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts index d57348d4770a..a1fd3fbf8dd1 100644 --- a/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts +++ b/e2e/testcafe-devextreme/tests/dataGrid/stateStoring/stateStoring.ts @@ -17,18 +17,18 @@ const makeLocalStorageJsonInvalid = ClientFunction(() => { }); const localConfig = (rows): any => ({ - dataSource: Array.from(new Array(rows).keys()).map((i) => ({ id: i })), - width: '100%', - keyExpr: 'id', - showBorders: true, - selectedRowKeys: [0], - selection: {mode: 'multiple'}, - stateStoring: { - enabled: true, - storageKey: 'storage_custom', - }, - columns: [{dataField: 'id'}, 'column2', 'column3', 'column4', 'column5'], -}) + dataSource: Array.from(new Array(rows).keys()).map((i) => ({ id: i })), + width: '100%', + keyExpr: 'id', + showBorders: true, + selectedRowKeys: [0], + selection: { mode: 'multiple' }, + stateStoring: { + enabled: true, + storageKey: 'storage_custom', + }, + columns: [{ dataField: 'id' }, 'column2', 'column3', 'column4', 'column5'], +}); test('The Grid should load if JSON in localStorage is invalid and stateStoring enabled', async (t) => { const dataGrid = new DataGrid(GRID_CONTAINER); @@ -102,17 +102,17 @@ test('Should select selectedRowKeys value if state.selectedRowKeys is null, unde const dataGrid = new DataGrid(GRID_CONTAINER); await t - .click(dataGrid.getDataCell(0, 0).element()) + .click(dataGrid.getDataCell(0, 0).element()); await t - .eval(() => location.reload()) + .eval(() => location.reload()); - await createWidget('dxDataGrid', localConfig); + await createWidget('dxDataGrid', localConfig(10)); await t .expect(dataGrid.getDataRow(0).isSelected) .ok(); - + }).before(async () => { await createWidget('dxDataGrid', localConfig(10)); -}) +});