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

51 lines
1.8 KiB
TypeScript
Raw Normal View History

import { screen, waitFor } from '@testing-library/react';
import { Placement } from '@popperjs/core';
import { InfoTooltip, InfoTooltipProps } from '../../src/utils/InfoTooltip';
import { renderWithEvents } from '../__mocks__/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
});
});