-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[test] Do not require ResizeObserver
mock in JSDOM
#8961
Conversation
Netlify deploy previewNetlify deploy preview: https://deploy-preview-8961--material-ui-x.netlify.app/ Updated pagesNo updates. These are the results for the performance tests:
|
React.useEffect(() => { | ||
// For JSDOM test environment that doesn't support ResizeObserver | ||
if (typeof ResizeObserver === 'undefined' && process.env.NODE_ENV === 'test') { | ||
apiRef.current.computeSizeAndPublishResizeEvent(); | ||
} | ||
}, [apiRef]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have this code in
mui-x/packages/grid/x-data-grid/src/components/base/GridBody.tsx
Lines 83 to 84 in 032349c
useEnhancedEffect(() => { | |
apiRef.current.computeSizeAndPublishResizeEvent(); |
Investigating more the bug I found the root problem. First, computeSizeAndPublishResizeEvent
depends on the value of rootElementRef
. By the time the effect above runs, in a test environment, this ref has not been set yet so it cannot store the dimensions. Consequentially, nothing is rendered and the test fails.
rootElementRef
is set in
apiRef.current.register('public', { rootElementRef: rootContainerRef }); |
computeSizeAndPublishResizeEvent
runs in between the 1st and 2nd render. Mocking ResizeObserver
is a hacky way to call computeSizeAndPublishResizeEvent
again, and hopefully have the ref available this time. I suspect we didn't face this problem in our tests because of StrictMode.
I have an alternative solution in #8963. My solution bases on using inside the effect only the refs created in GridBody
. When the effect runs we know for sure that all refs are available, so I pass the main element as an argument of computeSizeAndPublishResizeEvent
, nothing of registering the ref in the API, which requires a 2nd render to work correctly. I tested with your test repo and it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect we didn't face this problem in our tests because of StrictMode.
Yes, I can confirm this.
We don't have a global ResizeObserver mock and strict mode is the only reason the tests pass without it since useLayoutEffect runs twice
TODO: