-
Notifications
You must be signed in to change notification settings - Fork 598
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
fix(focusZone): handle complex child reordering #1225
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/components": patch | ||
--- | ||
|
||
handle complex child reordering within a focusZone |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ async function nextTick() { | |
return new Promise(resolve => setTimeout(resolve, 0)) | ||
} | ||
|
||
const moveDown = () => userEvent.type(document.activeElement!, '{arrowdown}') | ||
const moveUp = () => userEvent.type(document.activeElement!, '{arrowup}') | ||
|
||
// Since we use strict `isTabbable` checks within focus trap, we need to mock these | ||
// properties that Jest does not populate. | ||
beforeAll(() => { | ||
|
@@ -472,3 +475,49 @@ it('Should set aria-activedescendant correctly', () => { | |
|
||
controller.abort() | ||
}) | ||
|
||
it('Should handle elements being reordered', async () => { | ||
const {container} = render( | ||
<div> | ||
<div id="focusZone"> | ||
<button tabIndex={0}>Apple</button> | ||
<button tabIndex={0}>Banana</button> | ||
<button tabIndex={0}>Cantaloupe</button> | ||
<button tabIndex={0}>Durian</button> | ||
</div> | ||
</div> | ||
) | ||
|
||
const focusZoneContainer = container.querySelector<HTMLElement>('#focusZone')! | ||
const [firstButton, secondButton, thirdButton, fourthButton] = focusZoneContainer.querySelectorAll('button') | ||
const controller = focusZone(focusZoneContainer) | ||
|
||
firstButton.focus() | ||
expect(document.activeElement).toEqual(firstButton) | ||
|
||
moveDown() | ||
expect(document.activeElement).toEqual(secondButton) | ||
|
||
moveUp() | ||
expect(document.activeElement).toEqual(firstButton) | ||
|
||
// move secondButton and thirdButton to the end of the zone, in reverse order | ||
focusZoneContainer.appendChild(thirdButton) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL (from
|
||
focusZoneContainer.appendChild(secondButton) | ||
|
||
// The mutation observer fires asynchronously | ||
await nextTick() | ||
|
||
expect(document.activeElement).toEqual(firstButton) | ||
|
||
moveDown() | ||
expect(document.activeElement).toEqual(fourthButton) | ||
|
||
moveDown() | ||
expect(document.activeElement).toEqual(thirdButton) | ||
|
||
moveDown() | ||
expect(document.activeElement).toEqual(secondButton) | ||
|
||
controller.abort() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified the magic string
'{arrowdown}'
: https://github.com/testing-library/user-event#special-characters 👍