import * as React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import type { FunctionComponent } from 'react';
import { LinkHandler } from './LinkHandler';
const TestComponent: FunctionComponent<{ url: string; attributes?: object; children?: React.ReactNode }> = ({
url,
attributes,
children,
}) => {
return (
// eslint-disable-next-line react/jsx-props-no-spreading
{children}
);
};
describe('LinkHandler', () => {
const linkUrl = 'https://example.com';
const linkText = 'CLICK ME';
it('should render a link', async () => {
const { queryByText } = render({linkText});
expect((await queryByText(linkText))?.closest('a')).toBeTruthy();
});
it('should render a link containing the url', async () => {
const { queryByText } = render({linkText});
expect((await queryByText(linkText))?.closest('a')).toHaveAttribute('href', linkUrl);
});
it('should render a disabled link', async () => {
const { queryByText } = render(
{linkText}
,
);
expect((await queryByText(linkText))?.closest('a')).toHaveAttribute('disabled');
});
it('should render nothing if "hidden" is true', async () => {
const { queryByText } = render(
{linkText}
,
);
expect(await queryByText(linkText)).toBeNull();
});
it('should correctly set the action handler', async () => {
const spy = jest.fn();
const { queryByText } = render(
{linkText}
,
);
await queryByText(linkText)?.click();
expect(spy).toBeCalledTimes(1);
});
});