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

47 lines
1.7 KiB
TypeScript
Raw Normal View History

import { screen } from '@testing-library/react';
2020-12-25 12:39:54 +03:00
import { PropsWithChildren } from 'react';
import { DropdownBtn, DropdownBtnProps } from '../../src/utils/DropdownBtn';
import { renderWithEvents } from '../__helpers__/setUpTest';
2020-12-25 12:39:54 +03:00
describe('<DropdownBtn />', () => {
const setUp = (props: PropsWithChildren<DropdownBtnProps>) => renderWithEvents(
<DropdownBtn children="foo" {...props} />,
);
2020-12-25 12:39:54 +03:00
it.each([['foo'], ['bar'], ['baz']])('displays provided text in button', (text) => {
setUp({ text });
expect(screen.getByRole('button')).toHaveTextContent(text);
2020-12-25 12:39:54 +03:00
});
it.each([['foo'], ['bar'], ['baz']])('displays provided children in menu', async (children) => {
const { user } = setUp({ text: '', children });
2020-12-25 12:39:54 +03:00
await user.click(screen.getByRole('button'));
expect(screen.getByRole('menu')).toHaveTextContent(children);
2020-12-25 12:39:54 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[undefined, 'dropdown-btn__toggle btn-block'],
['', 'dropdown-btn__toggle btn-block'],
['foo', 'dropdown-btn__toggle btn-block foo'],
['bar', 'dropdown-btn__toggle btn-block bar'],
2020-12-25 12:39:54 +03:00
])('includes provided classes', (className, expectedClasses) => {
setUp({ text: '', className });
expect(screen.getByRole('button')).toHaveClass(expectedClasses);
2020-12-25 12:39:54 +03:00
});
2021-06-22 22:12:06 +03:00
it.each([
[100, 'min-width: 100px; '],
[250, 'min-width: 250px; '],
[undefined, ''],
])('renders proper styles when minWidth is provided', async (minWidth, expectedStyle) => {
const { user } = setUp({ text: '', minWidth });
await user.click(screen.getByRole('button'));
expect(screen.getByRole('menu')).toHaveAttribute(
'style',
2022-10-05 00:17:12 +03:00
`${expectedStyle}position: absolute; left: 0px; top: 0px; transform: translate(0px, 0px);`,
);
2021-06-22 22:12:06 +03:00
});
2020-12-25 12:39:54 +03:00
});