/* eslint-disable camelcase */
import { cleanup } from '@testing-library/react';
import { processRelations } from './relations';
import getData from '~tests/factories/data/data.factory';
import getPageList from '~tests/factories/pages/page.factory';

describe('Processor Relations', () => {
  test('Operator', () => {
    const relation_type = 'operator';
    const market = 'ie_en';
    const data = getData({ template: relation_type, count: 8 });
    const pages = {
      [market]: {
        [relation_type]: getPageList({ template: relation_type, count: 3, relation_id: 5 }),
        payment_method: [{ id: 1, path: 'payment/path' }],
      },
    };

    pages[market][relation_type].forEach((page, index) => {
      processRelations(page, relation_type, pages, market, data, index);
      expect(page.relation_type).toEqual(relation_type);
    });
    expect(pages[market][relation_type]).toHaveLength(3);
  });
  test('Game', () => {
    const relation_type = 'game';
    const market = 'ie_en';
    const data = getData({ template: relation_type, count: 3 });
    const pages = {
      [market]: {
        [relation_type]: getPageList({ template: relation_type, count: 3, relation_id: 2 }),
        software_provider: [{ id: 3 }],
      },
    };

    pages[market][relation_type].forEach((page, index) => {
      processRelations(page, relation_type, pages, market, data, index);
      expect(page.relation_type).toEqual(relation_type);
      expect(page.relation.id).toEqual(2);
    });
    expect(pages[market][relation_type]).toHaveLength(3);
  });
  test('Author', () => {
    const relation_type = 'author';
    const market = 'ie_en';
    const data = getData({ template: relation_type, relations: {} });
    const pages = {
      [market]: {
        [relation_type]: getPageList({
          template: relation_type,
          count: 3,
          relation: null,
          relation_id: 5,
        }),
      },
    };

    pages[market][relation_type].forEach((page, index) => {
      processRelations(page, relation_type, pages, market, data, index);
      expect(page.relation_type).toEqual(relation_type);
      expect(page.relation.name).toEqual('Author 5 Name');
    });
    expect(pages[market][relation_type]).toHaveLength(3);
  });
  test('Default', () => {
    const relation_type = 'page';
    const market = 'ie_en';
    const data = getData({ template: relation_type });
    const pages = {
      [market]: {
        [relation_type]: getPageList({ template: relation_type, relation_id: 5, type: 'default' }),
      },
    };

    pages[market][relation_type].forEach((page, index) => {
      processRelations(page, relation_type, pages, market, data, index);
      expect(page.relation_type).toEqual('page');
    });
    expect(pages[market][relation_type]).toHaveLength(1);
  });
});

afterEach(() => {
  cleanup();
});
