freshyo/apps/admin-ui/tests/setup.ts
2026-10-01 00:33:03 +05:30

122 lines
4 KiB
TypeScript

/**
* Shared Jest setup for apps/admin-ui.
*
* Only the native/context dependencies that cannot run under a plain Jest
* environment are stubbed here. Components, stores and hooks stay real.
*
* expo-router is NOT mocked globally — tests that need it add the mock
* themselves (see tests/mocks/router.ts), and router tests can use the real one.
*/
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
},
}))
/**
* Native / third-party modules that cannot run under jest. Stubs live in
* tests/mocks/native.ts.
*/
jest.mock('react-native-draggable-flatlist', () => require('./mocks/native').draggableFlatListMock())
jest.mock('react-native-tab-view', () => require('./mocks/native').tabViewMock())
jest.mock('react-native-toast-message', () => require('./mocks/native').toastMock())
jest.mock('expo-location', () => require('./mocks/native').locationMock())
jest.mock('expo-image-picker', () => require('./mocks/native').imagePickerMock())
jest.mock('expo-document-picker', () => require('./mocks/native').documentPickerMock())
jest.mock('expo-device', () => require('./mocks/native').deviceMock())
jest.mock('expo-haptics', () => require('./mocks/native').hapticsMock())
jest.mock('expo-linear-gradient', () => require('./mocks/native').linearGradientMock())
jest.mock('expo-blur', () => require('./mocks/native').blurMock())
jest.mock('expo-image', () => require('./mocks/native').imageMock())
/**
* useFocusEffect 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()
}, [])
},
}
})
/**
* Staff auth is stubbed with a signed-in shape, overridable via
* tests/mocks/staff-auth.ts. Real-provider tests unmock this module.
*/
jest.mock('@/components/context/staff-auth-context', () =>
require('./mocks/staff-auth').staffAuthContextMock()
)
/**
* Secure + casual storage services are backed by expo-secure-store. Mock at the
* service module path with an in-memory map.
*/
jest.mock('common-ui/src/services/StorageService', () => {
const store = new Map<string, string>()
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
}),
},
}
})
jest.mock('common-ui/src/services/StorageServiceCasual', () => {
const store = new Map<string, string>()
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
}),
},
}
})
/**
* Freeze the clock so date-sensitive screens (slots, orders, banners) are
* deterministic. Move it with jest.setSystemTime / advanceTimersByTime.
*/
jest.useFakeTimers({ now: Date.now() })