单元测试类型错误:无法读取未定义的属性“目标”

时间:2020-02-03 22:19:14

标签: javascript unit-testing undefined typeerror dom-events

当我单击单选按钮(来自material-ui)时,我正在尝试扩展显示框。只有一个单选按钮的名称为“ source”。

在渲染中,我有:

<Radio
   checked={this.state.someState === ix}
   value={ix}
   classes={{root: classes.checkbox, checked: classes.checked}}
   onClick={(e)=> this.handleExpandClick(e, sources)}
   name="source"
/>

handleExpandClick(event, sources) {
        const selectedSources = [...this.props.sources] || [];
        let index;
        const selectedSource = (event.target.name);
        console.log(selectedSource)
        if (event.target && event.target.checked) {
                if (selectedSources.indexOf(selectedSource) <=0) {
                    selectedSources.push(selectedSource)
                } else {
                    index = selectedSources.indexOf(selectedSource)
                    if (index >= 0) {
                        selectedSources.splice(index, 1);
                    }
                }
        } else {
            return null
        }
        this.setState({expandedSources: selectedSources})

当我console.log事件时,名称被标识为具有有效值的“源”,这是预期的。但是测试仍然失败,并说目标是“未定义的”。

单元测试:

*wrapper/instance setup here

it('should fire the handleExpandClick function when source checkbox is clicked', () => {
        const event = {target: {name: 'test'}};
        let handleClick = instance.handleExpandClick(event, sources);

        wrapper.find(Radio).at(0).simulate('click');
        expect(handleClick.calledOnce).toBe(true);
}); 

我在做什么错? 我仍在学习适当的单元测试,因此,任何建议都将不胜感激。

0 个答案:

没有答案