售后服务电话笑话单元测试失败

时间:2020-07-16 06:23:57

标签: reactjs jestjs axios jest-mock-axios

点击“继续”按钮后,它将发出售后服务电话并返回响应

//组件代码home.js

    import React, { useState } from 'react';
    import axios from 'axios';
    const CheckDetails = () => {
      const [email, setEmail] = useState(''); 
      
      // code for onchange email input field

      const handleOnContinue = () => {            
        const payload = {        
            usermail: email,
        };
        axios.post('/users', payload)
        .then((response) => {
         console.log(response);
         window.location.href = response.data.payModuleUrl;
        }, (error) => {
         console.log(error);
        });      
      }
     return(
       <div>
        <input type="text" onChange={onChangeEmail}/>
         <button id="btn-continue" type="button" class="button" onClick={handleOnContinue}>Continue</button> 
      </div>
     );
  };

home.test.js

   import React from 'react';
    import { mount } from 'enzyme';
    import mockAxios from 'axios';
    import { act } from 'react-dom/test-utils';
    import home from './home';
    const wrapper =  mount(<home/>);
    describe('Home component', () => {
      it('Should render Home component', () => {
        expect(wrapper.exists()).toEqual(true);
      });
    });
    describe('Continue button functionality', () => {
      it(`On click of continue button, makes a call to navigates to pay module`, () => {
        const responseData = {
         payModuleUrl: 'https://www.testPaymodule/00827382'
        } 
       jest.mock('axios');
       act(() => {
        mockAxios.post.mockImplementationOnce(() => Promise.resolve({ data: responseData }));
       });
       wrapper.find('#btn-continue').hostNodes().simulate('click');
        //expect(mockAxios.post).toHaveBeenCalled();
        expect(mockAxios).toHaveBeenCalledTimes(1);
      });
   });

对于以上代码“单击继续按钮,进行呼叫以导航至付款模块”失败 收到的电话数量:0预期的电话数量:1 enter image description here 任何人都可以帮助我,如何使用笑话为邮政服务电话编写测试

0 个答案:

没有答案