d3ro-voice/apps/mobile-rn/__tests__/notification-native.test.ts
2026-08-29 18:33:45 +09:00

71 lines
2.4 KiB
TypeScript

jest.mock('react-native', () => {
const nativeModule = {
getStatus: jest.fn(),
getRegistrationId: jest.fn(),
deleteRegistration: jest.fn(),
getInitialNotification: jest.fn(),
clearFullSyncRequired: jest.fn(),
showLocalTranscriptionCompleted: jest.fn(),
recordPermissionResult: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
}
const permissions = {
PERMISSIONS: { POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS' },
RESULTS: {
GRANTED: 'granted',
DENIED: 'denied',
NEVER_ASK_AGAIN: 'never_ask_again',
},
check: jest.fn(),
request: jest.fn(),
}
class NativeEventEmitter {
addListener(): { remove: () => void } {
return { remove: jest.fn() }
}
}
return {
NativeEventEmitter,
NativeModules: { D3RONotifications: nativeModule },
PermissionsAndroid: permissions,
Platform: { OS: 'android', Version: 33 },
__notificationTestDoubles: { nativeModule, permissions },
}
})
const testDoubles = (jest.requireMock('react-native') as {
__notificationTestDoubles: {
nativeModule: { recordPermissionResult: jest.Mock }
permissions: { check: jest.Mock; request: jest.Mock }
}
}).__notificationTestDoubles
import { requestNotificationPermission } from '../src/features/notifications/notification-native'
describe('Android notification permission persistence bridge', () => {
beforeEach(() => {
testDoubles.nativeModule.recordPermissionResult.mockReset().mockResolvedValue(undefined)
testDoubles.permissions.check.mockReset().mockResolvedValue(false)
testDoubles.permissions.request.mockReset()
})
test.each([
['denied', 'denied'],
['never_ask_again', 'blocked'],
['granted', 'granted'],
])('records Android result %s before returning %s', async (nativeResult, expected) => {
testDoubles.permissions.request.mockResolvedValue(nativeResult)
await expect(requestNotificationPermission()).resolves.toBe(expected)
expect(testDoubles.nativeModule.recordPermissionResult).toHaveBeenCalledWith(nativeResult)
})
test('does not prompt or rewrite state when permission is already granted', async () => {
testDoubles.permissions.check.mockResolvedValue(true)
await expect(requestNotificationPermission()).resolves.toBe('granted')
expect(testDoubles.permissions.request).not.toHaveBeenCalled()
expect(testDoubles.nativeModule.recordPermissionResult).not.toHaveBeenCalled()
})
})