Skip to content

Commit

Permalink
[DataGrid] Add test coverage for issues fixed in mui#15184 (mui#15282)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored Nov 18, 2024
1 parent 1434e8c commit d01353f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GridRowSelectionModel,
GridRowsProp,
GridColDef,
GridFilterModel,
} from '@mui/x-data-grid-pro';
import { getBasicGridData } from '@mui/x-data-grid-generator';

Expand Down Expand Up @@ -194,6 +195,69 @@ describe('<DataGridPro /> - Row selection', () => {
expect(apiRef.current.getSelectedRows()).to.have.keys([1]);
});

// Context: https://github.com/mui/mui-x/issues/15045
it('should not throw when using `isRowSelectable` and `keepNonExistentRowsSelected`', () => {
function TestDataGrid() {
const [gridRows, setRows] = React.useState(rows);
const onFilterChange = React.useCallback(
(filterModel: GridFilterModel) => {
if (filterModel.items?.length === 0) {
return;
}

const filteredRows = rows.filter((row) => {
return row.jobTitle.includes(filterModel.items[0].value);
});
setRows(filteredRows);
},
[setRows],
);
return (
<TreeDataGrid
defaultGroupingExpansionDepth={-1}
isRowSelectable={() => true}
rows={gridRows}
onFilterModelChange={onFilterChange}
keepNonExistentRowsSelected
/>
);
}
render(<TestDataGrid />);

// Select `Thomas`
fireEvent.click(
screen.getAllByRole('checkbox', {
name: /select row/i,
})[1],
);

expect(apiRef.current.getSelectedRows()).to.have.length(1);
expect(Array.from(apiRef.current.getSelectedRows())[0][0]).to.equal(1);

act(() => {
apiRef.current.setFilterModel({
items: [{ field: 'jobTitle', value: 'Head of Human Resources', operator: 'contains' }],
});
});

expect(apiRef.current.getSelectedRows()).to.have.length(1);
expect(Array.from(apiRef.current.getSelectedRows())[0][0]).to.equal(1);
});

// Context: https://github.com/mui/mui-x/issues/15068
it('should not call `onRowSelectionModelChange` when adding a new row', () => {
const onRowSelectionModelChange = spy();
const { setProps } = render(
<TreeDataGrid onRowSelectionModelChange={onRowSelectionModelChange} />,
);

act(() => {
setProps({ rows: [...rows, { id: 15, hierarchy: ['New'], jobTitle: 'Test Job' }] });
});

expect(onRowSelectionModelChange.callCount).to.equal(0);
});

it('should put the parent into indeterminate if some but not all the children are selected', () => {
render(<TreeDataGrid defaultGroupingExpansionDepth={-1} density="compact" />);

Expand Down
24 changes: 24 additions & 0 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
useGridApiRef,
GridApi,
GridPreferencePanelsValue,
GridRowSelectionModel,
} from '@mui/x-data-grid';
import {
getCell,
Expand Down Expand Up @@ -65,6 +66,29 @@ describe('<DataGrid /> - Row selection', () => {
);
}

// Context: https://github.com/mui/mui-x/issues/15079
it('should not call `onRowSelectionModelChange` twice when using filterMode="server"', () => {
const onRowSelectionModelChange = spy();
function TestDataGrid() {
const [, setRowSelectionModel] = React.useState<GridRowSelectionModel>([]);
const handleRowSelectionModelChange = React.useCallback((model: GridRowSelectionModel) => {
setRowSelectionModel(model);
onRowSelectionModelChange(model);
}, []);
return (
<TestDataGridSelection
getRowId={(row) => row.id}
checkboxSelection
onRowSelectionModelChange={handleRowSelectionModelChange}
filterMode="server"
/>
);
}
render(<TestDataGrid />);
fireEvent.click(getCell(0, 0).querySelector('input')!);
expect(onRowSelectionModelChange.callCount).to.equal(1);
});

describe('prop: checkboxSelection = false (single selection)', () => {
it('should select one row at a time on click WITHOUT ctrl or meta pressed', () => {
render(<TestDataGridSelection />);
Expand Down

0 comments on commit d01353f

Please sign in to comment.