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.

Style wrapping in 1.0 breaks Enzyme’s `text()` assertions

See original GitHub issue

When using shallow rendering, Enzyme lets us easily use text() or contain.text() assertions to check on textual content that is the entire set of child nodes for the node being tested. Material-UI’s Typography component seems to break it.

For instance, if my component looked like this with Material UI pre-1.0:

  • This is a v1.x issue (v0.x is no longer maintained).
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Pre-1.0, we didn’t need Typography all over the place, font styling cascaded. So my component would have looked like this:

    <div className="goal">
      <div className="summary">
        <h2>{name}</h2>
        <Gauge value={progress} max={target} />
        <small>{progress} {units} sur {target}</small>
      </div>
      <div className="cta">{adderComponent}</div>
    </div>

And I was able to smoothly do:

const wrapper = shallow(<GoalTrackerWidget goal={goal} progress={progress} />)
expect(wrapper.find('h2')).to.have.text(goal.name)
expect(wrapper).to.contain.text(`${progress} ${goal.units} sur ${goal.target}`)

In 1.0, my component has to look like this:

    <div className="goal">
      <div className="summary">
        <Typography variant="title" component="h2">{name}</Typography>
        <Gauge value={progress} max={target} />
        <Typography component="small">{progress} {units} sur {target}</Typography>
      </div>
      <div className="cta">{adderComponent}</div>
    </div>

And I would absolutely expect to be able to do this:

const wrapper = shallow(<GoalTrackerWidget goal={goal} progress={progress} />)
expect(wrapper.find('Typography[component="h2"]')).to.have.text(goal.name)
expect(wrapper).to.contain.text(`${progress} ${goal.units} sur ${goal.target}`)

If necessary, by using shallow = createShallow({ untilSelector: 'WithStyles' }) instead of Enzyme’s built-in shallow.

Current Behavior

However much I try, tweaking createShallow options (adding dive: true or not, etc.), when I stumble on a Typography component, its stated text is always <WithStyles(Typography) />, regardless of actual content. It nevers provides its child text as part of its shallow rendering. I have to resort to some klunky code much like the one found in your own test suites, like:

expect(wrapper.find('.name').childAt(0)).to.have.text(goal.name)
expect(wrapper.find('.details').children().map((c) => c.text()).join('')).to.equal(
  `${progress} ${goal.units} sur ${goal.target}`
)

(that latter expression doesn’t even work properly, as a shallow wrapper node over a number doesn’t yield the proper text, apparently)

Steps to Reproduce (for bugs)

  1. Go to this Codesandbox
  2. Wait for full module transpiling, and tests to run
  3. Check the console tab: it displays the debug() view of the Typography node. You can clearly see it has a single text child with “Material-UI”.
  4. Check the test output: the wrapper’s text() result is a fixed, context-free <WithStyles(Typography) />, instead of the node’s actual text contents.

Context

This completely impedes my ability to test the presence of text contents / fragments inside my React components.

Your Environment

Tech Version
Material-UI v1.2.1
React v16.4.1
Enzyme v4.19.1
Jest v23.1.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Jun 20, 2018

@tdd Thanks for opening this issue. Digging more into it, I start to believe it’s enzyme related. The behavior difference v0.x and v1.x can be expected. The large majorities of the v1.x component are now exported with a withStyles() higher-order component. It’s changing the enzyme behavior. Using createShallow won’t help.

This comment might give us some light 💡: https://github.com/airbnb/enzyme/issues/692#issuecomment-261407772. The best solution I can think of is:

-expect(typoElement.text()).toEqual("Material-UI");
+expect(typoElement.children().text()).toEqual("Material-UI");

https://codesandbox.io/s/q3w4r2o89j

0reactions
tddcommented, Jun 21, 2018

That is so weird, it b0rks on my own code when I get multiple children like this…?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Style wrapping in 1.0 breaks Enzyme's text() assertions #11927
When using shallow rendering, Enzyme lets us easily use text() or contain.text() assertions to check on textual content that is the entire ...
Read more >
Modern React testing, part 2: Jest and Enzyme - Artem Sapegin
You'll learn how to test React components with Jest and Enzyme and ... text using the Enzyme's text() method and Jest's toMatch() assert....
Read more >
producthunt - Bountysource
I think there is bug (or perhaps a mis-documentation) of the style() assertion, namely that it only works if you mount() the component....
Read more >
Testing React Components with Enzyme and Mocha
Enzyme, created by engineers at Airbnb, is “a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse ...
Read more >
text() => String - Enzyme - GitHub Pages
.text() => String. Returns a string of the rendered text of the current render tree. ... Note: can only be called on a...
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