import { checkSmsKeyRequest, createTempUser, sendSmsKeyRequest } from '../temp-user';
import { mockFailureResponse } from '../../mocks/server';
import { APIErrorType } from '../index';
import {
  mockedMobile,
  mockedRestrictedMobile,
  mockedSmsKey,
  mockedTempUserData,
  mockedWrongSmsKey
} from '../../mocks/data';

describe('create tempUser api', () => {
  const data = mockedTempUserData;

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

    expect(response).toBeDefined();
    expect(response).toMatchObject({ mobile: data.mobile });
  });

  test('failure creating tempUser', async () => {
    mockFailureResponse();
    try {
      await createTempUser(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);
    }
  });
});

describe('tempUser send smsKey api', () => {
  const data = mockedTempUserData;

  test('successful send smsKey', async () => {
    const response = await sendSmsKeyRequest(data);

    expect(response).toBeDefined();
    expect(response).toMatchObject({ mobile: data.mobile });
    expect(response).not.toHaveProperty('authKey');
  });

  test('failure send smsKey', async () => {
    mockFailureResponse();
    try {
      await sendSmsKeyRequest(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);
    }
  });

  test('failure send smsKey with error 403', async () => {
    const data = { mobile: mockedRestrictedMobile };
    try {
      await sendSmsKeyRequest(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(403);
      expect(error.data).toBe('محدودیت‌ ارسال کد پیامکی برای شما به پایان رسیده است.');
    }
  });
});

describe('tempUser check smsKey api', () => {
  test('successful check smsKey', async () => {
    const data = { mobile: mockedMobile, smsKey: mockedSmsKey };
    const response = await checkSmsKeyRequest(data);

    expect(response).toBeDefined();
    expect(response).toMatchObject({ mobile: data.mobile });
    expect(response).toHaveProperty('authKey');
  });

  test('failure check smsKey', async () => {
    mockFailureResponse();

    const data = { mobile: mockedMobile, smsKey: mockedSmsKey };
    try {
      await checkSmsKeyRequest(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);
    }
  });

  test('failure check smsKey with error 403', async () => {
    const data = { mobile: mockedRestrictedMobile, smsKey: mockedSmsKey };
    try {
      await checkSmsKeyRequest(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(403);
      expect(error.data).toBe('محدودیت‌ وارد کردن کد پیامکی اشتباه برای شما به پایان رسیده است.');
    }
  });

  test('failure check smsKey with other errors', async () => {
    const data = { mobile: mockedMobile, smsKey: mockedWrongSmsKey };
    try {
      await checkSmsKeyRequest(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(400);
      expect(error.data.smsKey).toBe('کد وارد شده اشتباه است.');
    }
  });
});
