0

Let's say I have a React element:

<p>
    <span class="first-part">
        foo
    </span>
    <span class="second-part">
        bar
    </span>
</p>

Using Enzyme, how do I test if the element contains a specific piece of text?

Dykotomee
  • 728
  • 6
  • 20

1 Answers1

0

You should find the entire text and determine if it contains the text using vanilla JavaScript, then test if your discovery is true or not via Enzyme:

it('should show correct text', () => {
    const wrapper = shallow(<div />);
    expect(wrapper.text().includes('foo')).toBe(true);
});
Dykotomee
  • 728
  • 6
  • 20
  • Thank you @Nelu for the original answer. See https://stackoverflow.com/questions/38277165/how-to-select-element-text-with-reactenzyme#answer-53490555. – Dykotomee Sep 16 '20 at 17:24