File size: 3,083 Bytes
68465df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import {Platform} from "react-native"
import CrustModule from "@mentra/crust"
import showAlert from "@/utils/AlertUtils"
import {checkAndRequestNotificationAccessSpecialPermission} from "./NotificationServiceUtils"
import {checkPermissionAfterSettingsReturn} from "./notificationPermissionFlow"
jest.mock("@mentra/crust", () => ({
__esModule: true,
default: {
hasNotificationListenerPermission: jest.fn(),
openNotificationListenerSettings: jest.fn(),
refreshNotificationListener: jest.fn(),
},
}))
jest.mock("@/utils/AlertUtils", () => ({
__esModule: true,
default: jest.fn(),
}))
jest.mock("./notificationPermissionFlow", () => ({
checkPermissionAfterSettingsReturn: jest.fn(),
}))
const mockCrust = CrustModule as jest.Mocked<typeof CrustModule>
const mockShowAlert = showAlert as jest.MockedFunction<typeof showAlert>
const mockCheckPermissionAfterSettingsReturn = checkPermissionAfterSettingsReturn as jest.MockedFunction<
typeof checkPermissionAfterSettingsReturn
>
describe("checkAndRequestNotificationAccessSpecialPermission", () => {
beforeEach(() => {
jest.clearAllMocks()
Object.defineProperty(Platform, "OS", {value: "android", configurable: true, writable: true})
mockCrust.hasNotificationListenerPermission.mockResolvedValue(false)
mockCrust.refreshNotificationListener.mockResolvedValue(true)
})
it("distinguishes postponing the prompt from denying in Settings", async () => {
const result = checkAndRequestNotificationAccessSpecialPermission()
await Promise.resolve()
const buttons = mockShowAlert.mock.calls[0][2]
buttons?.[0].onPress?.()
await expect(result).resolves.toBe("cancelled")
})
it("does not restart the listener when access is already granted", async () => {
mockCrust.hasNotificationListenerPermission.mockResolvedValue(true)
await expect(checkAndRequestNotificationAccessSpecialPermission()).resolves.toBe("granted")
expect(mockShowAlert).not.toHaveBeenCalled()
expect(mockCrust.refreshNotificationListener).not.toHaveBeenCalled()
})
it("reports denial after returning from Settings without access", async () => {
mockCheckPermissionAfterSettingsReturn.mockResolvedValue(false)
const result = checkAndRequestNotificationAccessSpecialPermission()
await Promise.resolve()
const buttons = mockShowAlert.mock.calls[0][2]
await buttons?.[1].onPress?.()
await expect(result).resolves.toBe("denied")
expect(mockCrust.refreshNotificationListener).not.toHaveBeenCalled()
})
it("starts the listener after access is granted in Settings", async () => {
mockCheckPermissionAfterSettingsReturn.mockResolvedValue(true)
const result = checkAndRequestNotificationAccessSpecialPermission()
await Promise.resolve()
const buttons = mockShowAlert.mock.calls[0][2]
await buttons?.[1].onPress?.()
await expect(result).resolves.toBe("granted")
expect(mockCrust.refreshNotificationListener).toHaveBeenCalledTimes(1)
})
})
|