1

Property 'getUsersTotalPayout` does not exist on type typeof PayoutApi

My Class:

import { bind } from 'decko';

import BaseApi from './Base';
import * as NS from './types';

class PayoutApi extends BaseApi {

  @bind
  public async getUsersTotalPayout(userId: string): Promise<number> {
    const params: NS.IGetUsersTotalPayoutRequest = { userId };
    const response = await this.actions.get<{ payout: number }>(
      '/api/get-total-payout',
      params,
    );
    return response.data.payout;
  }
}

export default PayoutApi;

The test file:

import PayoutApi from './LiquidityPool';

const endpoint = '/api/get-total-payout';

const userId = 'foo';

jest.mock(endpoint, () => ({
  getUsersTotalPayout: jest.fn(() => Promise.resolve({ data: { payout: 100.21 } }))
}));

describe('API: getUsersTotalPayout', () => {
  it('should make a request when we get images', () => {
    // const testApi = new PayoutApi();
    expect(PayoutApi.getUsersTotalPayout(userId)).toHaveBeenCalledWith(endpoint, 'GET');
  });
});

Getting that error on expect(PayoutApi.getUsersTotalPayout).toHaveBeenCalledWith(endpoint, 'GET');

0

1 Answer 1

1
  1. You are currently trying to call method on the class. Since it is not static you should instantiate object of class first.

    let api = new PayoutApi();
    expect(api.getUsersTotalPayout(userId).....)
    
  2. since jest.mock mocks module not endpoint or XHR request your test will try sending live request to /api/get-total-payout. For handling that it's needed to know what XHR wrapper do you use. Say for fetch() there is nice wrapper-mocker and libraries like axios also have their equivalence.

  3. As for test itself. It does not work if you call a method and make expect on its result. It should be running method that must call server and then checking for mocked XHR if it has been called with valid parameters:

    api.getUsersTotalPayout(userId);
    expect(fetch_or_other_wrapper_for_mocking_xhr.get_last_request_method()).toEqual('get', endpoint, userId)
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.