|
1 | 1 | import type { ServerMediaSignal } from '@rocket.chat/media-signaling'; |
| 2 | +import { Platform } from 'react-native'; |
2 | 3 |
|
3 | 4 | import { mediaCallsStateSignals } from './restApi'; |
4 | 5 |
|
5 | 6 | const mockSdkGet = jest.fn(); |
| 7 | +const mockSdkPost = jest.fn(); |
| 8 | + |
6 | 9 | jest.mock('./sdk', () => ({ |
7 | 10 | __esModule: true, |
8 | 11 | default: { |
9 | | - get: (...args: unknown[]) => mockSdkGet(...args) |
| 12 | + get: (...args: unknown[]) => mockSdkGet(...args), |
| 13 | + post: (...args: unknown[]) => mockSdkPost(...args) |
| 14 | + } |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('../notifications', () => ({ |
| 18 | + getDeviceToken: jest.fn() |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('../native/NativeVoip', () => ({ |
| 22 | + __esModule: true, |
| 23 | + default: { |
| 24 | + getLastVoipToken: jest.fn() |
10 | 25 | } |
11 | 26 | })); |
12 | 27 |
|
| 28 | +jest.mock('react-native-device-info', () => { |
| 29 | + const mock = require('react-native-device-info/jest/react-native-device-info-mock'); |
| 30 | + const getUniqueId = jest.fn(() => Promise.resolve('unique-device-id')); |
| 31 | + const defaultExport = { |
| 32 | + ...mock, |
| 33 | + getUniqueId |
| 34 | + }; |
| 35 | + return { |
| 36 | + __esModule: true, |
| 37 | + default: defaultExport, |
| 38 | + getUniqueId |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +function loadRegisterPushToken(platform: 'ios' | 'android' = 'android') { |
| 43 | + jest.resetModules(); |
| 44 | + Object.defineProperty(Platform, 'OS', { configurable: true, writable: true, value: platform }); |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 46 | + const notifications = require('../notifications'); |
| 47 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 48 | + const voipNative = require('../native/NativeVoip').default; |
| 49 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 50 | + const { registerPushToken } = require('./restApi'); |
| 51 | + return { |
| 52 | + // eslint-disable-next-line @typescript-eslint/consistent-type-imports |
| 53 | + registerPushToken: registerPushToken as typeof import('./restApi').registerPushToken, |
| 54 | + getDeviceToken: jest.mocked(notifications.getDeviceToken), |
| 55 | + getLastVoipToken: jest.mocked(voipNative.getLastVoipToken) |
| 56 | + }; |
| 57 | +} |
| 58 | + |
13 | 59 | describe('mediaCallsStateSignals', () => { |
14 | 60 | beforeEach(() => { |
15 | 61 | jest.clearAllMocks(); |
@@ -55,3 +101,86 @@ describe('mediaCallsStateSignals', () => { |
55 | 101 | expect(result.success).toBe(false); |
56 | 102 | }); |
57 | 103 | }); |
| 104 | + |
| 105 | +describe('registerPushToken', () => { |
| 106 | + const platformOsAtSuiteStart = Platform.OS; |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + Object.defineProperty(Platform, 'OS', { configurable: true, writable: true, value: platformOsAtSuiteStart }); |
| 110 | + }); |
| 111 | + |
| 112 | + beforeEach(() => { |
| 113 | + jest.clearAllMocks(); |
| 114 | + mockSdkPost.mockResolvedValue(undefined); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns early when there is no device push token', async () => { |
| 118 | + const { registerPushToken, getDeviceToken: getToken } = loadRegisterPushToken(); |
| 119 | + getToken.mockReturnValue(''); |
| 120 | + |
| 121 | + await registerPushToken(); |
| 122 | + |
| 123 | + expect(mockSdkPost).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('on iOS returns early when push token exists but VoIP token is missing', async () => { |
| 127 | + const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadRegisterPushToken('ios'); |
| 128 | + getToken.mockReturnValue('apns-token'); |
| 129 | + getVoip.mockReturnValue(''); |
| 130 | + |
| 131 | + await registerPushToken(); |
| 132 | + |
| 133 | + expect(mockSdkPost).not.toHaveBeenCalled(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('on Android still registers when VoIP token is missing', async () => { |
| 137 | + const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadRegisterPushToken('android'); |
| 138 | + getToken.mockReturnValue('fcm-token'); |
| 139 | + getVoip.mockReturnValue(''); |
| 140 | + |
| 141 | + await registerPushToken(); |
| 142 | + |
| 143 | + expect(mockSdkPost).toHaveBeenCalledTimes(1); |
| 144 | + expect(mockSdkPost).toHaveBeenCalledWith( |
| 145 | + 'push.token', |
| 146 | + expect.objectContaining({ |
| 147 | + id: 'unique-device-id', |
| 148 | + value: 'fcm-token', |
| 149 | + type: 'gcm', |
| 150 | + appName: expect.any(String) |
| 151 | + }) |
| 152 | + ); |
| 153 | + const payload = mockSdkPost.mock.calls[0][1] as Record<string, unknown>; |
| 154 | + expect(Object.prototype.hasOwnProperty.call(payload, 'voipToken')).toBe(false); |
| 155 | + }); |
| 156 | + |
| 157 | + it('dedupes when the same push and VoIP tokens are registered again', async () => { |
| 158 | + const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadRegisterPushToken('ios'); |
| 159 | + getToken.mockReturnValue('apns-token'); |
| 160 | + getVoip.mockReturnValue('voip-token'); |
| 161 | + |
| 162 | + await registerPushToken(); |
| 163 | + await registerPushToken(); |
| 164 | + |
| 165 | + expect(mockSdkPost).toHaveBeenCalledTimes(1); |
| 166 | + }); |
| 167 | + |
| 168 | + it('on iOS posts apn payload with voipToken when both tokens are present', async () => { |
| 169 | + const { registerPushToken, getDeviceToken: getToken, getLastVoipToken: getVoip } = loadRegisterPushToken('ios'); |
| 170 | + getToken.mockReturnValue('apns-token'); |
| 171 | + getVoip.mockReturnValue('voip-token'); |
| 172 | + |
| 173 | + await registerPushToken(); |
| 174 | + |
| 175 | + expect(mockSdkPost).toHaveBeenCalledWith( |
| 176 | + 'push.token', |
| 177 | + expect.objectContaining({ |
| 178 | + id: 'unique-device-id', |
| 179 | + value: 'apns-token', |
| 180 | + type: 'apn', |
| 181 | + appName: expect.any(String), |
| 182 | + voipToken: 'voip-token' |
| 183 | + }) |
| 184 | + ); |
| 185 | + }); |
| 186 | +}); |
0 commit comments