import { renderHookWithQueryClientWrapper } from '../../../utils/test/react-query';
import * as settleRequestAPI from '../../../api/settle-request';
import { mockFailureResponse } from '../../../mocks/server';
import { useSettleRequests } from '../use-settle-request';

describe('useSettleRequests hook', () => {
  test('fetching query hook', async () => {
    const { result } = renderHookWithQueryClientWrapper(useSettleRequests);
    expect(result.current.isFetching).toBeTruthy();
    expect(result.current.status).toBe('loading');
  });

  test('successful query hook', async () => {
    const fetchIntroducerTransactionsSpy = jest.spyOn(settleRequestAPI, 'fetchSettleRequests');
    const { result, waitFor } = renderHookWithQueryClientWrapper(useSettleRequests);
    await waitFor(() => result.current.isSuccess);

    expect(fetchIntroducerTransactionsSpy).toHaveBeenCalled();
    expect(result.current.isSuccess).toBeTruthy();
    expect(result.current.data).toBeDefined();
    jest.restoreAllMocks();
  });

  test('failure query hook', async () => {
    mockFailureResponse();
    const { result, waitFor } = renderHookWithQueryClientWrapper(useSettleRequests);
    await waitFor(() => result.current.isError);

    expect(result.current.isError).toBeTruthy();
    expect(result.current.error).toBeDefined();
  });
});
