Skip to content

Commit

Permalink
[refactor] Add use-focus-trap tests with Shift + Tab (#4969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon-Dickson authored Oct 12, 2023
1 parent 9806bfc commit 203b602
Showing 1 changed file with 65 additions and 0 deletions.
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

0 comments on commit 203b602

Please sign in to comment.