|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + * |
| 4 | + * iOS-only mute tests: requires isIOS = true so didPerformSetMutedCallAction listener is registered. |
| 5 | + */ |
| 6 | +import { setupMediaCallEvents } from './MediaCallEvents'; |
| 7 | +import { useCallStore } from './useCallStore'; |
| 8 | + |
| 9 | +const mockAddEventListener = jest.fn(); |
| 10 | + |
| 11 | +jest.mock('../../methods/helpers', () => ({ |
| 12 | + ...jest.requireActual('../../methods/helpers'), |
| 13 | + isIOS: true |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('./useCallStore', () => ({ |
| 17 | + useCallStore: { |
| 18 | + getState: jest.fn() |
| 19 | + } |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../../store', () => ({ |
| 23 | + __esModule: true, |
| 24 | + default: { |
| 25 | + dispatch: jest.fn() |
| 26 | + } |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('../../native/NativeVoip', () => ({ |
| 30 | + __esModule: true, |
| 31 | + default: { |
| 32 | + clearInitialEvents: jest.fn(), |
| 33 | + getInitialEvents: jest.fn(() => null) |
| 34 | + } |
| 35 | +})); |
| 36 | + |
| 37 | +jest.mock('./MediaSessionInstance', () => ({ |
| 38 | + mediaSessionInstance: { |
| 39 | + endCall: jest.fn() |
| 40 | + } |
| 41 | +})); |
| 42 | + |
| 43 | +jest.mock('../restApi', () => ({ |
| 44 | + registerPushToken: jest.fn(() => Promise.resolve()) |
| 45 | +})); |
| 46 | + |
| 47 | +jest.mock('react-native-callkeep', () => ({ |
| 48 | + __esModule: true, |
| 49 | + default: { |
| 50 | + addEventListener: (...args: unknown[]) => mockAddEventListener(...args), |
| 51 | + clearInitialEvents: jest.fn(), |
| 52 | + setCurrentCallActive: jest.fn(), |
| 53 | + getInitialEvents: jest.fn(() => Promise.resolve([])) |
| 54 | + } |
| 55 | +})); |
| 56 | + |
| 57 | +const activeCallBase = { |
| 58 | + call: {} as object, |
| 59 | + callId: 'uuid-1', |
| 60 | + nativeAcceptedCallId: null as string | null |
| 61 | +}; |
| 62 | + |
| 63 | +function getMuteHandler(): (payload: { muted: boolean; callUUID: string }) => void { |
| 64 | + const call = mockAddEventListener.mock.calls.find(([name]) => name === 'didPerformSetMutedCallAction'); |
| 65 | + if (!call) { |
| 66 | + throw new Error('didPerformSetMutedCallAction listener not registered'); |
| 67 | + } |
| 68 | + return call[1] as (payload: { muted: boolean; callUUID: string }) => void; |
| 69 | +} |
| 70 | + |
| 71 | +describe('setupMediaCallEvents — didPerformSetMutedCallAction (iOS)', () => { |
| 72 | + const toggleMute = jest.fn(); |
| 73 | + const getState = useCallStore.getState as jest.Mock; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + jest.clearAllMocks(); |
| 77 | + toggleMute.mockClear(); |
| 78 | + mockAddEventListener.mockImplementation(() => ({ remove: jest.fn() })); |
| 79 | + getState.mockReturnValue({ ...activeCallBase, isMuted: false, toggleMute }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('registers didPerformSetMutedCallAction via RNCallKeep.addEventListener', () => { |
| 83 | + setupMediaCallEvents(); |
| 84 | + expect(mockAddEventListener).toHaveBeenCalledWith('didPerformSetMutedCallAction', expect.any(Function)); |
| 85 | + }); |
| 86 | + |
| 87 | + it('calls toggleMute when muted state differs from OS and UUIDs match', () => { |
| 88 | + setupMediaCallEvents(); |
| 89 | + getMuteHandler()({ muted: true, callUUID: 'uuid-1' }); |
| 90 | + expect(toggleMute).toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('does not call toggleMute when muted state already matches OS even if UUIDs match', () => { |
| 94 | + getState.mockReturnValue({ ...activeCallBase, isMuted: true, toggleMute }); |
| 95 | + setupMediaCallEvents(); |
| 96 | + getMuteHandler()({ muted: true, callUUID: 'uuid-1' }); |
| 97 | + expect(toggleMute).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('drops event when callUUID does not match active call id', () => { |
| 101 | + setupMediaCallEvents(); |
| 102 | + getMuteHandler()({ muted: true, callUUID: 'uuid-2' }); |
| 103 | + expect(toggleMute).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('drops event when there is no active call object even if UUIDs match', () => { |
| 107 | + getState.mockReturnValue({ |
| 108 | + call: null, |
| 109 | + callId: 'uuid-1', |
| 110 | + nativeAcceptedCallId: null, |
| 111 | + isMuted: false, |
| 112 | + toggleMute |
| 113 | + }); |
| 114 | + setupMediaCallEvents(); |
| 115 | + getMuteHandler()({ muted: true, callUUID: 'uuid-1' }); |
| 116 | + expect(toggleMute).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | +}); |
0 commit comments