Testing components using both widthWidth and withStyles
See original GitHub issueWhen using createShallow({ dive: true })
only one level of unwrapping seems to occur on components using both withWidth and withStyles.
- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
With a simple component, like:
const Message = ({ classes, text }) => (
<Typography className={classes.message}>{text}</Typography>
);
export default withWidth()(withStyles(styles)(Message));
we’d like to write a test asserting the content of the message, eg:
describe('render', () => {
let shallow;
let wrapper;
let text;
beforeEach(() => {
text = 'Hello, world!';
shallow = createShallow({ dive: true });
wrapper = shallow(<Message text={text} />);
console.log(wrapper.debug());
});
it('should render the text prop', () => {
expect(wrapper.find('Typography').text()).toContain(text);
});
});
Current Behavior
It looks like createShallow({ dive: true })
only unwraps withWidth, and we still have Message enclosed in withStyles.
console.log src/Message.test.js:19
<Fragment>
<WithStyles(Message) width="md" text="Hello, world!" />
<EventListener target="window" onResize={[Function: debounced]} />
</Fragment>
render › should render the text prop
Method “text” is only meant to be run on a single node. 0 found instead.
at ShallowWrapper.single (node_modules/enzyme/build/ShallowWrapper.js:1815:17)
at ShallowWrapper.text (node_modules/enzyme/build/ShallowWrapper.js:1000:21)
at Object.it (src/Message.test.js:23:39)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at <anonymous>
Steps to Reproduce
Link:
- Just run sample
yarn test
@ https://github.com/eideer/mui-hoc
Context
Your Environment
Tech | Version |
---|---|
Material-UI | v3.1.0 |
React | v16.5.1 |
enzyme | v3.6.0 |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Testing components using both widthWidth and withStyles
When using createShallow({ dive: true }) only one level of unwrapping seems to occur on components using both withWidth and withStyles.
Read more >How to use both withWidth and withStyles as higher order ...
I am trying to use both withStyles and withWidth HOC in the same functional component in my project. I am stuck and not...
Read more >How to Setup React Testing Library for Material UI Styled ...
withStyles HOC returns component with classes attribute and link your styles object to component. If you are interested in actual logic, please refer...
Read more >Best practices for unit testing with a React/Redux approach
Here's a somewhat complicated async action creator we would like to test that does one or two factor authentication:.
Read more >Style library interoperability - Material UI - MUI
This guide aims to document the most popular alternatives, but you should find that the principles applied here can be adapted to other...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @oliviertassinari - using
find
on the fragment is what I was missing, then recursing withshallow
is working fine:@eideer It’s because the withWith is using a fragment. See #12825.