jest test : Cannot read property 'status' of undefined

Viewed 1613

I am trying to test a function with Axios get request. when I get a response I'm calling another function to filter the JSON document.

// sample.js

import axios from "axios";

public async getFruitData{
  const response = await axios.get("https://api.url.com/search/fruit", {
    params: {
      client_id: process.env.REACT_APP_UNSPLASH_TOKEN,
      query: term
    }
  });

return this.filtersResponse(response.data.results); //<---throwing error
};

filterResponse(detailsResponse) {
  const excludeFruit = ['apple'];
  return detailsResponse.filter(record => !excludeFruit.includes(record.fruit_type));
}

here my jest test

import axios from 'axios';
import Fruit from '../app/fruit';

jest.mock('axios');

test('it filter jsont fruit  ', async () => {
  mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: 'test' }));
  const result = await getFruitData();
  expect(result).toBe({"data": "test"});
  expect(filterResponse(record)).toBeDefined()
});

when I run the test it gave me the following error --> TypeError: Cannot read property 'status' of undefined

How can I test this scenario

0 Answers
Related