Skip to content

Commit d7ac79c

Browse files
[core] Add assertion about checkbox rerenders (#8974)
1 parent b83080d commit d7ac79c

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

packages/grid/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GridEditModes,
1111
useGridApiRef,
1212
GridApi,
13+
GridRow,
1314
} from '@mui/x-data-grid';
1415
import {
1516
getCell,
@@ -190,7 +191,7 @@ describe('<DataGrid /> - Row Selection', () => {
190191
expect(getColumnHeaderCell(0).querySelectorAll('input')).to.have.length(1);
191192
});
192193

193-
it('should check and uncheck when double clicking the row', () => {
194+
it('should check then uncheck when clicking twice the row', () => {
194195
render(<TestDataGridSelection checkboxSelection />);
195196
expect(getSelectedRowIds()).to.deep.equal([]);
196197
expect(getRow(0).querySelector('input')).to.have.property('checked', false);
@@ -774,4 +775,54 @@ describe('<DataGrid /> - Row Selection', () => {
774775
expect(getSelectedRowIds()).to.deep.equal([]);
775776
});
776777
});
778+
779+
describe('performance', () => {
780+
it('should not rerender unrelated rows', () => {
781+
// TODO: remove this requirement, find ways to scope down react tree rerenders.
782+
const MemoizedRow = React.memo(GridRow);
783+
784+
// Couldn't use <RenderCounter> because we need to track multiple components
785+
let commits: any[] = [];
786+
function CustomCell(props: any) {
787+
React.useEffect(() => {
788+
commits.push({
789+
rowId: props.id,
790+
});
791+
});
792+
return <div>Hello</div>;
793+
}
794+
795+
render(
796+
<div style={{ width: 300, height: 300 }}>
797+
<DataGrid
798+
columns={[
799+
{ field: 'id', headerName: 'id', type: 'number' },
800+
{
801+
field: 'currencyPair',
802+
headerName: 'Currency Pair',
803+
renderCell: (params) => <CustomCell {...params} />,
804+
},
805+
]}
806+
slots={{
807+
row: MemoizedRow,
808+
}}
809+
rows={[
810+
{ id: 0, currencyPair: 'USDGBP' },
811+
{ id: 1, currencyPair: 'USDEUR' },
812+
]}
813+
autoHeight={isJSDOM}
814+
checkboxSelection
815+
/>
816+
</div>,
817+
);
818+
expect(getSelectedRowIds()).to.deep.equal([]);
819+
expect(getRow(0).querySelector('input')).to.have.property('checked', false);
820+
commits = [];
821+
fireEvent.click(getCell(0, 1));
822+
expect(getSelectedRowIds()).to.deep.equal([0]);
823+
expect(getRow(0).querySelector('input')).to.have.property('checked', true);
824+
// It shouldn't rerender rowId 1
825+
expect(commits).to.deep.equal([{ rowId: 0 }]);
826+
});
827+
});
777828
});

0 commit comments

Comments
 (0)