|
| 1 | +import { NativeModules } from "react-native"; |
| 2 | +import { trigger } from "../index"; |
| 3 | +import { HapticFeedbackTypes } from "../types"; |
| 4 | + |
| 5 | +jest.mock("../NativeHapticFeedback", () => ({ |
| 6 | + default: { |
| 7 | + trigger: jest.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +const NativeHapticFeedbackMock = require("../NativeHapticFeedback").default; |
| 12 | + |
| 13 | +describe("RNReactNativeHapticFeedback", () => { |
| 14 | + beforeAll(() => { |
| 15 | + global.__turboModuleProxy = null; |
| 16 | + NativeModules.RNHapticFeedback = { |
| 17 | + trigger: jest.fn(), |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should trigger haptic feedback with default options using NativeModules when turbo module is not used", () => { |
| 26 | + trigger(HapticFeedbackTypes.selection); |
| 27 | + |
| 28 | + expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith( |
| 29 | + "selection", |
| 30 | + { |
| 31 | + enableVibrateFallback: false, |
| 32 | + ignoreAndroidSystemSettings: false, |
| 33 | + }, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should trigger haptic feedback with turbo module when enabled", () => { |
| 38 | + global.__turboModuleProxy = true; |
| 39 | + |
| 40 | + trigger(HapticFeedbackTypes.selection); |
| 41 | + |
| 42 | + expect(NativeHapticFeedbackMock.trigger).toHaveBeenCalledWith("selection", { |
| 43 | + enableVibrateFallback: false, |
| 44 | + ignoreAndroidSystemSettings: false, |
| 45 | + }); |
| 46 | + |
| 47 | + global.__turboModuleProxy = null; |
| 48 | + }); |
| 49 | + |
| 50 | + it("should pass the correct options", () => { |
| 51 | + const options = { |
| 52 | + enableVibrateFallback: true, |
| 53 | + ignoreAndroidSystemSettings: true, |
| 54 | + }; |
| 55 | + |
| 56 | + trigger(HapticFeedbackTypes.selection, options); |
| 57 | + |
| 58 | + expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith( |
| 59 | + "selection", |
| 60 | + options, |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should handle the case when options is a boolean", () => { |
| 65 | + // @ts-expect-error - we're testing the case when options is a boolean for deprecated behavior |
| 66 | + trigger(HapticFeedbackTypes.selection, true); |
| 67 | + |
| 68 | + expect(NativeModules.RNHapticFeedback.trigger).toHaveBeenCalledWith( |
| 69 | + "selection", |
| 70 | + { |
| 71 | + enableVibrateFallback: true, |
| 72 | + ignoreAndroidSystemSettings: false, |
| 73 | + }, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should warn when haptic feedback module is not available", () => { |
| 78 | + delete NativeModules.RNHapticFeedback; |
| 79 | + |
| 80 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); |
| 81 | + |
| 82 | + trigger(HapticFeedbackTypes.selection); |
| 83 | + |
| 84 | + expect(warnSpy).toHaveBeenCalledWith( |
| 85 | + "RNReactNativeHapticFeedback is not available", |
| 86 | + ); |
| 87 | + |
| 88 | + warnSpy.mockRestore(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments