import { render, screen } from '@testing-library/react';
import * as React from 'react';
import { Alert } from '../Alert';
import { ComponentClassNames, ComponentText } from '../../shared/constants';
describe('Alert:', () => {
it('can render Alert variations', async () => {
render(
Info
Error
Warning
Success
Default
);
const info = await screen.findByTestId('info');
const error = await screen.findByTestId('error');
const warning = await screen.findByTestId('warning');
const success = await screen.findByTestId('success');
const defaultAlert = await screen.findByTestId('default');
expect(info.dataset['variation']).toBe('info');
expect(info.classList).toContain(`${ComponentClassNames['Alert']}--info`);
expect(error.dataset['variation']).toBe('error');
expect(error.classList).toContain(`${ComponentClassNames['Alert']}--error`);
expect(warning.dataset['variation']).toBe('warning');
expect(warning.classList).toContain(
`${ComponentClassNames['Alert']}--warning`
);
expect(success.dataset['variation']).toBe('success');
expect(success.classList).toContain(
`${ComponentClassNames['Alert']}--success`
);
expect(defaultAlert.dataset['variation']).toBe(undefined);
});
it('can render a heading for the alert', async () => {
render(
Testing the alert heading
);
const alertHeading = await screen.findByText('Test heading');
expect(alertHeading.nodeName).toBe('DIV');
expect(
alertHeading.classList.contains(ComponentClassNames.AlertHeading)
).toBe(true);
expect(
alertHeading.parentElement?.parentElement?.classList.contains(
ComponentClassNames.Alert
)
).toBe(true);
});
it('can render an icon via the hasIcon prop', async () => {
render(
Has Icon
No Icon
Default Alert Without Icon
);
const hasIcon = await screen.findByTestId('hasIcon');
const noIcon = await screen.findByTestId('noIcon');
const defaultAlert = await screen.findByTestId('default');
expect(hasIcon.childElementCount).toBe(2);
expect(noIcon.childElementCount).toBe(1);
expect(defaultAlert.childElementCount).toBe(1);
expect(
hasIcon.firstElementChild?.classList.contains(ComponentClassNames.Icon)
).toBe(true);
expect(
noIcon.firstElementChild?.classList.contains(ComponentClassNames.Icon)
).toBe(false);
});
it('can be dismissible', async () => {
render(
Not dismissible by default
Is dismissible
);
const notDismissible = await screen.findByTestId('notDismissible');
const isDismissible = await screen.findByTestId('isDismissible');
expect(notDismissible.childElementCount).toBe(1);
expect(isDismissible.childElementCount).toBe(2);
});
it('should set aria-hidden to be true on decorative icons', () => {
const { container } = render(
);
const icons = container.querySelectorAll(`.${ComponentClassNames.Icon}`);
expect(icons.length).toEqual(2);
icons.forEach((icon) => {
expect(icon).toHaveAttribute('aria-hidden', 'true');
});
});
it('can configure an accessible label for the dismiss button', () => {
const customDismissButtonLabel = 'Testing 123';
render(
Default dismiss button label
Custom dismiss button label
);
const [defaultLabel, customLabel] = screen.queryAllByRole('button');
expect(defaultLabel.getAttribute('aria-label')).toBe(
ComponentText.Alert.dismissButtonLabel
);
expect(customLabel.getAttribute('aria-label')).toBe(
customDismissButtonLabel
);
});
it('renders as an aria alert', async () => {
render(Alert with an aria role);
const alert = await screen.findByRole('alert');
expect(alert).toBeDefined();
});
it('can allow role override', async () => {
render(
Alert with role overridden
);
const alert = await screen.findByTestId('noAlertRole');
expect(alert).toHaveAttribute('role', 'none');
});
it('can apply styling via props', async () => {
render(
Test alert
);
const alert = await screen.findByTestId('alertId');
expect(alert).toHaveStyle({
backgroundColor: 'var(--amplify-colors-white)',
fontStyle: 'italic',
});
});
it('can apply a custom className', async () => {
render();
const alert = await screen.findByTestId('alertId');
expect(alert.classList.contains('custom-alert')).toBe(true);
expect(alert.classList.contains(ComponentClassNames.Alert)).toBe(true);
});
it('can render any arbitrary data-* attribute', async () => {
render();
const alert = await screen.findByTestId('dataTest');
expect(alert.dataset['demo']).toBe('true');
});
describe('Forward ref:', () => {
it('should forward ref to container DOM element', async () => {
const testId = 'alert';
const ref = React.createRef();
render();
await screen.findByTestId(testId);
expect(ref.current?.nodeName).toBe('DIV');
});
it('should forward ref to dismiss button DOM element', async () => {
const ref = React.createRef();
render();
await screen.findByRole('button');
expect(ref.current?.nodeName).toBe('BUTTON');
});
});
});