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

import OpenGraph from '.';

describe('OpenGraph Component', () => {
  test('Render opengraph component', () => {
    const { container, getByText } = render(
      <OpenGraph
        text="test title"
        page={{
          title: 'test',
          path: '/test/path',
        }}
      />
    );
    global.open = jest.fn();

    const fbURL = `https://www.facebook.com/sharer/sharer.php?caption=test&u=${getUrl(
      '/test/path'
    )}`;
    const twitterURL = `https://twitter.com/intent/tweet?text=test&url=${getUrl('/test/path')}`;

    expect(getByText('test title')).toBeTruthy();
    expect(container.querySelectorAll('svg')).toHaveLength(2);
    expect(container.querySelectorAll('a')[0]).toHaveAttribute('href', fbURL);
    expect(container.querySelectorAll('a')[1]).toHaveAttribute('href', twitterURL);

    const firstAhref = container.querySelectorAll('a')[0];
    const secondAhref = container.querySelectorAll('a')[1];
    // eslint-disable-next-line no-lone-blocks
    {
      firstAhref && fireEvent.click(firstAhref);
    }
    {
      secondAhref && fireEvent.click(secondAhref);
    }
    expect(global.open).toBeCalled();
  });

  test('Render component without Title', () => {
    const { getByText } = render(
      <OpenGraph
        page={{
          title: 'test2',
          path: '/test2/path',
        }}
      />
    );
    expect(getByText('Share:')).toBeTruthy();
  });
});
afterEach(() => {
  cleanup();
});
