为什么enzyme.find(' [htmlFor]')返回两个元素而不是一个?

时间:2017-12-07 05:17:21

标签: javascript jquery reactjs enzyme

我正在测试React无状态功能,它基本上是三个单选按钮:

export default function (props) {
    return (
        <ButtonToolbar className="float-right" aria-label="Toolbar with button groups">
          <ButtonGroup className="mr-3" data-toggle="buttons" aria-label="First group"
                onChange={props.handleLeaseOption}>
            <Label check htmlFor='option_active'
                    className={props.optionSelected === 'option_active' ?
                                classNameActive :
                                classNameInactive} >
              <Input type="radio"
                     name={name}
                     id='option_active'
                     value='option_active' /> Active
            </Label>
            <Label check htmlFor='option_inactive'
                    className={props.optionSelected === 'option_inactive' ?
                                classNameActive :
                                classNameInactive} >
              <Input type="radio"
                     name={name}
                     id='option_inactive'
                     value='option_inactive' /> Inactive
            </Label>
            <Label check htmlFor='option_all'
                    className={props.optionSelected === 'option_all' ?
                                classNameActive :
                                classNameInactive} >
              <Input type="radio"
                     name={name}
                     id='option_all'
                     value='option_all' /> All
            </Label>
          </ButtonGroup>
        </ButtonToolbar>);
}

我的测试代码如下所示 - 我要做的是首先将单选按钮设置为“不活动”&#39;当用户点击按钮“活动”时然后调用mock函数。

test('Option Active', () => {
    const propsInactive = {"optionSelected": ConstVariables.optionInctive};
    const handleLeaseOption = jest.fn();
    const wrapper = mount(LeaseOption(propsInactive));
    const inputActive = wrapper.find('[htmlFor="option_active"]');

    inputActive.simulate('click');
    expect(handleLeaseOption).toBeCalledWith(ConstVariables.optionActive);
});

上面的代码我得到了错误:

Method “simulate” is only meant to be run on a single node. 2 found instead.

然后我将行选择器更改为:

const inputActive = wrapper.find('[htmlFor="' + ConstVariables.optionActive + '"]').first();

这次我收到了错误:

● Option Active

expect(jest.fn()).toBeCalledWith(expected)

Expected mock function to have been called with:
  ["option_active"]
But it was not called.

任何人都可以提供帮助:1。为什么我的选择器没有按预期工作? 2.为什么我的模拟函数没有被调用?

0 个答案:

没有答案