question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing components using both widthWidth and withStyles

See original GitHub issue

When 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:

  1. 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
erkdecommented, Sep 19, 2018

Thanks @oliviertassinari - using find on the fragment is what I was missing, then recursing with shallow is working fine:

  beforeEach(() => {
    text = 'Hello, world!';
    shallow = createShallow({ dive: true });
    wrapper = shallow(<Message text={text} />).find("WithStyles(Message)");
    wrapper = until.call(wrapper.shallow({}), 'Message');
  });

  it('should render the text prop', () => {
    expect(wrapper.find('WithStyles(Typography)').html()).toContain(text);
  });
0reactions
oliviertassinaricommented, Sep 18, 2018

@eideer It’s because the withWith is using a fragment. See #12825.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found