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

50 lines
1.9 KiB
TypeScript
Raw Normal View History

import { render, screen } from '@testing-library/react';
2020-12-21 11:57:46 +03:00
import { PropsWithChildren } from 'react';
import { Message, MessageProps } from '../../src/utils/Message';
2020-12-21 11:57:46 +03:00
describe('<Message />', () => {
const setUp = (props: PropsWithChildren<MessageProps> = {}) => render(<Message {...props} />);
2020-12-21 11:57:46 +03:00
it.each([
[true, 'col-md-12'],
[false, 'col-md-10 offset-md-1'],
[undefined, 'col-md-10 offset-md-1'],
])('renders expected classes based on width', (fullWidth, expectedClass) => {
const { container } = setUp({ fullWidth });
expect(container.firstChild?.firstChild).toHaveClass(expectedClass);
2020-12-21 11:57:46 +03:00
});
it.each([
2022-03-26 14:17:42 +03:00
[true, 'These are the children contents'],
[false, 'These are the children contents'],
[true, undefined],
[false, undefined],
2020-12-21 11:57:46 +03:00
])('renders expected content', (loading, children) => {
setUp({ loading, children });
2020-12-21 11:57:46 +03:00
expect(screen.queryAllByRole('img', { hidden: true })).toHaveLength(loading ? 1 : 0);
2020-12-21 11:57:46 +03:00
if (loading) {
expect(screen.getByText(children || 'Loading...')).toHaveClass('ms-2');
2020-12-21 11:57:46 +03:00
} else {
expect(screen.getByRole('heading')).toHaveTextContent(children || '');
2020-12-21 11:57:46 +03:00
}
});
it.each([
2022-03-26 14:17:42 +03:00
['error', 'border-danger', 'text-danger'],
['default', '', 'text-muted'],
[undefined, '', 'text-muted'],
2020-12-21 11:57:46 +03:00
])('renders proper classes based on message type', (type, expectedCardClass, expectedH3Class) => {
const { container } = setUp({ type: type as 'default' | 'error' | undefined });
2020-12-21 11:57:46 +03:00
expect(container.querySelector('.card-body')).toHaveAttribute('class', expect.stringContaining(expectedCardClass));
expect(screen.getByRole('heading')).toHaveClass(`text-center mb-0 ${expectedH3Class}`);
2020-12-21 11:57:46 +03:00
});
it.each([{ className: 'foo' }, { className: 'bar' }, {}])('renders provided classes', ({ className }) => {
const { container } = setUp({ className });
expect(container.firstChild).toHaveClass(`g-0${className ? ` ${className}` : ''}`);
2020-12-21 11:57:46 +03:00
});
});