import { createIntroducer, fetchIntroducer } from '../introducer';
import { mockFailureResponse } from '../../mocks/server';
import { APIErrorType } from '../index';
import { mockedCreateIntroducerData } from '../../mocks/data';

const introducerProperties = ['id', 'user', 'code', 'iban', 'sharePrice', 'studentCount'];
const codeProperties = [
  'id',
  'text',
  'share',
  'discount',
  'validDays',
  'sharePrice',
  'studentCount'
];

describe('fetch introducer api', () => {
  test('successful fetching introducer', async () => {
    const response = await fetchIntroducer();

    expect(response).toBeDefined();
    for (const introducerProperty of introducerProperties) {
      expect(response).toHaveProperty(introducerProperty);
    }
    for (const codeProperty of codeProperties) {
      expect(response.code).toHaveProperty(codeProperty);
    }
  });

  test('failure fetching introducer', async () => {
    mockFailureResponse();
    try {
      await fetchIntroducer();
      // 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 introducer api', () => {
  const data = mockedCreateIntroducerData;

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

    expect(response).toBeDefined();
    for (const introducerProperty of introducerProperties) {
      expect(response).toHaveProperty(introducerProperty);
    }
    expect(response).toMatchObject({ iban: data.iban });
    for (const codeProperty of codeProperties) {
      expect(response.code).toHaveProperty(codeProperty);
    }
  });

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