import { renderHookWithQueryClientWrapper } from '../../../utils/test/react-query';
import { useIntroducer } from '../use-introducer';
import { mockFailureResponse } from '../../../mocks/server';

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

  test('successful query hook', async () => {
    const { result, waitFor } = renderHookWithQueryClientWrapper(useIntroducer);
    await waitFor(() => result.current.isSuccess);

    expect(result.current.data).toBeDefined();
    if (result.current.data) {
      expect(result.current.data).toHaveProperty('id');
      expect(result.current.data).toHaveProperty('user');
      expect(result.current.data).toHaveProperty('iban');
      expect(result.current.data).toHaveProperty('studentCount');
      expect(result.current.data).toHaveProperty('code');
      expect(result.current.data.code).toHaveProperty('id');
      expect(result.current.data.code).toHaveProperty('text');
      expect(result.current.data.code).toHaveProperty('studentCount');
    }
  });

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

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