Skip to content

Commit 51cc61c

Browse files
authored
Merge pull request #40 from underscopeio/fix/use-navigation-context
Fix args precedence in useNavigation
2 parents bda383e + 7157fa9 commit 51cc61c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/hooks/useNavigation.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ describe('useNavigation', () => {
4141
expect(result.error).toBeUndefined()
4242
})
4343

44+
it('should return helpers taking precedence componentId over CompoenentIdProvider', () => {
45+
jest.spyOn(createNavigationCommands, 'default')
46+
47+
const wrapper = ({ children }) => (
48+
<NavigationProvider value={{ componentId: 'componentId' }}>{children}</NavigationProvider>
49+
)
50+
51+
const { result, unmount } = renderHook(() => useNavigation('componentIdArgument'), { wrapper })
52+
53+
unmount()
54+
55+
expect(createNavigationCommands.default).toHaveBeenCalledWith('componentIdArgument')
56+
57+
expect(Object.keys(result.current)).toEqual(navigationCommandKeys)
58+
expect(result.error).toBeUndefined()
59+
})
60+
4461
it('should throw when not using NavigationProvider nor componentId as argument', () => {
4562
const { result, unmount } = renderHook(() => useNavigation())
4663

src/hooks/useNavigation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const useNavigation = (
1717
*/
1818
componentId?: string
1919
) => {
20-
const { componentId: id = componentId } = useContext(NavigationContext)
20+
const { componentId: contextComponentId } = useContext(NavigationContext)
21+
22+
const id = componentId || contextComponentId
2123

2224
if (!id) {
2325
throw new Error('Missing "componentId". Use NavigationContext or pass "componentId" as argument.')

0 commit comments

Comments
 (0)