From f8f93c88b682fe3d5a34d26aa42919e99e6ed0a2 Mon Sep 17 00:00:00 2001 From: Tomasz Janiczek Date: Tue, 27 Jun 2023 11:11:13 +0200 Subject: [PATCH] fix: remove conditional React Navigation 7 support --- .../__tests__/adapter.v6.test.ts | 26 ------------------- .../__tests__/adapter.v7.test.ts | 26 ------------------- src/react-navigation/adapter.ts | 24 ----------------- .../views/MaterialBottomTabView.tsx | 5 ++-- 4 files changed, 2 insertions(+), 79 deletions(-) delete mode 100644 src/react-navigation/__tests__/adapter.v6.test.ts delete mode 100644 src/react-navigation/__tests__/adapter.v7.test.ts delete mode 100644 src/react-navigation/adapter.ts diff --git a/src/react-navigation/__tests__/adapter.v6.test.ts b/src/react-navigation/__tests__/adapter.v6.test.ts deleted file mode 100644 index 37d3525374..0000000000 --- a/src/react-navigation/__tests__/adapter.v6.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -// @ts-ignore -import { useLinkTools, useLinkBuilder } from '@react-navigation/native'; -import { renderHook } from '@testing-library/react-native'; - -import { useNavigationLink } from '../adapter'; - -jest.mock('@react-navigation/native', () => ({ - useLinkBuilder: jest.fn().mockReturnValue(jest.fn()), -})); - -describe('React Navigation adapter', () => { - describe('when v6 is used', () => { - it('should use useLinkBuilder() to create link', () => { - let builder; - - renderHook(() => { - builder = useNavigationLink(); - builder('routeKey'); - }); - - expect(useLinkTools).toBeUndefined(); - expect(useLinkBuilder).toHaveBeenCalled(); - expect(builder).toHaveBeenCalledWith('routeKey'); - }); - }); -}); diff --git a/src/react-navigation/__tests__/adapter.v7.test.ts b/src/react-navigation/__tests__/adapter.v7.test.ts deleted file mode 100644 index 4bc11e678e..0000000000 --- a/src/react-navigation/__tests__/adapter.v7.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -// @ts-ignore -import { useLinkTools, useLinkBuilder } from '@react-navigation/native'; -import { renderHook } from '@testing-library/react-native'; - -import { useNavigationLink } from '../adapter'; - -jest.mock('@react-navigation/native', () => ({ - useLinkTools: jest.fn().mockReturnValue({ buildHref: jest.fn() }), -})); - -describe('React Navigation adapter', () => { - describe('when v7 is used', () => { - it('should use useLinkTools() to create link', () => { - let builder; - - renderHook(() => { - builder = useNavigationLink(); - builder('routeKey'); - }); - - expect(useLinkTools).toHaveBeenCalled(); - expect(useLinkBuilder).toBeUndefined(); - expect(builder).toHaveBeenCalledWith('routeKey'); - }); - }); -}); diff --git a/src/react-navigation/adapter.ts b/src/react-navigation/adapter.ts deleted file mode 100644 index 30131b8012..0000000000 --- a/src/react-navigation/adapter.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { - // @ts-ignore: this hook is not available in React Navigation v7 - useLinkBuilder, - // @ts-ignore: this hook is not available in React Navigation v6 - useLinkTools, -} from '@react-navigation/native'; - -function useV7LinkBuilder() { - const tools = useLinkTools(); - return tools.buildHref; -} - -type NavigationLink = () => ( - name: string, - params?: object -) => string | undefined; - -/** - * In React Navigation 7 `useLinkBuilder` was superseded by `useLinkTools` - * https://reactnavigation.org/docs/7.x/upgrading-from-6.x/#the-uselinkto-and-uselinkbuilder-hooks-are-merged-into-uselinktools - **/ - -export const useNavigationLink: NavigationLink = - typeof useLinkTools !== 'undefined' ? useV7LinkBuilder : useLinkBuilder; diff --git a/src/react-navigation/views/MaterialBottomTabView.tsx b/src/react-navigation/views/MaterialBottomTabView.tsx index a582c2dfbc..df846ae2fb 100644 --- a/src/react-navigation/views/MaterialBottomTabView.tsx +++ b/src/react-navigation/views/MaterialBottomTabView.tsx @@ -7,11 +7,11 @@ import { ParamListBase, Route, TabNavigationState, + useLinkBuilder, } from '@react-navigation/native'; import BottomNavigation from '../../components/BottomNavigation/BottomNavigation'; import MaterialCommunityIcon from '../../components/MaterialCommunityIcon'; -import { useNavigationLink } from '../adapter'; import type { MaterialBottomTabDescriptorMap, MaterialBottomTabNavigationConfig, @@ -23,14 +23,13 @@ type Props = MaterialBottomTabNavigationConfig & { navigation: MaterialBottomTabNavigationHelpers; descriptors: MaterialBottomTabDescriptorMap; }; - export default function MaterialBottomTabView({ state, navigation, descriptors, ...rest }: Props) { - const buildLink = useNavigationLink(); + const buildLink = useLinkBuilder(); return (