docbrown/app/javascript/crayons/Modal/__tests__/Modal.test.jsx
dependabot[bot] 3f3b13f382
Bump @testing-library/user-event from 13.5.0 to 14.1.0 (#17253)
* Bump @testing-library/user-event from 13.5.0 to 14.1.0

Bumps [@testing-library/user-event](https://github.com/testing-library/user-event) from 13.5.0 to 14.1.0.
- [Release notes](https://github.com/testing-library/user-event/releases)
- [Changelog](https://github.com/testing-library/user-event/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/user-event/compare/v13.5.0...v14.1)

---
updated-dependencies:
- dependency-name: "@testing-library/user-event"
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* refactor to account for breaking changes

* remove redundant cleanup

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
2022-04-13 10:41:08 -06:00

157 lines
4.2 KiB
JavaScript

import { h } from 'preact';
import { axe } from 'jest-axe';
import '@testing-library/jest-dom';
import { render, waitFor } from '@testing-library/preact';
import userEvent from '@testing-library/user-event';
import { Modal } from '../Modal';
it('should have no a11y violations', async () => {
const { container } = render(
<Modal title="This is a modal title">This is the modal body content</Modal>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('should trap focus inside the modal by default', async () => {
const { getByText, getByLabelText } = render(
<div>
<button>Outside modal button</button>
<Modal title="This is a modal title">
<button>Modal content button</button>
</Modal>
</div>,
);
const closeButton = getByLabelText('Close', { selector: 'button' });
await waitFor(() => expect(closeButton).toHaveFocus());
userEvent.tab();
expect(getByText('Modal content button')).toHaveFocus();
userEvent.tab();
expect(closeButton).toHaveFocus();
});
it('should trap focus in the custom selector if provided in props', async () => {
const { getByText } = render(
<div>
<button>Outside modal button</button>
<Modal title="This is a modal title" focusTrapSelector="#trap-focus-here">
<button>Outside focus trap button</button>
<div id="trap-focus-here">
<button>Inside focus trap button</button>
</div>
</Modal>
</div>,
);
const buttonInsideFocusTrap = getByText('Inside focus trap button');
await waitFor(() => expect(buttonInsideFocusTrap).toHaveFocus());
});
it('should close when the close button is clicked', async () => {
const onClose = jest.fn();
const { getByLabelText } = render(
<Modal title="This is a modal title" onClose={onClose}>
This is the modal body content
</Modal>,
);
const closeButton = getByLabelText('Close', { selector: 'button' });
closeButton.click();
expect(onClose).toHaveBeenCalledTimes(1);
});
it('should close when Escape is pressed', () => {
const onClose = jest.fn();
render(
<Modal title="This is a modal title" onClose={onClose}>
This is the modal body content
</Modal>,
);
userEvent.keyboard('{Escape}');
expect(onClose).toHaveBeenCalledTimes(1);
});
it("shouldn't close on outside click by default", async () => {
const onClose = jest.fn();
const { getByText } = render(
<div>
<p>Outside content</p>
<Modal title="This is a modal title" onClose={onClose}>
This is the modal body content
</Modal>
</div>,
);
await userEvent.click(getByText('Outside content'));
expect(onClose).not.toHaveBeenCalled();
});
it('should close on click outside, if enabled', async () => {
const onClose = jest.fn();
const { getByText } = render(
<div>
<p>Outside content</p>
<Modal
title="This is a modal title"
onClose={onClose}
backdropDismissible
>
This is the modal body content
</Modal>
</div>,
);
await userEvent.click(getByText('Outside content'));
expect(onClose).toHaveBeenCalledTimes(1);
});
it('should render with additional class names', async () => {
const { getByTestId } = render(
<Modal
title="This is a modal title"
className="some-additional-class-name"
onClose={jest.fn()}
>
This is the modal body content
</Modal>,
);
const modalContainer = getByTestId('modal-container');
expect(
modalContainer.classList.contains('some-additional-class-name'),
).toEqual(true);
});
it('should render with a backdrop', async () => {
const { getByTestId } = render(
<Modal title="This is a modal title" backdrop onClose={jest.fn()}>
This is the modal body content
</Modal>,
);
const modalOverlay = getByTestId('modal-overlay');
expect(modalOverlay).not.toBeNull();
});
it('should render with a different size modal', async () => {
const { getByTestId } = render(
<Modal title="This is a modal title" size="large" onClose={jest.fn()}>
This is the modal body content
</Modal>,
);
const modalContainer = getByTestId('modal-container');
expect(modalContainer.classList.contains('crayons-modal--large')).toEqual(
true,
);
});