/* eslint-disable no-lone-blocks */
/* eslint-disable no-unused-expressions */
import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import OperatorCtaButton from './operator-cta';

const data = (activeStatus = 'active') => ({
  operator: {
    status: activeStatus,
  },
  pageTemplate: 'default',
  buttonType: 'primary',
  buttonSize: 'm',
  tracker: 'main',
  translationsObj: {
    active: { translationKey: 'play_now', defaultValue: 'Visit' },
    not_recommended: { translationKey: 'not_recommended', defaultValue: 'Not Recommended' },
    coming_soon: { translationKey: 'coming_soon', defaultValue: 'Soon Available' },
    inactive: { translationKey: 'inactive', defaultValue: 'Not Accepting New Players' },
    blacklisted: { translationKey: 'blacklisted', defaultValue: 'Blacklisted' },
  },
});

describe('OperatorCtaButton Component', () => {
  test('OperatorCtaButton with status active', () => {
    const { getByText } = render(<OperatorCtaButton {...data()} />);
    expect(getByText('Visit')).toBeTruthy();
  });

  test('OperatorCtaButton with status inactive', () => {
    const { getByText } = render(<OperatorCtaButton {...data('inactive')} />);
    expect(getByText('Not Accepting New Players')).toBeTruthy();
  });

  test('OperatorCtaButton with status not_recommended', () => {
    const { getByText } = render(<OperatorCtaButton {...data('not_recommended')} />);
    expect(getByText('Not Recommended')).toBeTruthy();
  });

  test('OperatorCtaButton with status coming_soon', () => {
    const { getByText } = render(<OperatorCtaButton {...data('coming_soon')} />);
    expect(getByText('Soon Available')).toBeTruthy();
  });

  test('Affiliate object in cookie', () => {
    window = Object.create(window);
    const url = 'http://test.com/?fbclid=param1&msclkid=param2&gclid=param3';
    Object.defineProperty(window, 'location', {
      value: {
        href: url,
      },
      writable: true, // possibility to override
    });
    const { container } = render(
      <OperatorCtaButton {...data()} pageTemplate="test" module="test2" tracker="testing" />
    );
    const button = container.querySelector('a');
    fireEvent.click(button);
    const affObject = JSON.parse(document.cookie.split('=')[1]);
    expect(affObject.page_type).toEqual('test');
    expect(affObject.module).toEqual('test2');
    expect(affObject.request_url).toEqual('http://test.com/');
    // expect(affObject.facebook_click_id).toEqual('param1');
    expect(affObject.microsoft_click_id).toEqual('param2');
    expect(affObject.google_click_id).toEqual('param3');
    expect(affObject.tracker_name).toEqual('testing');
  });

  test('Default Affiliate object in cookie', () => {
    window = Object.create(window);
    const url = 'http://testdomain.com/';
    Object.defineProperty(window, 'location', {
      value: {
        href: url,
      },
      writable: true, // possibility to override
    });
    const { container } = render(<OperatorCtaButton {...data()} />);
    const button = container.querySelector('a');
    fireEvent.click(button);
    const affObject = JSON.parse(document.cookie.split('=')[1]);
    expect(affObject.page_type).toEqual('default');
    expect(affObject.request_url).toEqual('http://testdomain.com/');
  });
});

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