【问题标题】:How do I test if a React element contains text in Enzyme?如何测试 React 元素是否包含 Enzyme 中的文本?
【发布时间】:2021-01-03 13:56:59
【问题描述】:

假设我有一个 React 元素:

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

使用 Enzyme,我如何测试元素是否包含特定的文本?

【问题讨论】:

    标签: reactjs enzyme


    【解决方案1】:

    您应该找到整个文本并使用普通 JavaScript 确定它是否包含该文本,然后通过 Enzyme 测试您的发现是否为 true

    it('should show correct text', () => {
        const wrapper = shallow(<div />);
        expect(wrapper.text().includes('foo')).toBe(true);
    });
    

    【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2018-03-12
    • 2016-11-11
    • 1970-01-01
    • 2020-04-16
    • 2010-11-13
    相关资源
    最近更新 更多