Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix container width change on React 18 #5566

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ export const useGridVirtualScroller = (props: UseGridVirtualScrollerProps) => {
setContainerWidth(rootRef.current!.clientWidth);
}, [rowsMeta.currentPageTotalHeight]);

const handleResize = React.useCallback<GridEventListener<'resize'>>(() => {
if (rootRef.current) {
setContainerWidth(rootRef.current.clientWidth);
}
const handleResize = React.useCallback<GridEventListener<'resize'>>((params) => {
setContainerWidth(params.width);
}, []);

useGridApiEventHandler(apiRef, 'resize', handleResize);
Expand Down
17 changes: 7 additions & 10 deletions packages/grid/x-data-grid/src/tests/layout.DataGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
// @ts-ignore Remove once the test utils are typed
import { createRenderer, screen, ErrorBoundary, waitFor } from '@mui/monorepo/test/utils';
import { SinonStub, stub, spy } from 'sinon';
import { stub, spy } from 'sinon';
import { expect } from 'chai';
import {
DataGrid,
Expand All @@ -17,7 +17,7 @@ import { createTheme, ThemeProvider } from '@mui/material/styles';
import { getColumnHeaderCell, getColumnValues, getCell, getRow } from 'test/utils/helperFn';

describe('<DataGrid /> - Layout & Warnings', () => {
const { clock, render } = createRenderer({ clock: 'fake' });
const { clock, render } = createRenderer({ clock: 'real' });

const baselineProps = {
rows: [
Expand Down Expand Up @@ -59,14 +59,10 @@ describe('<DataGrid /> - Layout & Warnings', () => {
});

it('should resize the width of the columns', async () => {
// Using a fake clock also affects `requestAnimationFrame`.
// Calling clock.tick() should call the callback passed, but it doesn't work.
stub(window, 'requestAnimationFrame').callsFake((fn: any) => fn());
stub(window, 'cancelAnimationFrame');
Comment on lines -64 to -65
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mocking requestAnimationFrame doesn't feel right so I inverted the logic: use real clock by default and fake clock on demand.


interface TestCaseProps {
width?: number;
}

const TestCase = (props: TestCaseProps) => {
const { width = 300 } = props;
return (
Expand All @@ -87,9 +83,6 @@ describe('<DataGrid /> - Layout & Warnings', () => {
rect = container.querySelector('[role="row"][data-rowindex="0"]').getBoundingClientRect();
expect(rect.width).to.equal(400 - 2);
});

(window.requestAnimationFrame as SinonStub).restore();
(window.cancelAnimationFrame as SinonStub).restore();
});

// Adaptation of describeConformance()
Expand Down Expand Up @@ -203,6 +196,8 @@ describe('<DataGrid /> - Layout & Warnings', () => {
});

describe('warnings', () => {
clock.withFakeTimers();

it('should error if the container has no intrinsic height', () => {
expect(() => {
render(
Expand Down Expand Up @@ -257,6 +252,8 @@ describe('<DataGrid /> - Layout & Warnings', () => {
});

describe('swallow warnings', () => {
clock.withFakeTimers();

beforeEach(() => {
stub(console, 'error');
});
Expand Down