Skip to content

Commit

Permalink
[TextareaAutosize] Test for error when used with React 18 & Suspense (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
howlettt authored and Daniel Rabe committed Nov 29, 2022
1 parent d1fc97c commit 9a4d73b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 0 additions & 2 deletions packages/mui-base/src/TextareaAutosize/TextareaAutosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(props, ref)
// ResizeObserver's handler that runs because of the change in the layout is trying to
// access a dom node that is no longer there (as the fallback component is being shown instead).
// See https://github.com/mui/material-ui/issues/32640
// TODO: Add tests that will ensure the component is not failing when
// replaced by Suspense with a fallback, once React is updated to version 18
if (inputRef.current) {
syncHeightWithFlushSycn();
}
Expand Down
30 changes: 30 additions & 0 deletions test/e2e/fixtures/TextareaAutosize/TextareaAutosizeSuspense.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import TextareaAutosize from '@mui/base/TextareaAutosize';
import Button from '@mui/material/Button';
import * as React from 'react';

function LazyRoute() {
const [isDone, setIsDone] = React.useState(false);

if (!isDone) {
// Force React to show fallback suspense
throw new Promise((resolve) => {
window.setTimeout(resolve, 1);
setIsDone(true);
});
}

return <div />;
}

export default function TextareaAutosizeSuspense() {
const [showRoute, setShowRoute] = React.useState(false);

return (
<React.Fragment>
<Button onClick={() => setShowRoute((r) => !r)}>Toggle view</Button>
<React.Suspense fallback={null}>
{showRoute ? <LazyRoute /> : <TextareaAutosize />}
</React.Suspense>
</React.Fragment>
);
}
4 changes: 3 additions & 1 deletion test/e2e/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import ReactDOM from 'react-dom';
import { createRoot } from 'react-dom/client';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
import * as DomTestingLibrary from '@testing-library/dom';
import TestViewer from './TestViewer';
Expand Down Expand Up @@ -107,7 +108,8 @@ if (typeof ReactDOM.unstable_createRoot === 'function') {
const root = ReactDOM.unstable_createRoot(container);
root.render(children);
} else {
ReactDOM.render(children, container);
const root = createRoot(container);
root.render(children);
}

window.DomTestingLibrary = DomTestingLibrary;
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,20 @@ describe('e2e', () => {
);
});
});

describe('<TextareaAutosize />', () => {
// https://github.com/mui/material-ui/issues/32640
it('should handle suspense without error', async () => {
const pageErrors: string[] = [];
page.on('pageerror', (err) => pageErrors.push(err.name));

await renderFixture('TextareaAutosize/TextareaAutosizeSuspense');
expect(await page.isVisible('textarea')).to.equal(true);
await page.click('button');
expect(await page.isVisible('textarea')).to.equal(false);
await page.waitForTimeout(200); // Wait for debounce to fire (166)

expect(pageErrors.length).to.equal(0);
});
});
});

0 comments on commit 9a4d73b

Please sign in to comment.