docbrown/app/javascript/crayons/MobileDrawer/__tests__/MobileDrawer.test.jsx
depfu[bot] 34cd4e3675
[js] Update all Yarn dependencies (2022-08-17) (#18343)
Co-authored-by: depfu[bot] <23717796+depfu[bot]@users.noreply.github.com>
Co-authored-by: Mac Siri <krairit.siri@gmail.com>
2022-08-18 12:28:43 -04:00

89 lines
2.3 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 { MobileDrawer } from '../MobileDrawer';
describe('<MobileDrawer />', () => {
let user;
beforeEach(() => {
user = userEvent.setup();
});
it('should have no a11y violations', async () => {
const { container } = render(
<MobileDrawer title="Example MobileDrawer">
<button>Click me</button>
</MobileDrawer>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('should render correctly', () => {
const { container } = render(
<MobileDrawer title="Example MobileDrawer">
<button>Click me</button>
</MobileDrawer>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('should trap focus inside the drawer by default', async () => {
const { getByRole } = render(
<div>
<button>Outside drawer button</button>
<MobileDrawer title="Example MobileDrawer">
<button>Inside drawer button</button>
<button>Inside drawer button 2</button>
</MobileDrawer>
</div>,
);
const innerDrawerButton = getByRole('button', {
name: 'Inside drawer button',
});
await waitFor(() => expect(innerDrawerButton).toHaveFocus());
await user.keyboard('{Tab}');
expect(
getByRole('button', { name: 'Inside drawer button 2' }),
).toHaveFocus();
await user.keyboard('{Tab}');
expect(innerDrawerButton).toHaveFocus();
});
it('should close when Escape is pressed', async () => {
const onClose = jest.fn();
render(
<MobileDrawer title="Example MobileDrawer" onClose={onClose}>
<button>Inner button</button>
</MobileDrawer>,
);
await user.keyboard('{Escape}');
expect(onClose).toHaveBeenCalled();
});
it('should close on click outside', async () => {
const onClose = jest.fn();
const { getByText } = render(
<div>
<p>Outside content</p>
<MobileDrawer title="Example MobileDrawer" onClose={onClose}>
<button>Inner button</button>
</MobileDrawer>
,
</div>,
);
await user.click(getByText('Outside content'));
expect(onClose).toHaveBeenCalled();
});
});