diff --git a/src/hooks/use-focus-events.test.js b/src/hooks/use-focus-events.test.js index 0701d2c..be60286 100644 --- a/src/hooks/use-focus-events.test.js +++ b/src/hooks/use-focus-events.test.js @@ -1,6 +1,6 @@ -import React from 'react'; +import React, { useState } from 'react'; import '@testing-library/jest-dom'; -import { render, fireEvent, screen } from '@testing-library/react'; +import { render, fireEvent, act } from '@testing-library/react'; import { FocusRoot, FocusNode, @@ -9,7 +9,7 @@ import { } from '../index'; describe('useFocusEvents', () => { - describe('onBlur/onFocus', () => { + describe('focus/blur', () => { it('calls them when appropriate', () => { const nodeAOnFocused = jest.fn(); const nodeAOnBlurred = jest.fn(); @@ -68,4 +68,66 @@ describe('useFocusEvents', () => { expect(nodeBOnBlurred.mock.calls.length).toBe(0); }); }); + + it('disabled/enabled (enabled by default)', () => { + const nodeAOnDisabled = jest.fn(); + const nodeAOnEnabled = jest.fn(); + const nodeBOnDisabled = jest.fn(); + const nodeBOnEnabled = jest.fn(); + let focusStore; + let updateDisableA; + + function TestComponent() { + const [disableA, setDisableA] = useState(false); + focusStore = useFocusStoreDangerously(); + + updateDisableA = setDisableA; + + useFocusEvents('nodeA', { + disabled: nodeAOnDisabled, + enabled: nodeAOnEnabled, + }); + + useFocusEvents('nodeB', { + disabled: nodeBOnDisabled, + enabled: nodeBOnEnabled, + }); + + return ( + <> + + + + ); + } + + render( + + + + ); + + let focusState = focusStore.getState(); + expect(focusState.focusedNodeId).toEqual('nodeA'); + expect(focusState.focusHierarchy).toEqual(['root', 'nodeA']); + + expect(nodeAOnDisabled.mock.calls.length).toBe(0); + expect(nodeAOnEnabled.mock.calls.length).toBe(0); + expect(nodeBOnDisabled.mock.calls.length).toBe(0); + expect(nodeBOnEnabled.mock.calls.length).toBe(0); + + act(() => updateDisableA(true)); + + expect(nodeAOnDisabled.mock.calls.length).toBe(1); + expect(nodeAOnEnabled.mock.calls.length).toBe(0); + expect(nodeBOnDisabled.mock.calls.length).toBe(0); + expect(nodeBOnEnabled.mock.calls.length).toBe(0); + + act(() => updateDisableA(false)); + + expect(nodeAOnDisabled.mock.calls.length).toBe(1); + expect(nodeAOnEnabled.mock.calls.length).toBe(1); + expect(nodeBOnDisabled.mock.calls.length).toBe(0); + expect(nodeBOnEnabled.mock.calls.length).toBe(0); + }); });