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

140 lines
3.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Manual mock for `expo-router` (and `expo-router/tabs`, `expo-router/drawer`).
*
* Navigation is recorded, never performed. Layout primitives render their
* children so route files can be mounted directly.
*/
import * as React from 'react'
type AnyRecord = Record<string, any>
function fn() {
return jest.fn()
}
export const router = {
push: fn(),
replace: fn(),
back: fn(),
navigate: fn(),
dismiss: fn(),
dismissTo: fn(),
dismissAll: fn(),
canGoBack: jest.fn(() => true),
setParams: fn(),
reload: fn(),
prefetch: fn(),
}
let params: AnyRecord = {}
let pathname = '/'
let segments: string[] = []
export function __setParams(next: AnyRecord) {
params = next ?? {}
}
export function __setPathname(next: string) {
pathname = next
}
export function __setSegments(next: string[]) {
segments = next ?? []
}
export function __resetRouterMock() {
params = {}
pathname = '/'
segments = []
Object.values(router).forEach((value) => {
if (typeof (value as any).mockClear === 'function') (value as any).mockClear()
})
router.canGoBack.mockReturnValue(true)
}
export const useRouter = () => router
export const useLocalSearchParams = () => params
export const useGlobalSearchParams = () => params
export const usePathname = () => pathname
export const useSegments = () => segments
export const useNavigationContainerRef = () => ({
addListener: jest.fn(() => jest.fn()),
isReady: () => true,
navigate: fn(),
})
export const useRootNavigationState = () => ({ key: 'root', routes: [] })
export const useIsFocused = () => true
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 useNavigation = () => ({
setOptions: fn(),
addListener: jest.fn(() => jest.fn()),
removeListener: fn(),
dispatch: fn(),
goBack: fn(),
canGoBack: jest.fn(() => true),
getParent: jest.fn(() => undefined),
navigate: fn(),
push: fn(),
replace: fn(),
})
export const useNavigationState = () => ({ index: 0, routes: [] })
export const Redirect = ({ href }: { href: any }) => {
router.replace(href)
return null
}
export const Link = ({ children, href, ...rest }: any) => {
if (React.isValidElement(children)) {
return React.cloneElement(children as any, {
...rest,
onPress: (event: any) => {
;(children as any).props?.onPress?.(event)
router.push(href)
},
})
}
return React.createElement(React.Fragment, null, children)
}
const makeLayout = (name: string) => {
const Layout = ({ children, ...rest }: any) =>
React.createElement(React.Fragment, null, children)
Layout.displayName = name
Layout.Screen = (_props: any) => null
Layout.Trigger = (_props: any) => null
Layout.Protected = ({ children }: any) =>
React.createElement(React.Fragment, null, children)
return Layout
}
export const Stack = makeLayout('Stack')
export const Tabs = makeLayout('Tabs')
export const Drawer = makeLayout('Drawer')
export const Slot = ({ children }: any) =>
React.createElement(React.Fragment, null, children)
export const ErrorBoundary = ({ children }: any) =>
React.createElement(React.Fragment, null, children)
export default {
router,
useRouter,
useLocalSearchParams,
useGlobalSearchParams,
usePathname,
useSegments,
useNavigation,
useFocusEffect,
Redirect,
Link,
Stack,
Tabs,
Drawer,
Slot,
ErrorBoundary,
}