Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模拟点击样式化组件测试_Javascript_Reactjs_Enzyme - Fatal编程技术网

Javascript 模拟点击样式化组件测试

Javascript 模拟点击样式化组件测试,javascript,reactjs,enzyme,Javascript,Reactjs,Enzyme,我有一个样式化的按钮: const MyButton = styled.button`...` 我用onClickprop渲染它: <MyButton onClick={props.onClick}>A Button</MyButton> 在控制台中,我得到:方法“simulate”将在1个节点上运行。改为找到0。 使用component.debug()进行调试时,我看到元素是… 我尝试更改我的find()以接收styled.button,甚至添加了一个类名,我可以在

我有一个样式化的按钮:

const MyButton = styled.button`...`
我用
onClick
prop渲染它:

<MyButton onClick={props.onClick}>A Button</MyButton>
在控制台中,我得到:
方法“simulate”将在1个节点上运行。改为找到0。

使用
component.debug()
进行调试时,我看到元素是
我尝试更改我的
find()
以接收
styled.button
,甚至添加了一个类名,我可以在调试时看到这个类名,但似乎什么都没有得到元素

如何找到元素并在其上模拟事件?
谢谢

如果您正在测试MyButton,那么您应该导入它并使用它:

import MyButton from './some-button';
const component = shallow(<MyButton />);
component.find(MyButton).simulate('click');
从“/some button”导入MyButton;
常量分量=浅();
component.find(MyButton.simulate('click');
但是,您正在使用未导入的按钮,或在实际代码中使用的按钮?

组件一起使用。查找(按钮)
您试图在按钮中查找按钮,但在您的示例中并非如此。只需选择:

let counter = 0;
const component = shallow(
     <Button onClick={() => counter++}>
          A Button
     </Button>
);
component.simulate('click');
let计数器=0;
常数分量=浅(
计数器+++}>
钮扣
);
component.simulate('click');

您好,如果不清楚,很抱歉,我提到我正在导入它。测试文件还有一些jest dom测试,它使用导入的按钮
let counter = 0;
const component = shallow(
     <Button onClick={() => counter++}>
          A Button
     </Button>
);
component.simulate('click');