1

I'm trying to mock API call with library calls 'jest-when'. It works correctly When I call method GET, but it doesn't work when it's method POST.

The API call that I want to mock is here

  const use = () => {
    fetch(
      `/api/v1/coupons/17/ja?coupon_id=17033&very_id=100&type=use`,
      {
        method: 'POST',
      }
    ).then(() => {
      setUseButton(true);
    });
  };

And I tried to write mock like below

global.fetch = jest.fn();
when(global.fetch)
  .calledWith('/api/v1/coupons/17/ja?coupon_id=17033&very_id=100&type=use&result_no=313',{
    method: 'POST',
  })
  .mockReturnValue(
    Promise.resolve({
      json: () => Promise.resolve(COUPON_RESPONSE),
    })
  );

But there's an error like below, and I have no idea to fix it.

TypeError: Cannot read properties of undefined (reading 'then')
  {
    method: 'POST',
   }
 ).then((res) => {
      setUseButton(true);
Yuzu
  • 59
  • 1
  • 8

0 Answers0