import React from 'react';
import userEvent from '@testing-library/user-event';
import renderWithTheme from '../../../testUtils/renderWithTheme';
import Alert, { getAlertIcon } from '..';
describe('Alert', () => {
it('shows icon, title & content', () => {
const { getByText, getByTestId } = renderWithTheme(
);
expect(getByTestId('alert-icon')).toBeInTheDocument();
expect(getByText('A title')).toBeInTheDocument();
expect(getByText('A content')).toBeInTheDocument();
});
it('shows title & content only', () => {
const { getByText, queryByTestId } = renderWithTheme(
);
expect(queryByTestId('alert-icon')).not.toBeInTheDocument();
expect(getByText('A title')).toBeInTheDocument();
expect(getByText('A content')).toBeInTheDocument();
});
it('shows close icon button when onClose is defined', () => {
const onClose = jest.fn();
const { getByTestId } = renderWithTheme(
);
expect(getByTestId('close-button')).toBeInTheDocument();
userEvent.click(getByTestId('close-button'));
expect(onClose).toHaveBeenCalledTimes(1);
});
});
describe('getAlertIcon', () => {
it('returns defaultIcon when icon is not defined', () => {
expect(getAlertIcon('user', undefined)).toEqual({
tag: 'Some',
value: { left: 'user', tag: 'Left' },
});
});
it('returns icon when icon is one of hero icons', () => {
expect(getAlertIcon('user', 'add')).toEqual({
tag: 'Some',
value: { left: 'add', tag: 'Left' },
});
});
it('returns icon element when icon is a ReactElement', () => {
expect(getAlertIcon('user',
Icon
)).toEqual({
tag: 'Some',
value: { right: Icon
, tag: 'Right' },
});
});
it('returns none when icon is null', () => {
expect(getAlertIcon('user', null)).toEqual({
tag: 'None',
});
});
});