shlink-web-client/test/utils/InfoTooltip.test.tsx

52 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-02-18 12:40:37 +03:00
import type { Placement } from '@popperjs/core';
2023-02-18 13:11:01 +03:00
import { screen, waitFor } from '@testing-library/react';
import type { InfoTooltipProps } from '../../shlink-web-component/src/utils/components/InfoTooltip';
import { InfoTooltip } from '../../shlink-web-component/src/utils/components/InfoTooltip';
2022-07-10 20:44:49 +03:00
import { renderWithEvents } from '../__helpers__/setUpTest';
2021-08-23 19:26:15 +03:00
describe('<InfoTooltip />', () => {
const setUp = (props: Partial<InfoTooltipProps> = {}) => renderWithEvents(
<InfoTooltip placement="right" {...props} />,
);
2021-08-23 19:26:15 +03:00
it.each([
2022-03-26 14:17:42 +03:00
[undefined],
['foo'],
['bar'],
2021-08-23 19:26:15 +03:00
])('renders expected className on span', (className) => {
const { container } = setUp({ className });
2021-08-23 19:26:15 +03:00
if (className) {
expect(container.firstChild).toHaveClass(className);
} else {
expect(container.firstChild).toHaveAttribute('class', '');
}
2021-08-23 19:26:15 +03:00
});
it.each([
[<span key={1}>foo</span>, 'foo'],
['Foo', 'Foo'],
['Hello', 'Hello'],
[['One', 'Two', <span key={3} />], 'OneTwo'],
])('passes children down to the nested tooltip component', async (children, expectedContent) => {
const { container, user } = setUp({ children });
2021-08-23 19:26:15 +03:00
container.firstElementChild && await user.hover(container.firstElementChild);
await waitFor(() => expect(screen.getByRole('tooltip')).toBeInTheDocument());
expect(screen.getByRole('tooltip')).toHaveTextContent(expectedContent);
2021-08-23 19:26:15 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
['right' as Placement],
['left' as Placement],
['top' as Placement],
['bottom' as Placement],
])('places tooltip where requested', async (placement) => {
const { container, user } = setUp({ placement });
2021-08-23 19:26:15 +03:00
container.firstElementChild && await user.hover(container.firstElementChild);
await waitFor(() => expect(screen.getByRole('tooltip')).toBeInTheDocument());
expect(screen.getByRole('tooltip').parentNode).toHaveAttribute('data-popper-placement', placement);
2021-08-23 19:26:15 +03:00
});
});