Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@mantine/core] Add tests for <FocusTrap /> using shift + tab #4969

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/mantine-core/src/components/FocusTrap/FocusTrap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,71 @@ describe('@mantine/core/FocusTrap', () => {
expect(document.body).toHaveFocus();
});

it('traps focus on shift + tab', async () => {
render(
<FocusTrap>
<div>
<input />
<button type="button">Button</button>
<input type="radio" />
</div>
</FocusTrap>
);

await wait(10);
expect(screen.getByRole('textbox')).toHaveFocus();

userEvent.tab();
expect(screen.getByRole('button')).toHaveFocus();

userEvent.tab({ shift: true });
await wait(10);
expect(screen.getByRole('textbox')).toHaveFocus();

userEvent.tab({ shift: true });
await wait(10);
expect(screen.getByRole('radio')).toHaveFocus();
});

it('traps focus on shift + tab and handles Radio Groups', async () => {
render(
<FocusTrap>
<div>
<label htmlFor="1">
Option One
<input type="radio" name="group" id="1" />
</label>

<label htmlFor="2">
Option Two
<input type="radio" name="group" id="2" />
</label>

<label htmlFor="3">
Option Three
<input type="radio" name="group" id="3" />
</label>

<button type="button">Button</button>
</div>
</FocusTrap>
);

await wait(10);
expect(screen.getByLabelText('Option One')).toHaveFocus();

userEvent.tab();
expect(screen.getByRole('button')).toHaveFocus();

userEvent.tab({ shift: true });
await wait(10);
expect(screen.getByLabelText('Option Three')).toHaveFocus();

userEvent.tab({ shift: true });
await wait(10);
expect(screen.getByRole('button')).toHaveFocus();
});

it('manages aria-hidden attributes', () => {
const adjacentDiv = document.createElement('div');
adjacentDiv.setAttribute('data-testid', 'adjacent');
Expand Down