124 lines
3.7 KiB
TypeScript
124 lines
3.7 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[] = []
|
|
|
|
let lastResponse: any = null
|
|
|
|
export function __configure(next: Partial<NotifConfig>) {
|
|
Object.assign(config, next)
|
|
}
|
|
|
|
/** Seed the response that `getLastNotificationResponseAsync` replays (cold start). */
|
|
export function __setLastResponse(response: any) {
|
|
lastResponse = response
|
|
}
|
|
|
|
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
|
|
lastResponse = null
|
|
}
|
|
|
|
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 () => lastResponse)
|
|
export const clearLastNotificationResponseAsync = jest.fn(async () => undefined)
|
|
|
|
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,
|
|
clearLastNotificationResponseAsync,
|
|
addNotificationReceivedListener,
|
|
addNotificationResponseReceivedListener,
|
|
removeNotificationSubscription,
|
|
useLastNotificationResponse,
|
|
__configure,
|
|
__reset,
|
|
__setLastResponse,
|
|
__emitNotification,
|
|
__emitResponse,
|
|
}
|