Skip to content

Commit

Permalink
Add support for back
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jun 17, 2021
1 parent 04707b6 commit b7643bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/lrud-input/key-to-binding-map.ts
Expand Up @@ -5,4 +5,5 @@ export default {
ArrowLeft: 'left',
ArrowRight: 'right',
Enter: 'select',
Escape: 'back',
};
57 changes: 27 additions & 30 deletions src/tests/focus-node-events.test.js
Expand Up @@ -296,10 +296,10 @@ describe('FocusNode Events', () => {
});

describe('onBack', () => {
it.skip('calls it when Esc is pressed', () => {
it('calls it when Esc is pressed', () => {
const rootonBack = jest.fn();
const nodeAonBack = jest.fn();
const nodeBonBack = jest.fn();
const nodeAOnBack = jest.fn();
const nodeBOnBack = jest.fn();
let focusStore;

function TestComponent() {
Expand All @@ -309,13 +309,13 @@ describe('FocusNode Events', () => {
<FocusNode focusId="testRoot" onBack={rootonBack}>
<FocusNode
focusId="nodeA"
onBack={nodeAonBack}
onBack={nodeAOnBack}
data-testid="nodeA"
/>
<FocusNode
focusId="nodeB"
data-testid="nodeB"
onBack={nodeBonBack}
onBack={nodeBOnBack}
/>
</FocusNode>
);
Expand All @@ -330,36 +330,33 @@ describe('FocusNode Events', () => {
expect(focusStore.getState().focusedNodeId).toEqual('nodeA');

expect(rootonBack.mock.calls.length).toBe(0);
expect(nodeAonBack.mock.calls.length).toBe(0);
expect(nodeBonBack.mock.calls.length).toBe(0);
expect(nodeAOnBack.mock.calls.length).toBe(0);
expect(nodeBOnBack.mock.calls.length).toBe(0);

fireEvent.keyDown(window, {
code: 'Esc',
key: 'Esc',
code: 'Escape',
key: 'Escape',
});

expect(rootonBack.mock.calls.length).toBe(1);
expect(nodeAonBack.mock.calls.length).toBe(1);
expect(nodeBonBack.mock.calls.length).toBe(0);

// expect(rootonBack).toHaveBeenCalledWith(
// expect.objectContaining({
// key: 'select',
// isArrow: false,
// node: focusStore.getState().nodes.testRoot,
// })
// );

// expect(nodeAonBack).toHaveBeenCalledWith(
// expect.objectContaining({
// key: 'select',
// isArrow: false,
// // The event fires before the state updates to reflect the new
// // active state. This allows you to, say, prevent default
// // the action.
// node: preFocusedNodeA,
// })
// );
expect(nodeAOnBack.mock.calls.length).toBe(1);
expect(nodeBOnBack.mock.calls.length).toBe(0);

expect(rootonBack).toHaveBeenCalledWith(
expect.objectContaining({
key: 'back',
isArrow: false,
node: focusStore.getState().nodes.testRoot,
})
);

expect(nodeAOnBack).toHaveBeenCalledWith(
expect.objectContaining({
key: 'back',
isArrow: false,
node: focusStore.getState().nodes.nodeA,
})
);
});
});
});

0 comments on commit b7643bb

Please sign in to comment.