Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Oct 18, 2024
1 parent bc887b6 commit dfb7f87
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/__tests__/focus-zone.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {FocusKeys, FocusZoneSettings, focusZone} from '../focus-zone.js'
import {
FocusKeys,
FocusZoneSettings,
activeDescendantActivatedDirectly,
focusZone,
isActiveDescendantAttribute,
} from '../focus-zone.js'
import {fireEvent, render} from '@testing-library/react'
import React from 'react'
import React, {act} from 'react'

Check failure on line 9 in src/__tests__/focus-zone.test.tsx

View workflow job for this annotation

GitHub Actions / lint

'act' is defined but never used
import userEvent from '@testing-library/user-event'

async function nextTick() {
Expand Down Expand Up @@ -431,6 +437,41 @@ it('Should focus-in to the first element if the last-focused element is removed'
controller.abort()
})

it('Should clear all active descendants when focus moves outside the zone', async () => {
const {container} = render(
<div>
<button tabIndex={0} id="outside">
Bad Apple
</button>
<input type="text" id="input" tabIndex={0} />
<div id="focusZone">
<button tabIndex={0}>Apple</button>
<button tabIndex={0}>Banana</button>
<button tabIndex={0}>Cantaloupe</button>
</div>
</div>,
)

const focusZoneContainer = container.querySelector<HTMLElement>('#focusZone')!
const control = container.querySelector<HTMLInputElement>('#input')!
const [firstButton, secondButton] = focusZoneContainer.querySelectorAll('button')
const outsideButton = container.querySelector<HTMLElement>('#outside')!
const controller = focusZone(focusZoneContainer, {activeDescendantControl: control})

control.focus()
const allActiveDescendants = focusZoneContainer.querySelectorAll(`[${isActiveDescendantAttribute}]`)
expect(allActiveDescendants.length).toEqual(1)
expect(allActiveDescendants[0]).toEqual(firstButton)

secondButton.setAttribute(isActiveDescendantAttribute, activeDescendantActivatedDirectly)
expect(focusZoneContainer.querySelectorAll(`[${isActiveDescendantAttribute}]`).length).toEqual(2)

outsideButton.focus()
expect(focusZoneContainer.querySelectorAll(`[${isActiveDescendantAttribute}]`).length).toEqual(0)

controller.abort()
})

it('Should call onActiveDescendantChanged properly', async () => {
const user = userEvent.setup()
const {container} = render(
Expand Down Expand Up @@ -625,7 +666,7 @@ it('Should ignore hidden elements if strict', async () => {
controller.abort()
})

it('Shoud move to tabbable elements if onlyTabbable', async () => {
it('Should move to tabbable elements if onlyTabbable', async () => {
const user = userEvent.setup()
const {container} = render(
<div id="focusZone">
Expand Down

0 comments on commit dfb7f87

Please sign in to comment.