freshyo/apps/user-ui/tests/mocks/expo-notifications-mock.ts
2026-10-18 21:51:01 +05:30

113 lines
3.3 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Configurable stand-in for `expo-notifications`.
*
* Tests can flip the permission result and fire the received/response listeners
* that `NotificationProvider` subscribes to.
*/
interface NotifConfig {
permission: 'granted' | 'denied' | 'undetermined'
requestResult: 'granted' | 'denied'
token: string
projectId: string | null
}
const config: NotifConfig = {
permission: 'granted',
requestResult: 'granted',
token: 'ExponentPushToken[test-token]',
projectId: 'test-project-id',
}
type Listener = (payload: any) => void
const receivedListeners: Listener[] = []
const responseListeners: Listener[] = []
export function __configure(next: Partial<NotifConfig>) {
Object.assign(config, next)
}
export function __reset() {
config.permission = 'granted'
config.requestResult = 'granted'
config.token = 'ExponentPushToken[test-token]'
config.projectId = 'test-project-id'
receivedListeners.length = 0
responseListeners.length = 0
}
export function __emitNotification(notification: any) {
receivedListeners.forEach((listener) => listener(notification))
}
export function __emitResponse(response: any) {
responseListeners.forEach((listener) => listener(response))
}
export const AndroidImportance = { MAX: 5, HIGH: 4, DEFAULT: 3, LOW: 2, MIN: 1 }
export const setNotificationHandler = jest.fn()
export const setNotificationChannelAsync = jest.fn(async () => null)
export const getPermissionsAsync = jest.fn(async () => ({
status: config.permission,
granted: config.permission === 'granted',
canAskAgain: true,
}))
export const requestPermissionsAsync = jest.fn(async () => ({
status: config.requestResult,
granted: config.requestResult === 'granted',
canAskAgain: true,
}))
export const getExpoPushTokenAsync = jest.fn(async (_args?: any) => ({ data: config.token }))
export const getDevicePushTokenAsync = jest.fn(async () => ({ data: config.token }))
export const scheduleNotificationAsync = jest.fn(async () => 'notification-id')
export const cancelAllScheduledNotificationsAsync = jest.fn(async () => undefined)
export const getLastNotificationResponseAsync = jest.fn(async () => null)
export function addNotificationReceivedListener(listener: Listener) {
receivedListeners.push(listener)
return {
remove: () => {
const index = receivedListeners.indexOf(listener)
if (index >= 0) receivedListeners.splice(index, 1)
},
}
}
export function addNotificationResponseReceivedListener(listener: Listener) {
responseListeners.push(listener)
return {
remove: () => {
const index = responseListeners.indexOf(listener)
if (index >= 0) responseListeners.splice(index, 1)
},
}
}
export const removeNotificationSubscription = jest.fn((subscription: any) => {
subscription?.remove?.()
})
export const useLastNotificationResponse = () => null
export default {
AndroidImportance,
setNotificationHandler,
setNotificationChannelAsync,
getPermissionsAsync,
requestPermissionsAsync,
getExpoPushTokenAsync,
getDevicePushTokenAsync,
scheduleNotificationAsync,
cancelAllScheduledNotificationsAsync,
getLastNotificationResponseAsync,
addNotificationReceivedListener,
addNotificationResponseReceivedListener,
removeNotificationSubscription,
useLastNotificationResponse,
__configure,
__reset,
__emitNotification,
__emitResponse,
}