/* eslint-disable jsx-a11y/anchor-is-valid */
/* 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 Link from '.';

describe('Link Component', () => {
  test('render Link', () => {
    const { container, getAllByText, getByText } = render(
      <Link to="/test/link" className="testClassName">
        Test Content
      </Link>
    );
    expect(getAllByText('Test Content')).toBeTruthy();
    expect(getByText('Test Content').closest('a')).toHaveAttribute('href', '/test/link');
    expect(container.querySelectorAll('.testClassName')).toHaveLength(1);
  });

  test('path without slash', () => {
    const { getByText } = render(
      <Link to="test/link" className="testClassName">
        Test Content
      </Link>
    );
    expect(getByText('Test Content').closest('a')).toHaveAttribute('href', '/test/link');
  });

  test('test GTM class', () => {
    const { getByText } = render(
      <Link to="test/link" className="testClassName test-gtm">
        Test Content
      </Link>
    );
    expect(getByText('Test Content').closest('a').classList.contains('test-gtm')).toBe(true);
  });
  test('with window.location.pathname: /preview', () => {
    delete global.window.location;
    global.window ??= Object.create(window);
    global.window.location = {
      pathname: '/preview',
    };

    const { container } = render(
      <Link to="test/link">
        <span>Just Test</span>
      </Link>
    );

    expect(container.querySelectorAll('a')).toHaveLength(1);
    expect(container.querySelectorAll('a')[0]).toHaveAttribute('href', 'test/link');
    const ahref = container.querySelector('a');
    // eslint-disable-next-line no-lone-blocks
    {
      ahref && fireEvent.click(ahref);
    }
  });

  test('Without to false ', () => {
    const { getByText } = render(
      <Link to="">
        <span>Just Test</span>
      </Link>
    );
    expect(getByText('Just Test')).toBeTruthy();
  });

  test('Without to ', () => {
    const { getByText } = render(
      <Link to="">
        <span>Just Test 2</span>
      </Link>
    );
    expect(getByText('Just Test 2')).toBeTruthy();
  });
});
afterEach(() => {
  cleanup();
});
