Skip to content

Commit

Permalink
More focus node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jun 17, 2021
1 parent fa9e3bf commit dc97a5c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
72 changes: 71 additions & 1 deletion src/focus-node.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useRef } from 'react';
import '@testing-library/jest-dom';
import { render, act, fireEvent, screen } from '@testing-library/react';
import {
Expand Down Expand Up @@ -136,4 +136,74 @@ describe('FocusNode', () => {
expect(warning.mock.calls[0][1]).toEqual('ROOT_ID_WAS_PASSED');
});
});

describe('propsFromNode', () => {
it('allows you to pass props based on the node', () => {
let focusStore;
let setFocus;

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

return (
<>
<FocusNode
focusId="nodeA"
data-testid="nodeA"
propsFromNode={(node) => {
return {
className: node.isFocused ? 'sandwiches' : 'spaghetti',
};
}}
/>
<FocusNode focusId="nodeB" />
</>
);
}

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

expect(focusStore.getState().focusedNodeId).toBe('nodeA');
let nodeA = screen.getByTestId('nodeA');
expect(nodeA).toHaveClass('sandwiches');

act(() => setFocus('nodeB'));

expect(nodeA).toHaveClass('spaghetti');
});
});

describe('ref', () => {
it('allows you to pass a ref', () => {
let focusStore;
let elRef;

function TestComponent() {
focusStore = useFocusStoreDangerously();
elRef = useRef();

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

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

expect(focusStore.getState().focusedNodeId).toBe('nodeA');
let nodeA = screen.getByTestId('nodeA');
expect(elRef.current).toBe(nodeA);
});
});
});
2 changes: 0 additions & 2 deletions src/focus-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ export function FocusNode(
'ROOT_ID_WAS_PASSED'
);
}
}

if (process.env.NODE_ENV !== 'production') {
if (nonStringFocusId) {
warning(
'A focus node with an invalid focus ID was created: "root". This is a reserved ID, so it has been ' +
Expand Down

0 comments on commit dc97a5c

Please sign in to comment.