docbrown/app/javascript/shared/components/__tests__/focusTrap.test.jsx
2023-10-19 12:59:54 -04:00

30 lines
810 B
JavaScript

import { h } from 'preact';
import { render, waitFor } from '@testing-library/preact';
import '@testing-library/jest-dom';
import { userEvent } from '@testing-library/user-event';
import { FocusTrap } from '../focusTrap';
describe('<FocusTrap />', () => {
it('should trap focus within crayon-modals', async () => {
const { getByTestId } = render(
<div>
<input type="text " />
<FocusTrap>
<div class="crayons-modal">
<input type="text" data-testid="firstInput" />
<input type="text" />
</div>
</FocusTrap>
</div>,
);
const firstInput = getByTestId('firstInput');
await waitFor(() => expect(firstInput).toHaveFocus());
userEvent.tab();
userEvent.tab();
expect(firstInput).toHaveFocus();
});
});