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

How to jest mock the size #193

Open
miyagiborn opened this issue Jun 8, 2020 · 3 comments
Open

How to jest mock the size #193

miyagiborn opened this issue Jun 8, 2020 · 3 comments

Comments

@miyagiborn
Copy link

Are there any examples on how to mock the size for jest tests?

I'm using the sizeMe()(MyComponent) and expecting the size to come in as a prop. I've tried mocking the getBoundingClientRect but the size.width is still returning undefined.

@ilanrot90
Copy link

add this modal to your mocks folder
`import React from 'react';

const sizeMeProps = {
size: {
height: 770,
position: null,
width: 1200,
},
};

const sizeMe = (options) => (Component) => {
return (props) => <Component size={sizeMeProps.size} {...props} />;
};

export const SizeMe = (props) => {
return props.children(sizeMeProps);
};

export default sizeMe;`

@TomPlum
Copy link

TomPlum commented Apr 3, 2021

I'm using TypeScript and did the following.

My Props interface extended the SizeMeProps.
I exported with the withSize() HOC: export default withSize()(MyComponent).

Then I defined the size props in MyComponent.test.tsx.

const props = { size: { height: 25, width: 70, } };

Then simply spread them into my component when rendering in the test:

render(<MyComponent someRequredProps={blah} {...props} />);

@william10000
Copy link

william10000 commented Aug 11, 2021

We put react-sizeme.js in the src/__mocks__ directory

const sizeMeProps = {
  size: {
    height: 100,
    width: 100,
  },
};

export const withSize = () => (SizedComponent) => (props) => (
  <SizedComponent size={sizeMeProps.size} {...props} />
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants