如何测试其他组件的属性中存在的组件的功能?

时间:2019-11-07 13:24:12

标签: reactjs typescript unit-testing jestjs enzyme

我已经使用了Material-ui的List组件。在此组件内部,我有一个prop副标题,其中包含一个复选框。我想测试复选框的onClick功能。

            <List
                component="nav"
                subheader={<span style={{ display: 'inline-flex', width: '100%' }}>
                    < ListSubheader style={{ top: 'inherit', width: '80%', fontSize: '30px', fontWeight: 'bold' }}>
                        {HEADING.industries}
                    </ListSubheader >
                    <Checkbox
                        color="default"
                        checked={this.state.checkedSelectAll}
                        onClick={handleSelectAll}
                    />
                    <Typography className="selectAllText">
                        Select All
                    </Typography>
                </span>}
            >

我写了:

it('should call handleSelectAll', () => {

    wrapper = shallow(<IndustryGroupModal {...props} />);

    wrapper.find(Checkbox).simulate('click');

});

出现此错误:

Method “simulate” is meant to be run on 1 node. 0 found instead.

1 个答案:

答案 0 :(得分:0)

尝试一下,我认为应该足够了:

it('should call handleSelectAll', () => {

    wrapper = shallow(<IndustryGroupModal {...props} />);


    wrapper.prop("subheader").props.children[1].props.onClick()

});

我希望这能解决您的问题,祝您愉快!