-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Usage with react-test-renderer #493
Comments
Some components (eg Other components (eg Most react-virtualized components don't need a real DOM and so can be tested with Jest. However |
A very awkward way to mock AutoSizer: 🤣 // MyComp.js
import React from 'react';
import { Grid } from 'react-virtualized';
let { AutoSizer } = require('react-virtualized');
if (process.env.NODE_ENV === "test") {
AutoSizer = require('react-virtualized/dist/commonjs/AutoSizer');
}
class MyComp extends React.Component {
render() {
return (
<AutoSizer>
{({ width, height }) => (
<Grid
width={width}
height={height}
{/* ... */}
/>
)}
</AutoSizer>
);
}
} // MyComp.test.js
import React from 'react';
import MyComp from './MyComp';
jest.mock('react-virtualized/dist/commonjs/AutoSizer', () => {
const width = 1024;
const height = 768;
return ({ children }) =>
<div>{children({ width, height })}</div>;
});
// ... I couldn't find a way to mock only AutoSizer without polluting my application code. |
Actually, that's completely unnecessary, you can do this instead in your application code: <AutoSizer>
{({ width, height }) => {
let actualWidth = width;
let actualHeight = height;
if (process.env.NODE_ENV === 'test') {
actualWidth = window.innerWidth;
actualHeight = window.innerHeight;
}
// do something with these dimensions
}}
</AutoSizer> This will prevent width and height from being The contents of AutoSizer will not re-render on resize, but that's E2E territory anyway. |
Even easier solution: <AutoSizer>
{({ width, height }) => (
<List
height={height || 100}
width={width || 100}
/>
)}
</AutoSizer> |
@izakfilmalter nice solution, thanks for sharing! |
<AutoSizer defaultWidth={100} defaultHeight={100}></AutoSizer> |
Noting the above caveats about mocking the DOM, I adapted the AutoSizer's mock, though it's a little white-boxy. describe("My Test", () => {
const originalOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetHeight');
const originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');
beforeAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { configurable: true, value: 50 });
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, value: 50 });
});
afterAll(() => {
Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetHeight);
Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth);
});
// Your tests
}) I've noticed some issues that are pretty challenging to reproduce by defaulting the value of |
This works for me:
|
I'm having this error |
@mzedeler Thanks for the workaround. It worked for me.
|
Thanks @lokeshpathrabe . Your code solved the errors but my virtualized list don't seem to render its items. Instead, I ended up using this code, based on @thielium answer:
|
For the next person who struggles to get it importing. I am using typescript and import
To get the mock to work I had to re-export the rest of react-virtualized like so:
|
@ldavidpace, thanks a lot! You saved my day. |
It is ok! thanks a lot! |
This kinda works, but ends up making every HTML element think it's 1500px tall... including Menus (and Popover), which complain bitterly:
|
In my case 500px for each dimension was unnecessary. I was able to use a value of 2 and it worked just as well:
|
@ldavidpace thank you, your code works for me. And I just improved it and made a module
And all you need just add the line |
What if I import it using |
@mjangir see this #493 (comment) |
Thanks for this!! |
I'm trying to unit test components that render RV components with the
react-test-renderer
, and running into this error: facebook/react#7371.It seems like an issue with the usage of
findDOMNode
. What is the recommended way to unit test RV components?The text was updated successfully, but these errors were encountered: