簡體   English   中英

使用 JEST 和 React 測試庫在 Onclick Function 中測試 API 調用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM