shlink-web-client/test/servers/helpers/HighlightCard.test.tsx

53 lines
1.6 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
2023-02-18 12:40:37 +03:00
import type { ReactNode } from 'react';
import { MemoryRouter } from 'react-router-dom';
2023-02-18 12:40:37 +03:00
import type { HighlightCardProps } from '../../../src/servers/helpers/HighlightCard';
import { HighlightCard } from '../../../src/servers/helpers/HighlightCard';
describe('<HighlightCard />', () => {
const setUp = (props: HighlightCardProps & { children?: ReactNode }) => render(
<MemoryRouter>
<HighlightCard {...props} />
</MemoryRouter>,
);
it.each([
2022-03-26 14:17:42 +03:00
[undefined],
[false],
])('does not render icon when there is no link', (link) => {
setUp({ title: 'foo', link: link as undefined | false });
expect(screen.queryByRole('img', { hidden: true })).not.toBeInTheDocument();
expect(screen.queryByRole('link')).not.toBeInTheDocument();
});
it.each([
2022-03-26 14:17:42 +03:00
['foo'],
['bar'],
['baz'],
])('renders provided title', (title) => {
setUp({ title });
expect(screen.getByText(title)).toHaveAttribute('class', expect.stringContaining('highlight-card__title'));
});
it.each([
2022-03-26 14:17:42 +03:00
['foo'],
['bar'],
['baz'],
])('renders provided children', (children) => {
setUp({ title: 'title', children });
expect(screen.getByText(children)).toHaveAttribute('class', expect.stringContaining('card-text'));
});
it.each([
2022-03-26 14:17:42 +03:00
['foo'],
['bar'],
['baz'],
])('adds extra props when a link is provided', (link) => {
setUp({ title: 'title', link });
expect(screen.getByRole('img', { hidden: true })).toBeInTheDocument();
expect(screen.getByRole('link')).toHaveAttribute('href', `/${link}`);
});
});