牛魔王的故事
2021-06-23 17:13:22
我有一個樣式按鈕:const MyButton = styled.button`...`我用onClick道具渲染它:<MyButton onClick={props.onClick}>A Button</MyButton>在我的按鈕測試文件中,我使用酶來測試onClick(樣式按鈕導(dǎo)入為“按鈕”):let counter = 0;const component = shallow( <Button onClick={() => counter++}> A Button </Button>);component.find(Button).simulate('click');在控制臺中我得到: Method “simulate” is meant to be run on 1 node. 0 found instead.使用調(diào)試時,component.debug()我看到元素是<styled.button>...</styled.button> 我嘗試更改我find()的接收styled.button甚至添加一個類名,我在調(diào)試時可以看到該類名,但似乎沒有得到元素。如何找到元素并在其上模擬事件?謝謝
2 回答

蠱毒傳說
TA貢獻(xiàn)1895條經(jīng)驗 獲得超3個贊
當(dāng)component.find(Button)您試圖在按鈕內(nèi)找到一個按鈕時,您的示例中并非如此。去吧:
let counter = 0;
const component = shallow(
<Button onClick={() => counter++}>
A Button
</Button>
);
component.simulate('click');

桃花長相依
TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊
如果您正在測試 MyButton,那么您應(yīng)該導(dǎo)入它并使用它:
import MyButton from './some-button';
const component = shallow(<MyButton />);
component.find(MyButton).simulate('click');
但是,您是在使用未導(dǎo)入的還是在實際代碼中使用的?
添加回答
舉報
0/150
提交
取消