【问题标题】:Test API call in Onclick Function using JEST and React Testing Library使用 JEST 和 React 测试库在 Onclick 函数中测试 API 调用
【发布时间】:2022-01-19 22:13:47
【问题描述】:

我需要帮助测试 Onclick 函数中的 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>
}

请帮助我了解如何执行此操作?

【问题讨论】:

    标签: javascript reactjs typescript jestjs react-testing-library


    【解决方案1】:

    您可以使用以下玩笑来模拟您的 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
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-04
      • 2020-03-24
      • 2020-06-14
      • 2020-06-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多