Skip to content

Commit

Permalink
Continue writing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jun 17, 2021
1 parent 5230734 commit fa0ce73
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/focus-node.tsx
Expand Up @@ -177,7 +177,8 @@ export function FocusNode(
);

const [nodeId] = useState(() => {
const nonStringFocusId = typeof focusId !== 'string';
const nonStringFocusId =
typeof focusId !== 'string' && focusId !== undefined;
const reservedFocusId = focusId === 'root';
const invalidNodeId = nonStringFocusId || reservedFocusId;

Expand Down
1 change: 0 additions & 1 deletion src/hooks/internal/use-on-change.ts
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import usePrevious from './use-previous';
import { warning } from '../../utils/warning';

type ComparatorFn<Value> = (a: Value, b: Value | undefined) => boolean;

Expand Down
28 changes: 28 additions & 0 deletions src/tests/focus-trap.test.js
@@ -0,0 +1,28 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render, fireEvent, screen } from '@testing-library/react';
import { FocusRoot, FocusNode, useFocusStoreDangerously } from '../index';
import { warning } from '../utils/warning';

describe('Focus Traps', () => {
it('warns when restoreTrapFocusHierarchy is passed to a non-trap', () => {
function TestComponent() {
return (
<FocusNode restoreTrapFocusHierarchy>
<FocusNode>
<FocusNode />
</FocusNode>
</FocusNode>
);
}

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

expect(warning).toHaveBeenCalledTimes(1);
expect(warning.mock.calls[0][1]).toEqual('RESTORE_TRAP_FOCUS_WITHOUT_TRAP');
});
});
21 changes: 21 additions & 0 deletions src/tests/grids.test.js
Expand Up @@ -30,6 +30,27 @@ describe('Grids', () => {
expect(warning.mock.calls[0][1]).toEqual('ORIENTATION_ON_GRID');
});

it('warns when onGridMove is passed to a non-grid', () => {
function TestComponent() {
return (
<FocusNode onGridMove={() => {}}>
<FocusNode>
<FocusNode />
</FocusNode>
</FocusNode>
);
}

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

expect(warning).toHaveBeenCalledTimes(1);
expect(warning.mock.calls[0][1]).toEqual('GRID_MOVE_NOT_ON_GRID');
});

it('warns when onMove is passed', () => {
function TestComponent() {
return (
Expand Down

0 comments on commit fa0ce73

Please sign in to comment.