2022-07-10 21:29:56 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { PropsWithChildren } from 'react';
|
2023-07-31 22:36:44 +03:00
|
|
|
import type { MessageProps } from '../../shlink-frontend-kit/src/block/Message';
|
|
|
|
import { Message } from '../../shlink-frontend-kit/src/block/Message';
|
2020-12-21 11:57:46 +03:00
|
|
|
|
|
|
|
describe('<Message />', () => {
|
2022-07-10 21:29:56 +03:00
|
|
|
const setUp = (props: PropsWithChildren<MessageProps> = {}) => render(<Message {...props} />);
|
2020-12-21 11:57:46 +03:00
|
|
|
|
|
|
|
it.each([
|
2022-07-10 21:29:56 +03:00
|
|
|
[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) => {
|
2022-07-10 21:29:56 +03:00
|
|
|
setUp({ loading, children });
|
2020-12-21 11:57:46 +03:00
|
|
|
|
2022-07-10 21:29:56 +03:00
|
|
|
expect(screen.queryAllByRole('img', { hidden: true })).toHaveLength(loading ? 1 : 0);
|
2020-12-21 11:57:46 +03:00
|
|
|
|
|
|
|
if (loading) {
|
2022-07-10 21:29:56 +03:00
|
|
|
expect(screen.getByText(children || 'Loading...')).toHaveClass('ms-2');
|
2020-12-21 11:57:46 +03:00
|
|
|
} else {
|
2022-07-10 21:29:56 +03:00
|
|
|
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) => {
|
2022-07-10 21:29:56 +03:00
|
|
|
const { container } = setUp({ type: type as 'default' | 'error' | undefined });
|
2020-12-21 11:57:46 +03:00
|
|
|
|
2022-07-10 21:29:56 +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 }) => {
|
2022-07-10 21:29:56 +03:00
|
|
|
const { container } = setUp({ className });
|
|
|
|
expect(container.firstChild).toHaveClass(`g-0${className ? ` ${className}` : ''}`);
|
2020-12-21 11:57:46 +03:00
|
|
|
});
|
|
|
|
});
|