|
1 |
| -import {useIsFocused as realUseIsFocused, useTheme as realUseTheme} from '@react-navigation/native'; |
| 1 | +/* eslint-disable import/prefer-default-export, import/no-import-module-exports */ |
| 2 | +import type * as ReactNavigation from '@react-navigation/native'; |
| 3 | +import createAddListenerMock from '../../../tests/utils/createAddListenerMock'; |
2 | 4 |
|
3 |
| -// We only want these mocked for storybook, not jest |
4 |
| -const useIsFocused: typeof realUseIsFocused = process.env.NODE_ENV === 'test' ? realUseIsFocused : () => true; |
| 5 | +const isJestEnv = process.env.NODE_ENV === 'test'; |
5 | 6 |
|
6 |
| -const useTheme = process.env.NODE_ENV === 'test' ? realUseTheme : () => ({}); |
| 7 | +const realReactNavigation = isJestEnv ? jest.requireActual<typeof ReactNavigation>('@react-navigation/native') : (require('@react-navigation/native') as typeof ReactNavigation); |
| 8 | + |
| 9 | +const useIsFocused = isJestEnv ? realReactNavigation.useIsFocused : () => true; |
| 10 | +const useTheme = isJestEnv ? realReactNavigation.useTheme : () => ({}); |
| 11 | + |
| 12 | +const {triggerTransitionEnd, addListener} = isJestEnv |
| 13 | + ? createAddListenerMock() |
| 14 | + : { |
| 15 | + triggerTransitionEnd: () => {}, |
| 16 | + addListener: () => {}, |
| 17 | + }; |
| 18 | + |
| 19 | +const useNavigation = () => ({ |
| 20 | + ...realReactNavigation.useNavigation, |
| 21 | + navigate: jest.fn(), |
| 22 | + getState: () => ({ |
| 23 | + routes: [], |
| 24 | + }), |
| 25 | + addListener, |
| 26 | +}); |
| 27 | + |
| 28 | +type NativeNavigationMock = typeof ReactNavigation & { |
| 29 | + triggerTransitionEnd: () => void; |
| 30 | +}; |
7 | 31 |
|
8 | 32 | export * from '@react-navigation/core';
|
9 |
| -export {useIsFocused, useTheme}; |
| 33 | +const Link = realReactNavigation.Link; |
| 34 | +const LinkingContext = realReactNavigation.LinkingContext; |
| 35 | +const NavigationContainer = realReactNavigation.NavigationContainer; |
| 36 | +const ServerContainer = realReactNavigation.ServerContainer; |
| 37 | +const DarkTheme = realReactNavigation.DarkTheme; |
| 38 | +const DefaultTheme = realReactNavigation.DefaultTheme; |
| 39 | +const ThemeProvider = realReactNavigation.ThemeProvider; |
| 40 | +const useLinkBuilder = realReactNavigation.useLinkBuilder; |
| 41 | +const useLinkProps = realReactNavigation.useLinkProps; |
| 42 | +const useLinkTo = realReactNavigation.useLinkTo; |
| 43 | +const useScrollToTop = realReactNavigation.useScrollToTop; |
| 44 | +export { |
| 45 | + // Overriden modules |
| 46 | + useIsFocused, |
| 47 | + useTheme, |
| 48 | + useNavigation, |
| 49 | + triggerTransitionEnd, |
| 50 | + |
| 51 | + // Theme modules are left alone |
| 52 | + Link, |
| 53 | + LinkingContext, |
| 54 | + NavigationContainer, |
| 55 | + ServerContainer, |
| 56 | + DarkTheme, |
| 57 | + DefaultTheme, |
| 58 | + ThemeProvider, |
| 59 | + useLinkBuilder, |
| 60 | + useLinkProps, |
| 61 | + useLinkTo, |
| 62 | + useScrollToTop, |
| 63 | +}; |
| 64 | + |
| 65 | +export type {NativeNavigationMock}; |
0 commit comments