![](/img/trans.png)
[英]How to test react component with onClick function Jest/React testing library
[英]Test API call in Onclick Function using JEST and React Testing Library
我需要幫助測試 Onclick Function 中的 API 調用
export function functionName (a, b, c){
const handleValue = async() =>{
axios.get(url, {method: 'post', body:{a: "a"}};
}
return <div>
{getvalue &&
<Button Onclick= {handleValue}>Button </Button>}
</div>
}
請幫助我了解如何執行此操作?
您可以使用以下玩笑來模擬您的 API 調用:
import axios from 'axios';
jest.mock('axios');
test('your test name', () => {
axios.post.mockResolvedValue({data: yourSampleResponsePayload});
// rest of your test contents such as button click and so on
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.