Skip to content

Commit

Permalink
avoid using fake timers + check selectedRowsCount element
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii committed Mar 18, 2024
1 parent 9578245 commit 72d6e88
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ export const useGridRowSelection = (
: gridExpandedSortedRowIdsSelector(apiRef);

const filterModel = gridFilterModelSelector(apiRef);

apiRef.current.selectRows(rowsToBeSelected, params.value, filterModel?.items.length > 0);
},
[apiRef, props.checkboxSelectionVisibleOnly, props.pagination],
Expand Down
42 changes: 30 additions & 12 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, fireEvent, screen, act, userEvent } from '@mui-internal/test-utils';
import {
createRenderer,
fireEvent,
screen,
act,
userEvent,
waitFor,
} from '@mui-internal/test-utils';
import {
DataGrid,
DataGridProps,
Expand All @@ -19,6 +26,7 @@ import {
getColumnHeaderCell,
getColumnHeadersTextContent,
getActiveCell,
grid,
} from 'test/utils/helperFn';
import { getBasicGridData } from '@mui/x-data-grid-generator';

Expand Down Expand Up @@ -182,7 +190,6 @@ describe('<DataGrid /> - Row selection', () => {
});

describe('prop: checkboxSelection = true (multi selection)', () => {
clock.withFakeTimers();
it('should allow to toggle prop.checkboxSelection', () => {
const { setProps } = render(<TestDataGridSelection />);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'Currency Pair']);
Expand Down Expand Up @@ -357,7 +364,7 @@ describe('<DataGrid /> - Row selection', () => {
expect(input2.checked).to.equal(true);
});

it('should only select filtered items when "select all" is toggled after applying a filter', () => {
it('should only select filtered items when "select all" is toggled after applying a filter', async () => {
render(
<TestDataGridSelection
checkboxSelection
Expand All @@ -366,25 +373,36 @@ describe('<DataGrid /> - Row selection', () => {
open: true,
openedPanelValue: GridPreferencePanelsValue.filters,
},
filter: {
filterModel: {
items: [],
},
},
}}
/>,
);
const selectAllCheckbox = screen.getByRole('checkbox', { name: 'Select all rows' });
fireEvent.click(selectAllCheckbox);
expect(getSelectedRowIds()).to.deep.equal([0, 1, 2, 3]);
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([0, 1, 2, 3]);
expect(grid('selectedRowCount')?.textContent).to.equal('4 rows selected');
});

fireEvent.change(screen.getByRole('spinbutton', { name: 'Value' }), {
target: { value: 1 },
});
clock.tick(500);
await waitFor(() => {
// Previous selection remains, but only one row is visible
expect(getSelectedRowIds()).to.deep.equal([1]);
expect(grid('selectedRowCount')?.textContent).to.equal('4 rows selected');
});

fireEvent.click(selectAllCheckbox); // Unselect all
expect(getSelectedRowIds()).to.deep.equal([]);
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([]);
expect(grid('selectedRowCount')).to.equal(null);
});

fireEvent.click(selectAllCheckbox); // Select all filtered rows
expect(getSelectedRowIds()).to.deep.equal([1]);
await waitFor(() => {
expect(getSelectedRowIds()).to.deep.equal([1]);
expect(grid('selectedRowCount')?.textContent).to.equal('1 row selected');
});
});
});

Expand Down

0 comments on commit 72d6e88

Please sign in to comment.