【问题标题】:How to test child component rendered or not with isRequired props and onclick如何使用 isRequired 道具和 onclick 测试渲染或不渲染的子组件
【发布时间】:2021-09-25 06:46:29
【问题描述】:

这是MasterView.jsx

function MasterView(props) {
    return (
        <FilterCard title="Pass" onClick={() =>setFilter([{ id: PASSED}])}/>
        <FilterCard title="fail" onClick={() =>setFilter([{ id: Failed}])}/>
   )
}

这是MasterViewSpec.jsx

describe('<MasterView />', () => {  
    test('renders Child component', () => {
        const history = createMemoryHistory('/dashboard')
        const wrapper = mount(<MasterView history={history} />);
        expect(wrapper.find(<FilterCard title="Passed" onClick={() => setActiveFilters([{ id: 'passed' }])} />).length).toEqual(1);
    });
});

遇到以下问题 -

expect(received).toEqual(expected) // deep equality

Expected: 1
Received: undefined

【问题讨论】:

    标签: javascript reactjs jestjs


    【解决方案1】:

    来自酶mount documentation

    注意:与浅层或静态渲染不同,完全渲染实际上是将组件安装在 DOM 中,[...]

    来自shallow documentation

    浅渲染有助于限制您将组件作为一个单元进行测试,并确保您的测试不会间接断言子组件的行为。

    因此,如果您要查找FilterCard,则必须使用shallow 而不是mount

    您还以错误的方式使用了.find() 方法。您必须使用 enzyme selector 作为参数。所以你的测试代码应该是这样的:

    describe('<MasterView />', () => {  
        test('renders Child component', () => {
            const history = createMemoryHistory('/dashboard')
            const wrapper = shallow(<MasterView history={history} />);
            expect(wrapper.find(FilterCard).length).toEqual(1);
        });
    });
    

    如果你想测试一个特定的prop 是否被传递给一个子组件,你应该这样做:

    describe('<MasterView />', () => {  
        test('renders Child component', () => {
            const history = createMemoryHistory('/dashboard')
            const wrapper = shallow(<MasterView history={history} />);
            const childTitle = wrapper.find(FilterCard).at(0).props().title;
            expect(childTitle).toEqual("Passed");
        });
    });
    

    如果您想按组件构造函数及其属性查找,可以使用 FilterCard[title="Passed"] 之类的选择器。

    describe('<MasterView />', () => {  
        test('renders Child component', () => {
            const history = createMemoryHistory('/dashboard')
            const wrapper = shallow(<MasterView history={history} />);
            expect(wrapper.find(FilterCard[title="Passed"]).length).toEqual(1);
        });
    });
    

    来自documentation

    React 组件名称和道具(Button、Button[type="submit"] 等) - 但是,请注意,强烈建议通过组件构造函数/函数而不是显示名称来查找。强>

    我的亮点。

    【讨论】:

    • 遇到问题 - 方法“props”旨在在 1 个节点上运行。找到了 0 个。当我使用上面的代码时
    • 我有 5 个 子元素,每个子元素都有 6 个道具和一个 onClick 函数。都是必需的属性类型。以上两个代码快照都不起作用
    • expect(wrapper.find(FilterCard).length).toEqual(1);对于这一行,我们没有检查所需的道具。如何添加
    • 我刚刚回答了这个问题。如果您想进行更多检查,请更具体。您希望对所需道具进行什么类型的验证?当您执行wrapper.find(FilterCard) 时,您将获得所有FilterCards 孩子。然后,您可以使用 .prop("title") 访问他们的属性,然后再使用 expect
    • 当我执行这个时- const childTitle = wrapper.find(FilterCard).at(1).props().title; ,我遇到了问题-方法“props”旨在在 1 个节点上运行。找到了 0 个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    • 2018-01-11
    • 2020-01-20
    • 2021-11-07
    • 2012-07-18
    • 2019-06-25
    • 2020-07-17
    相关资源
    最近更新 更多