-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Style wrapping in 1.0 breaks Enzyme’s text()
assertions
#11927
Comments
@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 This comment might give us some light 💡: enzymejs/enzyme#692 (comment). The best solution I can think of is: -expect(typoElement.text()).toEqual("Material-UI");
+expect(typoElement.children().text()).toEqual("Material-UI"); |
Hey @oliviertassinari that is correct. There's actually another bug I just submitted on the incorrect rendering by This trick works when your single child is the text node (in which case I would advocate that string interpolation for child contents not be done with JSX: // Not a good idea, test-wise and vDOM-wise
return (
<Typography…>
{progress} {units} out of {target}
</Typography>
) …but rather be done using ES Template Strings in a single text child node: // More practical in all other aspects
return (
<Typography…>
{`${progress} ${units} out of ${target}`}
</Typography>
) At least this made it much easier for me to test and looks much better in, say, Jest snapshots. |
For some reason this works too: <Typography variant="display1" gutterBottom>
{"Material"} {"UI"}
</Typography> const typoElement = wrapper.find(
'WithStyles(Typography)[variant="display1"]'
);
console.log(typoElement.debug());
expect(
typoElement
.dive()
.dive()
.text()
).toEqual("Material UI"); Maybe using the mount API in this case would make more sense. |
Yes, I think that's because there are no inline whitespace elements between the interpolated elements. |
That is so weird, it b0rks on my own code when I get multiple children like this…? |
When using shallow rendering, Enzyme lets us easily use
text()
orcontain.text()
assertions to check on textual content that is the entire set of child nodes for the node being tested. Material-UI'sTypography
component seems to break it.For instance, if my component looked like this with Material UI pre-1.0:
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:And I was able to smoothly do:
In 1.0, my component has to look like this:
And I would absolutely expect to be able to do this:
If necessary, by using
shallow = createShallow({ untilSelector: 'WithStyles' })
instead of Enzyme's built-inshallow
.Current Behavior
However much I try, tweaking
createShallow
options (addingdive: true
or not, etc.), when I stumble on aTypography
component, its statedtext
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:(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)
debug()
view of theTypography
node. You can clearly see it has a single text child with "Material-UI".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
The text was updated successfully, but these errors were encountered: