Created
January 20, 2017 16:55
-
-
Save elevine/69a27e067032c53d64d66f4f56e978cc to your computer and use it in GitHub Desktop.
Jest mock for the react-virtualized AutoSizer component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // __mocks__/react-virtualized.js | |
| // note - at the top of your test file, include this line: jest.mock('react-virtualized'); | |
| import React from 'react'; | |
| const reactVirtualized = jest.genMockFromModule('react-virtualized'); | |
| const autoSizerProps = { | |
| height: 100, | |
| width: 100 | |
| } | |
| const MockAutoSizer = ( props ) => { | |
| return <div> { props.children(autoSizerProps) } </div>; | |
| }; | |
| reactVirtualized.AutoSizer = MockAutoSizer; | |
| module.exports = reactVirtualized; |
If you installed only the auto-sizer sub-module, then you can use this code which is written using TypeScript and ES6 modules:
// react-virtualized-auto-sizer.tsx
import React from 'react';
type AutoSizerChildrenProps = {
children: ({ height, width }: { height: number; width: number }) => void;
};
const AutoSizer = ({ children }: AutoSizerChildrenProps) => (
<div>{children({ height: 600, width: 1000 })}</div>
);
export default AutoSizer;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try returning/exporting a new object