import React from "react"; import { renderHook } from "@testing-library/react-native"; import { NavigationProvider, useNavigation, type AppRouter } from "../NavigationContext"; import * as expoRouterModule from "expo-router"; jest.mock("expo-router", () => { const mockRouter = { push: jest.fn(), replace: jest.fn(), back: jest.fn() }; return { __mockRouter: mockRouter, useRouter: () => mockRouter, }; }); const expoRouter = (expoRouterModule as unknown as { __mockRouter: AppRouter }).__mockRouter; beforeEach(() => { (expoRouter.push as jest.Mock).mockClear(); (expoRouter.replace as jest.Mock).mockClear(); (expoRouter.back as jest.Mock).mockClear(); }); describe("useNavigation", () => { it("falls back to expo-router when no provider is present", () => { const { result } = renderHook(() => useNavigation()); result.current.push("/welcome"); result.current.replace("/login"); result.current.back(); expect(expoRouter.push).toHaveBeenCalledWith("/welcome"); expect(expoRouter.replace).toHaveBeenCalledWith("/login"); expect(expoRouter.back).toHaveBeenCalled(); }); it("uses the override router when wrapped in NavigationProvider", () => { const custom: AppRouter = { push: jest.fn(), replace: jest.fn(), back: jest.fn(), }; const { result } = renderHook(() => useNavigation(), { wrapper: ({ children }) => ( {children} ), }); result.current.push("/a"); result.current.replace("/b"); result.current.back(); expect(custom.push).toHaveBeenCalledWith("/a"); expect(custom.replace).toHaveBeenCalledWith("/b"); expect(custom.back).toHaveBeenCalled(); expect(expoRouter.push).not.toHaveBeenCalled(); }); });