import { createUser, fetchUser } from '../user';
import { mockFailureResponse } from '../../mocks/server';
import { APIErrorType } from '../index';
import { mockedCreateUserData } from '../../mocks/data';

describe('fetchUser api', () => {
  test('successful fetching user', async () => {
    const response = await fetchUser();

    expect(response).toBeDefined();
    expect(response).toHaveProperty('id');
    expect(response).toHaveProperty('name');
    expect(response).toHaveProperty('mobile');
  });

  test('failure fetching user', async () => {
    mockFailureResponse();
    try {
      await fetchUser();
      // Fail test if above expression doesn't throw anything.
      expect(true).toBe(false);
    } catch (e) {
      const error = e as APIErrorType;
      expect(error.status).toBe(500);
    }
  });
});

describe('create user api', () => {
  const data = mockedCreateUserData;

  test('successful creating user', async () => {
    const response = await createUser(data);

    expect(response).toBeDefined();
    expect(response).toHaveProperty('id');
    expect(response).toMatchObject({ name: `${data.firstName} ${data.lastName}` });
    expect(response).toMatchObject({ mobile: data.mobile });
  });

  test('failure creating user', async () => {
    mockFailureResponse();
    try {
      await createUser(data);
      // Fail test if above expression doesn't throw anything.
      expect(true).toBe(false);
    } catch (e) {
      const error = e as APIErrorType;
      expect(error.status).toBe(500);
    }
  });
});
