亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模擬點擊樣式組件測試

模擬點擊樣式組件測試

牛魔王的故事 2021-06-23 17:13:22
我有一個樣式按鈕:const MyButton = styled.button`...`我用onClick道具渲染它:<MyButton onClick={props.onClick}>A Button</MyButton>在我的按鈕測試文件中,我使用酶來測試onClick(樣式按鈕導入為“按鈕”):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.使用調試時,component.debug()我看到元素是<styled.button>...</styled.button> 我嘗試更改我find()的接收styled.button甚至添加一個類名,我在調試時可以看到該類名,但似乎沒有得到元素。如何找到元素并在其上模擬事件?謝謝
查看完整描述

2 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

當component.find(Button)您試圖在按鈕內找到一個按鈕時,您的示例中并非如此。去吧:


let counter = 0;

const component = shallow(

     <Button onClick={() => counter++}>

          A Button

     </Button>

);

component.simulate('click');


查看完整回答
反對 回復 2021-06-24
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

如果您正在測試 MyButton,那么您應該導入它并使用它:


import MyButton from './some-button';

const component = shallow(<MyButton />);

component.find(MyButton).simulate('click');

但是,您是在使用未導入的還是在實際代碼中使用的?


查看完整回答
反對 回復 2021-06-24
  • 2 回答
  • 0 關注
  • 288 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號