Skip to content

Commit

Permalink
Begin testing warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jun 17, 2021
1 parent a1c4bd7 commit 6d52eec
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 18 deletions.
21 changes: 21 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
import '@testing-library/jest-dom';
import * as warning from './src/utils/warning';

beforeEach(() => {
if (console.error.mockRestore) {
console.error.mockRestore();
}

if (console.warn.mockRestore) {
console.warn.mockRestore();
}

if (warning.warning.mockRestore) {
warning.warning.mockRestore();
}

jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(warning, 'warning').mockImplementation(() => {});

warning.resetCodeCache();
});
2 changes: 1 addition & 1 deletion src/focus-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef } from 'react';
import createFocusStore from './focus-store';
import lrudInput from './lrud-input/focus-lrud';
import { ProviderValue, RootFocusNode, Orientation } from './types';
import warning from './utils/warning';
import { warning } from './utils/warning';

const FocusContext = React.createContext<null | ProviderValue>(null);

Expand Down
2 changes: 1 addition & 1 deletion src/focus-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, {
} from 'react';
import FocusContext from './focus-context';
import nodeFromDefinition from './utils/node-from-definition';
import warning from './utils/warning';
import { warning } from './utils/warning';
import {
FocusStore,
Id,
Expand Down
2 changes: 1 addition & 1 deletion src/focus-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import updateFocus from './update-focus/update-focus';
import handleArrowUtil from './handle-arrow/handle-arrow';
import enforceStateStructure from './utils/enforce-state-structure';
import recursivelyUpdateChildren from './utils/recursively-update-node';
import warning from './utils/warning';
import { warning } from './utils/warning';
import {
FocusState,
Orientation,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/use-on-change.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react';
import usePrevious from './use-previous';
import warning from '../../utils/warning';
import { warning } from '../../utils/warning';

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

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-active-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext, useState, useEffect, useRef } from 'react';
import FocusContext from '../focus-context';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { Node } from '../types';

export default function useActiveNode(): Node | null {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-focus-events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRef, useState } from 'react';
import useOnChange from './internal/use-on-change';
import useFocusNode from './use-focus-node';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { Id, Node } from '../types';

type EventCallback = (node: Node) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-focus-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext, useEffect, useState, useRef } from 'react';
import FocusContext from '../focus-context';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { Node, NodeHierarchy } from '../types';

function hierarchiesAreEqual(
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-focus-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext, useState, useEffect, useRef } from 'react';
import FocusContext from '../focus-context';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { Id, Node } from '../types';

export default function useFocusNode(focusId: Id): Node | null {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-focus-store-dangerously.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from 'react';
import FocusContext from '../focus-context';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { FocusStore } from '../types';

export default function useFocusStoreDangerously(): FocusStore {
Expand Down
61 changes: 60 additions & 1 deletion src/hooks/use-set-focus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useSetFocus,
useFocusStoreDangerously,
} from '../index';
import { warning } from '../utils/warning';

describe('useSetFocus', () => {
it('is a noop when the node is already focused', () => {
Expand Down Expand Up @@ -228,7 +229,6 @@ describe('useSetFocus', () => {
expect(Object.values(focusState.nodes)).toHaveLength(3);
});

// TODO: add check for warning
it('it is a noop if the node does not exist', () => {
let focusStore;
let setFocus;
Expand Down Expand Up @@ -280,5 +280,64 @@ describe('useSetFocus', () => {
expect(focusState.focusHierarchy).toEqual(['root', 'nodeA']);
expect(focusState.activeNodeId).toEqual(null);
expect(Object.values(focusState.nodes)).toHaveLength(3);

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

it('it is a noop with a nonsense argument', () => {
let focusStore;
let setFocus;

function TestComponent() {
focusStore = useFocusStoreDangerously();
setFocus = useSetFocus();

return (
<>
<FocusNode focusId="nodeA" data-testid="nodeA" />
<FocusNode focusId="nodeB" data-testid="nodeB" />
</>
);
}

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

let nodeA = screen.getByTestId('nodeA');
expect(nodeA).toHaveClass('isFocused');
expect(nodeA).toHaveClass('isFocusedLeaf');

let nodeB = screen.getByTestId('nodeB');
expect(nodeB).not.toHaveClass('isFocused');
expect(nodeB).not.toHaveClass('isFocusedLeaf');

let focusState = focusStore.getState();
expect(focusState.focusedNodeId).toEqual('nodeA');
expect(focusState.focusHierarchy).toEqual(['root', 'nodeA']);
expect(focusState.activeNodeId).toEqual(null);
expect(Object.values(focusState.nodes)).toHaveLength(3);

act(() => setFocus({}));

nodeA = screen.getByTestId('nodeA');
expect(nodeA).toHaveClass('isFocused');
expect(nodeA).toHaveClass('isFocusedLeaf');

nodeB = screen.getByTestId('nodeB');
expect(nodeB).not.toHaveClass('isFocused');
expect(nodeB).not.toHaveClass('isFocusedLeaf');

focusState = focusStore.getState();
expect(focusState.focusedNodeId).toEqual('nodeA');
expect(focusState.focusHierarchy).toEqual(['root', 'nodeA']);
expect(focusState.activeNodeId).toEqual(null);
expect(Object.values(focusState.nodes)).toHaveLength(3);

expect(warning).toHaveBeenCalledTimes(1);
expect(warning.mock.calls[0][1]).toEqual('NODE_ID_NOT_STRING_TO_SET_FOCUS');
});
});
2 changes: 1 addition & 1 deletion src/hooks/use-set-focus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from 'react';
import FocusContext from '../focus-context';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { Id } from '../types';

export default function useSetFocus(): (focusId: Id) => void {
Expand Down
4 changes: 2 additions & 2 deletions src/update-focus/compute-focus-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getParents, getChildren } from '../utils/tree-navigation';
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { FocusState, Id, Orientation, NodeHierarchy, Node } from '../types';

interface ComputeFocusHierarchyOptions {
Expand All @@ -22,7 +22,7 @@ function generateFocusHierarchyFromId({
orientation,
preferEnd,
}: GenerateFocusHierarchyFromIdOptions): Id[] {
const node = (focusState.nodes[propagateFromId] as unknown) as Node;
const node = focusState.nodes[propagateFromId] as unknown as Node;
let preferredChildren: NodeHierarchy = [];
if (node.trap) {
preferredChildren = node._focusTrapPreviousHierarchy;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/create-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import warning from '../utils/warning';
import { warning } from '../utils/warning';
import { NodeDefinition, FocusState, NodeMap, Id, Node } from '../types';
import getIndex from './get-index';

Expand Down Expand Up @@ -120,8 +120,8 @@ export default function createNodeDefinitionHierarchy({
const parentDefinition = nodeDefinitionHierarchy[parentLoopIndex];
const parentId = parentDefinition.focusId;

const parentNode = ((focusState.nodes[parentId] ||
nodeUpdates[parentId]) as unknown) as Node;
const parentNode = (focusState.nodes[parentId] ||
nodeUpdates[parentId]) as unknown as Node;

const parentChildren = parentNode.children;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/delete-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nodeIdIsFocused from './node-id-is-focused';
import warning from './warning';
import { warning } from './warning';
import updateFocus from '../update-focus/update-focus';
import { FocusState, Id, NodeMap, Node } from '../types';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface CodeCache {

let codeCache: CodeCache = {};

export default function warning(message: string, code: string) {
export function warning(message: string, code: string) {
// This ensures that each warning type is only logged out one time
if (code) {
if (codeCache[code]) {
Expand Down

0 comments on commit 6d52eec

Please sign in to comment.