Enzyme: dangerouslySetInnerHTML and text() not outputting

Created on 27 May 2016  路  8Comments  路  Source: enzymejs/enzyme

I have a simple <p> component that has a dangerouslySetInnerHTML and when doing a shallow render of:

<div>
    <p dangerouslySetInnerHTML={{ __html: 'blah' }} />
</div>

_wrapper.find('p').text() doesn't return 'blah' as expected.
_wrapper.find('p').html() returns 'blah' wrapped in the <p> tag.

Have a branch that proves that text() doesn't work

shallow bug

Most helpful comment

You could try using the .render() function like this on the shallow rendered object: https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/render.md

All 8 comments

Thanks for the report @silasb. Just curious, does it work as expected with mount? I'll look into this and try to reproduce when I get a chance.

Forgot to include the failing tests in my branch. I added a test for mount as well and it passes just fine.

You could try using the .render() function like this on the shallow rendered object: https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/render.md

You could try using the .render() function like this on the shallow rendered object: https://github.com/airbnb/enzyme/blob/master/docs/api/ShallowWrapper/render.md

This doesn't work for me in v3, apparently due to this issue.

@JasoonS that worked for me, thanks!

It works for me if I render() first. (v3+)

it("does something", () => {
    expect(component.render().text()).toEqual('assertion');
});

[v3.7.0]

I'm having the same issue but with mount. I tried to make it working by render() and then text() or html() and didn't work.
It generate the primitive text but not the element I've passed.

@raulfdm .text() and .html() are different (text wouldn't show html, and .html calls render); but i'd expect the element to show up in .debug() somehow. does it not?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amcmillan01 picture amcmillan01  路  3Comments

AdamYahid picture AdamYahid  路  3Comments

timhonders picture timhonders  路  3Comments

nelsonchen90 picture nelsonchen90  路  3Comments

mattkauffman23 picture mattkauffman23  路  3Comments