From dfb7f8791cb9b978bd6df0880f1d4857442183e3 Mon Sep 17 00:00:00 2001 From: Cameron Dutro Date: Fri, 18 Oct 2024 11:04:34 -0700 Subject: [PATCH] Add test --- src/__tests__/focus-zone.test.tsx | 47 +++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/__tests__/focus-zone.test.tsx b/src/__tests__/focus-zone.test.tsx index 5ed4363..875f226 100644 --- a/src/__tests__/focus-zone.test.tsx +++ b/src/__tests__/focus-zone.test.tsx @@ -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' import userEvent from '@testing-library/user-event' async function nextTick() { @@ -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( +
+ + +
+ + + +
+
, + ) + + const focusZoneContainer = container.querySelector('#focusZone')! + const control = container.querySelector('#input')! + const [firstButton, secondButton] = focusZoneContainer.querySelectorAll('button') + const outsideButton = container.querySelector('#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( @@ -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(