lundi 14 décembre 2020

I want to test react API with moxios

I am learning testing with jest and enzyme. I want to test an API with moxios but facing a lot of challenges. I have an API call as below:

useEffect(() => {axios.get(`https://API/call/with/dynamicvalue/${id}`,
        {
            headers: {
              Authorization: `Basic ${btoa(getToken())}`,
           },
          }).then((response)=>{
              let MoreData=response.data;
              console.log(MoreData)

        setFullAuditDetails(MoreData.data.slice(0,20).map(d=>{
            return{
            timeF: d.time.split('T')[0],
            actionF: d.method,
            userF: d.userName.split('.')[0]
            }
        }))
    })}, [])

I want to test this with moxios. I have done this:

const api = `https://API/call/with/dynamicvalue/372c7861-e09a-41ae-8c6d-7bbc7877ad79`

describe("Tests for API", () => {
    beforeEach(() => {
        moxios.install()
        moxios.stubRequest(api, {
        status: 200,
        response: {  "success": true }
    })
})
    
      afterEach(() => {
        moxios.uninstall()
      })

      test("Check for the response", (done) => {
        moxios.wait(() => {
            wrapper2.update();
        }).then(response => {
            console.log(response);
            expect('status').toEqual('200');
            done();
            wrapper2.unmount();
        });
      })
})

Getting error:

TypeError: Cannot read property 'then' of undefined

  39 | 
  40 |       test("Check for the response", (done) => {      
> 41 |         moxios.wait(() => {
     |         ^
  42 |             wrapper2.update();
  43 |         }).then(response => {
  44 |             console.log(response);

But not sure how to do it. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire