Skip to content

Commit

Permalink
Add additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerJDev committed Dec 20, 2023
1 parent 5d7e0b1 commit 160e95d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions src/__tests__/focus-zone.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,72 @@ it('Should ignore hidden elements after focus zone is enabled', async () => {

controller.abort()
})

it('Should respect unhidden elements after focus zone is enabled', async () => {
const user = userEvent.setup()
const {container, rerender} = render(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0} hidden>
Banana
</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

const focusZoneContainer = container.querySelector<HTMLElement>('#focusZone')!
const [firstButton, secondButton, thirdButton] = focusZoneContainer.querySelectorAll('button')
const controller = focusZone(focusZoneContainer)

firstButton.focus()
expect(document.activeElement).toEqual(firstButton)

await user.keyboard('{arrowdown}')
expect(document.activeElement).toEqual(thirdButton)

rerender(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

await user.keyboard('{arrowup}')
expect(document.activeElement).toEqual(secondButton)

controller.abort()
})

it('Should ignore disabled elements after focus zone is enabled', async () => {
const user = userEvent.setup()
const {container, rerender} = render(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

const focusZoneContainer = container.querySelector<HTMLElement>('#focusZone')!
const [firstButton, , thirdButton] = focusZoneContainer.querySelectorAll('button')
const controller = focusZone(focusZoneContainer)

firstButton.focus()
expect(document.activeElement).toEqual(firstButton)

rerender(
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0} disabled>
Banana
</button>
<button tabIndex={0}>Cantaloupe</button>
</div>,
)

await user.keyboard('{arrowdown}')
expect(document.activeElement).toEqual(thirdButton)

controller.abort()
})
2 changes: 1 addition & 1 deletion src/focus-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export function focusZone(container: HTMLElement, settings?: FocusZoneSettings):
}
}

// Similarly, if an element is un-hidden or "enabled", add it to the list of focusable elements
// Similarly, if an element is unhidden or "enabled", add it to the list of focusable elements
// If `mutation.oldValue` is not null, then we may assume that the element was previously hidden or disabled
if (mutation.type === 'attributes' && mutation.oldValue !== null) {
if (mutation.target instanceof HTMLElement) {
Expand Down

0 comments on commit 160e95d

Please sign in to comment.