import { renderHookWithQueryClientWrapper } from '../../../utils/test/react-query';
import { useUser } from '../use-user';
import * as userAPI from '../../../api/user';
import { mockFailureResponse } from '../../../mocks/server';

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

  test('successful query hook', async () => {
    const fetchUserSpy = jest.spyOn(userAPI, 'fetchUser');
    const { result, waitFor } = renderHookWithQueryClientWrapper(useUser);
    await waitFor(() => result.current.isSuccess);

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

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

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