-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
945 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import '@testing-library/jest-dom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { render, fireEvent, screen } from '@testing-library/react'; | ||
import { FocusRoot, FocusNode, useFocusStoreDangerously } from './index'; | ||
|
||
describe('<FocusRoot/>', () => { | ||
describe('wrapping', () => { | ||
it('does not wrap by default', () => { | ||
let focusStore; | ||
|
||
function TestComponent() { | ||
focusStore = useFocusStoreDangerously(); | ||
|
||
return ( | ||
<> | ||
<FocusNode focusId="nodeA" data-testid="nodeA"> | ||
A | ||
</FocusNode> | ||
<FocusNode focusId="nodeB" data-testid="nodeB"> | ||
B | ||
</FocusNode> | ||
</> | ||
); | ||
} | ||
|
||
render( | ||
<FocusRoot> | ||
<TestComponent /> | ||
</FocusRoot> | ||
); | ||
|
||
const nodeAEl = screen.getByTestId('nodeA'); | ||
expect(nodeAEl).toHaveClass('isFocused'); | ||
expect(nodeAEl).toHaveClass('isFocusedLeaf'); | ||
|
||
const nodeBEl = screen.getByTestId('nodeB'); | ||
expect(nodeBEl).not.toHaveClass('isFocused'); | ||
expect(nodeBEl).not.toHaveClass('isFocusedLeaf'); | ||
|
||
const 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); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowRight', | ||
key: 'ArrowRight', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowRight', | ||
key: 'ArrowRight', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
}); | ||
|
||
it('supports wrapping', () => { | ||
let focusStore; | ||
|
||
function TestComponent() { | ||
focusStore = useFocusStoreDangerously(); | ||
|
||
return ( | ||
<> | ||
<FocusNode focusId="nodeA" data-testid="nodeA"> | ||
A | ||
</FocusNode> | ||
<FocusNode focusId="nodeB" data-testid="nodeB"> | ||
B | ||
</FocusNode> | ||
</> | ||
); | ||
} | ||
|
||
render( | ||
<FocusRoot wrapping> | ||
<TestComponent /> | ||
</FocusRoot> | ||
); | ||
|
||
const nodeAEl = screen.getByTestId('nodeA'); | ||
expect(nodeAEl).toHaveClass('isFocused'); | ||
expect(nodeAEl).toHaveClass('isFocusedLeaf'); | ||
|
||
const nodeBEl = screen.getByTestId('nodeB'); | ||
expect(nodeBEl).not.toHaveClass('isFocused'); | ||
expect(nodeBEl).not.toHaveClass('isFocusedLeaf'); | ||
|
||
const 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); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowRight', | ||
key: 'ArrowRight', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowRight', | ||
key: 'ArrowRight', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeA'); | ||
}); | ||
}); | ||
|
||
describe('orientation', () => { | ||
it('can be configured to be vertical', () => { | ||
let focusStore; | ||
|
||
function TestComponent() { | ||
focusStore = useFocusStoreDangerously(); | ||
|
||
return ( | ||
<> | ||
<FocusNode focusId="nodeA" data-testid="nodeA"> | ||
A | ||
</FocusNode> | ||
<FocusNode focusId="nodeB" data-testid="nodeB"> | ||
B | ||
</FocusNode> | ||
</> | ||
); | ||
} | ||
|
||
render( | ||
<FocusRoot orientation="vertical"> | ||
<TestComponent /> | ||
</FocusRoot> | ||
); | ||
|
||
const nodeAEl = screen.getByTestId('nodeA'); | ||
expect(nodeAEl).toHaveClass('isFocused'); | ||
expect(nodeAEl).toHaveClass('isFocusedLeaf'); | ||
|
||
const nodeBEl = screen.getByTestId('nodeB'); | ||
expect(nodeBEl).not.toHaveClass('isFocused'); | ||
expect(nodeBEl).not.toHaveClass('isFocusedLeaf'); | ||
|
||
const 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); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowDown', | ||
key: 'ArrowDown', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowDown', | ||
key: 'ArrowDown', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
}); | ||
}); | ||
|
||
describe('orientation + wrapping', () => { | ||
it('functions together', () => { | ||
let focusStore; | ||
|
||
function TestComponent() { | ||
focusStore = useFocusStoreDangerously(); | ||
|
||
return ( | ||
<> | ||
<FocusNode focusId="nodeA" data-testid="nodeA"> | ||
A | ||
</FocusNode> | ||
<FocusNode focusId="nodeB" data-testid="nodeB"> | ||
B | ||
</FocusNode> | ||
</> | ||
); | ||
} | ||
|
||
render( | ||
<FocusRoot orientation="vertical" wrapping> | ||
<TestComponent /> | ||
</FocusRoot> | ||
); | ||
|
||
const nodeAEl = screen.getByTestId('nodeA'); | ||
expect(nodeAEl).toHaveClass('isFocused'); | ||
expect(nodeAEl).toHaveClass('isFocusedLeaf'); | ||
|
||
const nodeBEl = screen.getByTestId('nodeB'); | ||
expect(nodeBEl).not.toHaveClass('isFocused'); | ||
expect(nodeBEl).not.toHaveClass('isFocusedLeaf'); | ||
|
||
const 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); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowDown', | ||
key: 'ArrowDown', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeB'); | ||
|
||
fireEvent.keyDown(window, { | ||
code: 'ArrowDown', | ||
key: 'ArrowDown', | ||
}); | ||
|
||
expect(focusStore.getState().focusedNodeId).toEqual('nodeA'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import createFocusStore from './focus-store'; | ||
|
||
describe('createFocusStore', () => { | ||
it('returns an object with the right shape', () => { | ||
const focusStore = createFocusStore(); | ||
|
||
expect(focusStore).toBeTruthy(); | ||
|
||
expect(typeof focusStore.subscribe).toEqual('function'); | ||
expect(typeof focusStore.getState).toEqual('function'); | ||
expect(typeof focusStore.createNodes).toEqual('function'); | ||
expect(typeof focusStore.deleteNode).toEqual('function'); | ||
expect(typeof focusStore.setFocus).toEqual('function'); | ||
expect(typeof focusStore.updateNode).toEqual('function'); | ||
expect(typeof focusStore.handleArrow).toEqual('function'); | ||
expect(typeof focusStore.handleSelect).toEqual('function'); | ||
expect(typeof focusStore.configurePointerEvents).toEqual('function'); | ||
expect(typeof focusStore.destroy).toEqual('function'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.