import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Link } from './';
describe('Link component', () => {
it('should render a link', () => {
render(Click Me);
expect(screen.getByRole('link', { name: 'Click Me' })).toBeVisible();
});
it('should call onClick when given onClick props', async () => {
const handleClick = vi.fn();
render(
Click Me
,
);
await userEvent.click(screen.getByRole('link', { name: 'Click Me' }));
expect(handleClick).toHaveBeenCalledTimes(1);
});
it('should attach the right attribut when isExternal is provided', () => {
render(
Need help ?
,
);
const link = screen.getByRole('link', { name: 'Need help ?' });
expect(link).toHaveAttribute('target', '_blank');
expect(link).toHaveAttribute('rel', 'noopener noreferrer');
});
it('should support custom component through `as` props', () => {
const NavLink = ({ to, className }: { to: string; className: string }) => {
return (
I am a NavLink
);
};
const NextLink = ({
href,
className,
}: {
href: string;
className: string;
}) => {
return (
I am a NextLink
);
};
render();
const navEl = screen.getByRole('link', { name: 'I am a NavLink' });
expect(navEl).toBeVisible();
expect(navEl).toHaveAttribute('href', 'https://spendesk.com');
render(
,
);
const nextEl = screen.getByRole('link', { name: 'I am a NextLink' });
expect(nextEl).toBeVisible();
expect(nextEl).toHaveAttribute('href', 'https://spendesk.com');
});
});