Skip to content

Commit

Permalink
Add enabled/disabled focus events tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jun 17, 2021
1 parent 7703ff9 commit f783ae2
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions src/hooks/use-focus-events.test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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 (
<>
<FocusNode focusId="nodeA" data-testid="nodeA" disabled={disableA} />
<FocusNode focusId="nodeB" data-testid="nodeB" />
</>
);
}

render(
<FocusRoot>
<TestComponent />
</FocusRoot>
);

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);
});
});

0 comments on commit f783ae2

Please sign in to comment.