1

Header.js

const formSubmitHandle = () => {
   if (string !== "") {
       httpService.fetchData(var1, var2).then((res) => {
           //stuff to do...
       });
   } else {
      console.log("Type something to search");
   }
};

HttpService.js

const HttpService = {

     fetchData(var1,var2) {
        const url = "https://www.themealdb.com/api/json/v1/1/" + some code..

        return axios.get(url);
    },
};

Testing.js (the spying function )

  const apiFunc = jest
      .spyOn(HttpService, "fetchData")
      .mockImplementationOnce(() => {
          return Promise.resolve({
              json: () => Promise.resolve(MockResponseData),
         });
      });

And the error I'm getting

 TypeError: Cannot read property 'then' of undefined

  26 |   const formSubmitHandle = () => {
  27 |     if (keypress !== "") {
> 28 |       httpService.fetchData(var1, var2).then((res) => {
     |       ^

The function call is returned and works properly in the actual app, so why is it getting undefined?

--> return axios.get(url);

R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
Lajos Bela
  • 49
  • 6

0 Answers0