freshyo/apps/user-ui/tests/mocks/native/common.tsx
2026-11-01 19:11:48 +05:30

142 lines
5.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Shared named-export shims for the stateless native/Expo modules.
*
* Mapped by `jest.config.js` moduleNameMapper so that every copy of a package
* (root node_modules and packages/ui/node_modules) resolves to the same mock.
*/
import * as React from 'react'
import { View } from 'react-native'
/* expo-image */
const ImageComponent = ({ testID, source, ...props }: any) =>
React.createElement(View, {
...props,
testID: testID ?? 'expo-image',
accessibilityLabel: typeof source === 'object' ? source?.uri : undefined,
})
export const Image = Object.assign(ImageComponent, {
clearDiskCache: jest.fn(async () => true),
clearMemoryCache: jest.fn(async () => true),
prefetch: jest.fn(async () => true),
})
export const ImageBackground = Image
/* expo-file-system */
export const documentDirectory = 'file:///documents/'
export const getInfoAsync = jest.fn(async () => ({ exists: false }))
export const readAsStringAsync = jest.fn(async () => '')
export const writeAsStringAsync = jest.fn(async () => undefined)
export const deleteAsync = jest.fn(async () => undefined)
/* expo-linear-gradient */
export const LinearGradient = ({ children, testID, ...props }: any) =>
React.createElement(View, { ...props, testID: testID ?? 'linear-gradient' }, children)
/* expo-blur */
export const BlurView = ({ children, testID, ...props }: any) =>
React.createElement(View, { ...props, testID: testID ?? 'blur-view' }, children)
/* expo-status-bar */
export const StatusBar = (props: any) =>
React.createElement(View, { ...props, testID: props.testID ?? 'status-bar' })
export const StatusBarStyle = { Light: 'light', Dark: 'dark', Auto: 'auto' }
/* expo-font */
export const useFonts = jest.fn(() => [true, null])
export const loadAsync = jest.fn(async () => undefined)
export const isLoaded = jest.fn(() => true)
/* expo-updates */
export const checkForUpdateAsync = jest.fn(async () => ({ isAvailable: false }))
export const fetchUpdateAsync = jest.fn(async () => ({}))
export const reloadAsync = jest.fn(async () => undefined)
export const updateId = 'test-update-id'
/* expo-splash-screen */
export const preventAutoHideAsync = jest.fn(async () => undefined)
export const hideAsync = jest.fn(async () => undefined)
export const setOptions = jest.fn()
/* expo-web-browser */
export const openBrowserAsync = jest.fn(async () => ({ type: 'dismiss' }))
export const dismissBrowser = jest.fn(async () => undefined)
export const maybeCompleteAuthSession = jest.fn()
/* expo-linking */
export const openURL = jest.fn(async () => true)
export const openSettings = jest.fn(async () => undefined)
export const canOpenURL = jest.fn(async () => true)
export const createURL = jest.fn((path: string) => `freshyo://${path}`)
export const getInitialURL = jest.fn(async () => null)
export const addEventListener = jest.fn(() => ({ remove: jest.fn() }))
export const parse = jest.fn(() => ({ queryParams: {} }))
export const useURL = jest.fn(() => null)
/* expo-haptics */
export const impactAsync = jest.fn(async () => undefined)
export const notificationAsync = jest.fn(async () => undefined)
export const selectionAsync = jest.fn(async () => undefined)
export const ImpactFeedbackStyle = { Light: 'light', Medium: 'medium', Heavy: 'heavy' }
export const NotificationFeedbackType = { Success: 'success', Warning: 'warning', Error: 'error' }
/* expo-image-picker */
export const launchImageLibraryAsync = jest.fn(async () => ({ canceled: true, assets: null }))
export const launchCameraAsync = jest.fn(async () => ({ canceled: true, assets: null }))
export const requestMediaLibraryPermissionsAsync = jest.fn(async () => ({
granted: true,
status: 'granted',
}))
export const requestCameraPermissionsAsync = jest.fn(async () => ({ granted: true, status: 'granted' }))
export const MediaTypeOptions = { All: 'All', Images: 'Images', Videos: 'Videos' }
export const MediaType = { Images: 'images' }
/* expo-document-picker */
export const getDocumentAsync = jest.fn(async () => ({ canceled: true, assets: null }))
/* expo-device */
export const isDevice = true
export const deviceName = 'Test Device'
export const modelName = 'Test Model'
export const osName = 'iOS'
export const osVersion = '17.0'
export const DeviceType = { PHONE: 1, TABLET: 2 }
/* expo-auth-session */
export const makeRedirectUri = jest.fn(() => 'freshyo://redirect')
export const useAuthRequest = jest.fn(() => [null, null, jest.fn()])
export const ResponseType = { Token: 'token', IdToken: 'id_token' }
export const useIdTokenAuthRequest = jest.fn(() => [null, null, jest.fn()])
/* react-native-webview */
export const WebView = React.forwardRef((props: any, ref: any) =>
React.createElement(View, { ...props, ref, testID: props.testID ?? 'webview' })
)
;(WebView as any).displayName = 'WebView'
/* react-native-safe-area-context */
const insets = { top: 0, right: 0, bottom: 0, left: 0 }
const frame = { width: 390, height: 844, x: 0, y: 0 }
export const SafeAreaProvider = ({ children }: any) =>
React.createElement(React.Fragment, null, children)
export const SafeAreaView = ({ children, testID, ...props }: any) =>
React.createElement(View, { ...props, testID: testID ?? 'safe-area-view' }, children)
export const SafeAreaInsetsContext = React.createContext(insets)
export const SafeAreaFrameContext = React.createContext(frame)
export const initialWindowMetrics = { insets, frame }
export const useSafeAreaInsets = () => insets
export const useSafeAreaFrame = () => frame
export const withSafeAreaInsets = (Component: any) => Component
export const Rect = { __type: 'Rect' }
export default {
Image,
ImageBackground,
LinearGradient,
BlurView,
StatusBar,
WebView,
SafeAreaView,
SafeAreaProvider,
}