import React from 'react';
import { render, cleanup, waitFor, screen } from '@testing-library/react';
import { getSampleOperatorCards } from 'gatsby-core-theme/tests/factories/modules/card.factory';
import TemplateTwo from './index';

let data;
let props;
let relation;

beforeEach(() => {
  data = getSampleOperatorCards();

  props = {
    item: data.items[0],
    page: {
      template: 'operator_review',
      true_votes: 134,
      false_votes: 20,
    },
  };

  relation = props.item.relation;
});

describe('TemplateTwo component', () => {
  test('render with props', () => {
    const { container } = render(<TemplateTwo {...props} />);
    expect(container).toBeTruthy();
  });
  test('show correct logo and title', async () => {
    // eslint-disable-next-line global-require
    const func = require('gatsby-core-theme/src/helpers/device-detect');
    func.isNativeImageLazyLoadingSupported = jest.fn(() => true);

    const { getByText } = render(<TemplateTwo {...props} />);

    await waitFor(() => {
      const img = screen.getByAltText(relation.standardised_logo_url_object.alt);
      expect(img).toBeVisible();
      expect(img).toHaveAttribute(
        'src',
        'https://cdn.images.com/1656339756/casumo-casino-logo.svg'
      );
      expect(img).toHaveAttribute('width', '88');
      expect(img).toHaveAttribute('height', '88');
      expect(getByText(relation.name)).toBeTruthy();
    });
  });
  test('show correct author image', async () => {
    // eslint-disable-next-line global-require
    const func = require('gatsby-core-theme/src/helpers/device-detect');
    func.isNativeImageLazyLoadingSupported = jest.fn(() => true);

    render(<TemplateTwo {...props} />);

    await waitFor(() => {
      const img = screen.getByAltText(props.item.author.name);
      expect(img).toBeVisible();
      expect(img).toHaveAttribute(
        'src',
        'https://cdn.images.com/fit-in/21x21/1666946339/cameron-murphy.png'
      );
      expect(img).toHaveAttribute('width', '21');
      expect(img).toHaveAttribute('height', '21');
    });
  });
  test('if no author data, show nothing', async () => {
    props = {
      ...props,
      item: {
        ...props.item,
        author: null,
      },
    };
    const { container } = render(<TemplateTwo {...props} />);

    await waitFor(() => {
      expect(container.querySelector('.authorData')).toBeFalsy();
    });
  });
  test('show correct review link', async () => {
    const { getByText } = render(<TemplateTwo {...props} />);
    const link = getByText(`${relation.name} Review`);

    await waitFor(() => {
      expect(link).toBeTruthy();
      expect(link).toHaveAttribute('href', props.item.path);
    });
  });
  test('show correct bonus text', async () => {
    const { container } = render(<TemplateTwo {...props} />);

    await waitFor(() => {
      expect(container.querySelectorAll('.detailsBox span').length).toBe(2);
      expect(container.querySelectorAll('.detailsBox span')[0].innerHTML).toBe(
        relation.one_liners.main.one_liner.split('+')[0].replace('&', '&amp;')
      );
      expect(container.querySelectorAll('.detailsBox span')[1].innerHTML).toBe(
        `+${relation.one_liners.main.one_liner.split('+')[1]}`
      );
    });
  });
  test('hide bonus text if operator inactive', async () => {
    props.item.relation.status = 'inactive';
    const { container } = render(<TemplateTwo {...props} />);

    await waitFor(() => {
      expect(container.querySelector('.casinoText')).toBeFalsy();
    });
  });
  test('show correct payment methods', async () => {
    // eslint-disable-next-line global-require
    const func = require('gatsby-core-theme/src/helpers/device-detect');
    func.isNativeImageLazyLoadingSupported = jest.fn(() => true);

    const { container } = render(<TemplateTwo {...props} />);
    const img = screen.getByAltText(relation.bonus.deposit_methods[0].name);

    await waitFor(() => {
      expect(container.querySelectorAll('.paymentMethods li').length).toBe(5);
      expect(container.querySelectorAll('.paymentMethods li img').length).toBe(4);
      expect(container.querySelectorAll('.paymentMethods li')[4].innerHTML).toBe(
        `+${relation.bonus.deposit_methods.length - 4}`
      );

      expect(img).toBeVisible();
      expect(img).toHaveAttribute('src', 'https://cdn.images.com/1680181972/visa.svg');
      expect(img).toHaveAttribute('width', '54');
      expect(img).toHaveAttribute('height', '36');
    });
  });
  test('if no deposit methods data, show nothing', async () => {
    props.item.relation.bonus.deposit_methods = null;
    const { container } = render(<TemplateTwo {...props} />);

    await waitFor(() => {
      expect(container.querySelector('.paymentMethods')).toBeFalsy();
    });
  });

  test('should render ribbons when showRibbon is true', () => {
    props.item.relation.ribbons = ['New'];
    const { container } = render(<TemplateTwo item={props.item} showRibbon />);
    const ribbonElement = container.querySelector('.ribbon');
    expect(ribbonElement).toBeInTheDocument();
    expect(ribbonElement.textContent).toBe('New');
  });
  test('should not render ribbons when showRibbon is false', () => {
    const { container } = render(<TemplateTwo item={props.item} />);
    const ribbonElement = container.querySelector('.ribbon');
    expect(ribbonElement).not.toBeInTheDocument();
  });
  test('should not render ribbons when ribbons array is empty', () => {
    props.item.relation.ribbons = [];
    const { container } = render(<TemplateTwo item={props.item} />);
    const ribbonElement = container.querySelector('.ribbon');
    expect(ribbonElement).not.toBeInTheDocument();
  });
});
afterEach(() => {
  cleanup();
});
