import { mockFailureResponse } from '../../mocks/server';
import { APIErrorType } from '../index';
import { createSettleRequest, fetchSettleRequests } from '../settle-request';
import { mockedIBan } from '../../mocks/data';

const settleRequestProperties = ['id', 'status', 'iban', 'transaction', 'created'];
const transactionProperties = ['id', 'amount', 'date', 'paidFor'];

describe('fetch settle requests api', () => {
  test('successful fetching settle requests', async () => {
    const response = await fetchSettleRequests();

    expect(response).toBeDefined();
    for (const responseElemet of response) {
      for (const settleRequestProperty of settleRequestProperties) {
        expect(responseElemet).toHaveProperty(settleRequestProperty);
      }
      if (responseElemet.transaction) {
        for (const transactionProperty of transactionProperties) {
          expect(responseElemet.transaction).toHaveProperty(transactionProperty);
        }
      }
    }
  });

  test('failure fetching list settle requests', async () => {
    mockFailureResponse();
    try {
      await fetchSettleRequests();
      // 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 settle requests api', () => {
  const data = { iban: mockedIBan };

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

    expect(response).toBeDefined();
    expect(response).toHaveProperty('id');
    expect(response).toMatchObject({ iban: data.iban });
    expect(response).toMatchObject({ status: false });
  });

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