-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
text() not returning content for React elements #692
Comments
As a temporary workaround, would |
@ljharb In the example above it would, but depending on the implementation of
Using |
Clarifying per our offline discussion: it sounds like what you want is "get the text of the children prop" - since Given that, I suspect your best bet is |
I have noted this problem too. |
What if there was a |
OK, to restate: if you have const jsx = <Bar><Foo>hello</Foo></Bar>;
const wrapper = shallow(jsx);
assert.equal(wrapper.text(), '<Foo />'); // current implementation
const fooChildren = wrapper.find(Foo).prop('children');
assert.equal(React.Children.toArray(fooChildren), ['hello']); // works, but couples to the exact jsx structure of how `Bar` renders `Foo`'s children, so, bad.
assert.equal(shallow(<div>{fooChildren}</div>).text(), 'hello'); // works perfectly, but is inelegant
const foo = wrapper.dive();
assert.equal(foo.text(), 'hello'); // this depends on the `render` implementation of `Foo`. A method like Maybe a method like I'm still not convinced this is an edge case we want to support, but that seems like it might be a clean way to implement it if we do. |
I don't think this is still an issue in v3; please file a new issue if that's not the case. |
I am experiencing this issue with jest-enzyme 6.0.2 and enzyme 3..3.0 (according to npm outdated these are the latest versions). The |
@mddrill |
Dear @ljharb |
See #1436 for React Native issues. |
Say I have the following simple example:
And I want to verify the
text()
(children) of the component is either "Hello" or "Hello, Miles". This doesn't work when wrapped with React components, as it simply returns<Foo />
whentext()
is used.It's caused by these lines in
ShallowTraversal
:Couldn't this simply check
props.children
, stringify them, and join them? Is there a reason that usingtext()
on a React element is prohibited?Otherwise, I have to write tests like the following.
Which is kind of annoying, as the
span
is irrelevant.The text was updated successfully, but these errors were encountered: