/** * Shared Jest setup for apps/user-ui. * * Only the native/context dependencies that cannot run under a plain Jest * environment are stubbed here. Everything else (components, stores, hooks) * stays real so tests exercise the actual wiring. * * Note: no `typeof import('react-native')` type expressions below. Loading the * RN ambient globals from a type position here reverses their precedence with * @types/node's, which flips `setTimeout` from `number` to `NodeJS.Timeout` and * fails typecheck in unrelated app files (packages/ui/src/components/ * bottom-dropdown.tsx). */ /** * Note: `expo-router` is NOT mocked here. A global mock would also apply to * tests/home-page/navigation.test.tsx, which drives the real router. Test files * that need the mocked router add it themselves (see tests/mocks/router.ts). */ /** * Refresh plumbing, irrelevant to most assertions. * `useMarkDataFetchers` runs `useFocusEffect`, which throws without a * NavigationContainer. These are mocked by their own module paths rather than * partially mocking the `common-ui` index: several index components import * back from `common-ui` (button.tsx -> common-ui), so spreading * `jest.requireActual('common-ui')` walks a half-initialised module and throws. * * Rather than no-op'ing them, the mocks record the callback so * tests/home-page/home-refresh.test.tsx can invoke the page's handleRefresh * (see tests/mocks/refresh.ts). */ jest.mock('common-ui/hooks/useMarkDataFetchers', () => ({ __esModule: true, useMarkDataFetchers: (cb: () => void) => { require('./mocks/refresh').refreshHooks.markDataFetchers = cb }, })) jest.mock('common-ui/hooks/useManualRefresh', () => ({ __esModule: true, default: (cb: () => void) => { require('./mocks/refresh').refreshHooks.manualRefresh = cb }, })) // Registered here (not in the test files) so it is in place before any app // module imports axios. Implementations are set in tests/mocks/api.ts. jest.mock('axios') /** * Native / third-party modules that cannot run under jest. Stubs live in * tests/mocks/native.ts; individual tests spy on or override them as needed. */ jest.mock('react-native-webview', () => require('./mocks/native').webviewMock()) jest.mock('expo-updates', () => require('./mocks/native').updatesMock()) jest.mock('expo-linking', () => require('./mocks/native').linkingMock()) jest.mock('expo-location', () => require('./mocks/native').locationMock()) jest.mock('expo-notifications', () => require('./mocks/native').notificationsMock()) jest.mock('react-native-razorpay', () => require('./mocks/native').razorpayMock()) /** * useHideTabNav / useAuthenticatedRoute use @react-navigation/native's * useFocusEffect, which throws without a NavigationContainer. Run the callback * once on mount instead so focus-driven side effects are testable. */ jest.mock('@react-navigation/native', () => { const actual = jest.requireActual('@react-navigation/native') const React = require('react') return { ...actual, useFocusEffect: (callback: () => void | (() => void)) => { const ran = React.useRef(false) React.useEffect(() => { if (ran.current) return ran.current = true return callback() }, []) }, } }) /** * Auth is out of scope for these tests. `useUserDetails` throws without an * AuthProvider (which would drag in secure-store, tRPC login and redirect * handling), so the context is stubbed with a signed-out shape. Overridable via * tests/mocks/auth.ts (set authMock.userDetails) for signed-in cases. */ jest.mock('@/src/contexts/AuthContext', () => require('./mocks/auth').authContextMock() ) /** * Cart hooks import this deep path directly (not via the common-ui index), and * the real service talks to expo-secure-store. In-memory map instead; the cart * starts empty, which is the cold-start path these tests exercise. */ jest.mock('common-ui/src/services/StorageServiceCasual', () => { const store = new Map() return { StorageServiceCasual: { getItem: jest.fn(async (key: string) => store.get(key) ?? null), setItem: jest.fn(async (key: string, value: string) => { store.set(key, value) return true }), removeItem: jest.fn(async (key: string) => { store.delete(key) return true }), clearAll: jest.fn(async (keys: string[]) => { keys.forEach((key) => store.delete(key)) return true }), }, } }) /** * Secure StorageService (expo-secure-store backed). Mocked at the service * module path: packages/ui resolves expo-secure-store from its own node_modules, * so mocking the native module from here would not reach it. */ jest.mock('common-ui/src/services/StorageService', () => { const store = new Map() return { StorageService: { getItem: jest.fn(async (key: string) => store.get(key) ?? null), setItem: jest.fn(async (key: string, value: string) => { store.set(key, value) return true }), removeItem: jest.fn(async (key: string) => { store.delete(key) return true }), clearAll: jest.fn(async (keys: string[]) => { keys.forEach((key) => store.delete(key)) return true }), }, } }) /** * Freeze the clock for every test. The home page is time-sensitive — it drops * slots whose deliveryTime is not after `dayjs()` and flags slots "closing * soon" within 4 hours — while the slot mocks carry fixed deliveryTime values. * A drifting wall clock therefore makes those assertions flaky. * * Frozen at suite start rather than a hard-coded instant, so the fixed slot * dates in the mocks stay in the future. Use `jest.advanceTimersByTime()` / * `jest.setSystemTime()` in a test that needs to move through time. */ jest.useFakeTimers({ now: Date.now() })