2022-07-10 21:29:56 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
2020-12-21 11:57:46 +03:00
|
|
|
import { PropsWithChildren } from 'react';
|
2022-05-28 12:16:59 +03:00
|
|
|
import { Message, MessageProps } from '../../src/utils/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
|
|
|
});
|
|
|
|
});
|