freshyo/apps/admin-ui/tests/mocks/react-navigation-mock.tsx
2026-10-20 00:31:05 +05:30

82 lines
2.9 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Manual mock for `@react-navigation/native`.
*
* `useFocusEffect` is used by `useMarkDataFetchers` / `useDrawerTitle` /
* `useFocusCallback`; outside a NavigationContainer the real one throws. Here it
* behaves like a mount effect so focus-driven refreshes can be asserted.
*/
import * as React from 'react'
export const useFocusEffect = (callback: () => void | (() => void)) => {
React.useEffect(() => {
const cleanup = callback()
return typeof cleanup === 'function' ? cleanup : undefined
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [callback])
}
export const useIsFocused = () => true
export const useNavigation = () => ({
setOptions: jest.fn(),
addListener: jest.fn(() => jest.fn()),
removeListener: jest.fn(),
dispatch: jest.fn(),
goBack: jest.fn(),
canGoBack: jest.fn(() => true),
navigate: jest.fn(),
getParent: jest.fn(() => undefined),
getState: jest.fn(() => ({ routes: [] })),
})
export const useRoute = () => ({ key: 'test-route', name: 'test', params: {} })
export const useNavigationState = () => ({ index: 0, routes: [] })
export const useIsFocusedEffect = useFocusEffect
const ThemeProvider = ({ children }: any) => React.createElement(React.Fragment, null, children)
const NavigationContainer = React.forwardRef(({ children }: any, ref: any) =>
React.createElement(React.Fragment, null, children)
)
NavigationContainer.displayName = 'NavigationContainer'
const DefaultTheme = { dark: false, colors: {}, fonts: {} }
const DarkTheme = { dark: true, colors: {}, fonts: {} }
const CommonActions = {
navigate: (payload: any) => ({ type: 'NAVIGATE', payload }),
goBack: () => ({ type: 'GO_BACK' }),
reset: (payload: any) => ({ type: 'RESET', payload }),
setParams: (payload: any) => ({ type: 'SET_PARAMS', payload }),
}
const StackActions = {
push: (name: string, params?: any) => ({ type: 'PUSH', payload: { name, params } }),
replace: (name: string, params?: any) => ({ type: 'REPLACE', payload: { name, params } }),
pop: (count?: number) => ({ type: 'POP', payload: { count } }),
popToTop: () => ({ type: 'POP_TO_TOP' }),
}
const DrawerActions = {
openDrawer: () => ({ type: 'OPEN_DRAWER' }),
closeDrawer: () => ({ type: 'CLOSE_DRAWER' }),
toggleDrawer: () => ({ type: 'TOGGLE_DRAWER' }),
}
export const createNavigationContainerRef = () => ({
isReady: () => true,
navigate: jest.fn(),
dispatch: jest.fn(),
getCurrentRoute: () => undefined,
addListener: jest.fn(() => jest.fn()),
})
export const useNavigationContainerRef = createNavigationContainerRef
export { ThemeProvider, NavigationContainer, DefaultTheme, DarkTheme, CommonActions, StackActions, DrawerActions }
export default {
useFocusEffect,
useIsFocused,
useNavigation,
useRoute,
ThemeProvider,
NavigationContainer,
DefaultTheme,
DarkTheme,
CommonActions,
StackActions,
DrawerActions,
createNavigationContainerRef,
}