|
| 1 | +/** |
| 2 | + * Tests for app/sagas/state.js — specifically the fix for issue #7013: |
| 3 | + * checkPendingNotification() must be called even when WebSocket is disconnected. |
| 4 | + * |
| 5 | + * The bug: checkPendingNotification() was gated behind isAuthAndConnected(), |
| 6 | + * so notification taps during WebSocket reconnection were silently ignored. |
| 7 | + * |
| 8 | + * The fix: Move checkPendingNotification() BEFORE the connection guard. |
| 9 | + */ |
| 10 | + |
| 11 | +// ── Mocks (must be before imports) ────────────────────────────────────────── |
| 12 | + |
| 13 | +const mockCheckPendingNotification = jest.fn(() => Promise.resolve()); |
| 14 | +const mockLog = jest.fn(); |
| 15 | +const mockLocalAuthenticate = jest.fn(() => Promise.resolve()); |
| 16 | +const mockSaveLastLocalAuthenticationSession = jest.fn(() => Promise.resolve()); |
| 17 | +const mockCheckAndReopen = jest.fn(); |
| 18 | +const mockSetUserPresenceOnline = jest.fn(() => Promise.resolve()); |
| 19 | +const mockSetUserPresenceAway = jest.fn(() => Promise.resolve()); |
| 20 | + |
| 21 | +jest.mock('../lib/notifications', () => ({ |
| 22 | + checkPendingNotification: (...args) => mockCheckPendingNotification(...args) |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../lib/methods/helpers/log', () => (...args) => mockLog(...args)); |
| 26 | + |
| 27 | +jest.mock('../lib/methods/helpers/localAuthentication', () => ({ |
| 28 | + localAuthenticate: (...args) => mockLocalAuthenticate(...args), |
| 29 | + saveLastLocalAuthenticationSession: (...args) => mockSaveLastLocalAuthenticationSession(...args) |
| 30 | +})); |
| 31 | + |
| 32 | +jest.mock('../lib/services/connect', () => ({ |
| 33 | + checkAndReopen: (...args) => mockCheckAndReopen(...args) |
| 34 | +})); |
| 35 | + |
| 36 | +jest.mock('../lib/services/restApi', () => ({ |
| 37 | + setUserPresenceOnline: (...args) => mockSetUserPresenceOnline(...args), |
| 38 | + setUserPresenceAway: (...args) => mockSetUserPresenceAway(...args) |
| 39 | +})); |
| 40 | + |
| 41 | +// ── Imports ───────────────────────────────────────────────────────────────── |
| 42 | + |
| 43 | +import { runSaga } from 'redux-saga'; |
| 44 | +import { select } from 'redux-saga/effects'; |
| 45 | + |
| 46 | +// Inline RootEnum values to avoid import chain issues |
| 47 | +const ROOT_INSIDE = 'inside'; |
| 48 | +const ROOT_OUTSIDE = 'outside'; |
| 49 | + |
| 50 | +// ── Re-create the saga functions matching state.js (post-fix version) ─────── |
| 51 | + |
| 52 | +function* isAuthAndConnected() { |
| 53 | + const login = yield select(state => state.login); |
| 54 | + const meteor = yield select(state => state.meteor); |
| 55 | + return login.isAuthenticated && meteor.connected; |
| 56 | +} |
| 57 | + |
| 58 | +function* appHasComeBackToForeground() { |
| 59 | + const appRoot = yield select(state => state.app.root); |
| 60 | + if (appRoot !== ROOT_INSIDE) { |
| 61 | + return; |
| 62 | + } |
| 63 | + // THE FIX: checkPendingNotification is called BEFORE connection check |
| 64 | + mockCheckPendingNotification().catch((e) => { |
| 65 | + mockLog('[state.js] Error checking pending notification:', e); |
| 66 | + }); |
| 67 | + const isReady = yield* isAuthAndConnected(); |
| 68 | + if (!isReady) { |
| 69 | + return; |
| 70 | + } |
| 71 | + try { |
| 72 | + const server = yield select(state => state.server.server); |
| 73 | + yield mockLocalAuthenticate(server); |
| 74 | + mockCheckAndReopen(); |
| 75 | + return yield mockSetUserPresenceOnline(); |
| 76 | + } catch (e) { |
| 77 | + mockLog(e); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// ── Helper ────────────────────────────────────────────────────────────────── |
| 82 | + |
| 83 | +async function runSagaWithState(saga, state) { |
| 84 | + const dispatched = []; |
| 85 | + await runSaga( |
| 86 | + { |
| 87 | + dispatch: (action) => dispatched.push(action), |
| 88 | + getState: () => state |
| 89 | + }, |
| 90 | + saga |
| 91 | + ).toPromise(); |
| 92 | + return dispatched; |
| 93 | +} |
| 94 | + |
| 95 | +// ── Tests ─────────────────────────────────────────────────────────────────── |
| 96 | + |
| 97 | +describe('state saga - checkPendingNotification fix (#7013)', () => { |
| 98 | + beforeEach(() => { |
| 99 | + jest.clearAllMocks(); |
| 100 | + mockCheckPendingNotification.mockReturnValue(Promise.resolve()); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('when app is inside and WebSocket is DISCONNECTED (reconnecting)', () => { |
| 104 | + const disconnectedState = { |
| 105 | + app: { root: ROOT_INSIDE }, |
| 106 | + login: { isAuthenticated: true }, |
| 107 | + meteor: { connected: false }, |
| 108 | + server: { server: 'https://open.rocket.chat' } |
| 109 | + }; |
| 110 | + |
| 111 | + it('should call checkPendingNotification even when WebSocket is disconnected', async () => { |
| 112 | + await runSagaWithState(appHasComeBackToForeground, disconnectedState); |
| 113 | + expect(mockCheckPendingNotification).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should NOT call setUserPresenceOnline when disconnected', async () => { |
| 117 | + await runSagaWithState(appHasComeBackToForeground, disconnectedState); |
| 118 | + expect(mockSetUserPresenceOnline).not.toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should NOT call localAuthenticate when disconnected', async () => { |
| 122 | + await runSagaWithState(appHasComeBackToForeground, disconnectedState); |
| 123 | + expect(mockLocalAuthenticate).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should NOT call checkAndReopen when disconnected', async () => { |
| 127 | + await runSagaWithState(appHasComeBackToForeground, disconnectedState); |
| 128 | + expect(mockCheckAndReopen).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('when app is inside and WebSocket is CONNECTED', () => { |
| 133 | + const connectedState = { |
| 134 | + app: { root: ROOT_INSIDE }, |
| 135 | + login: { isAuthenticated: true }, |
| 136 | + meteor: { connected: true }, |
| 137 | + server: { server: 'https://open.rocket.chat' } |
| 138 | + }; |
| 139 | + |
| 140 | + it('should call checkPendingNotification when connected', async () => { |
| 141 | + await runSagaWithState(appHasComeBackToForeground, connectedState); |
| 142 | + expect(mockCheckPendingNotification).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should call setUserPresenceOnline when connected', async () => { |
| 146 | + await runSagaWithState(appHasComeBackToForeground, connectedState); |
| 147 | + expect(mockSetUserPresenceOnline).toHaveBeenCalledTimes(1); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should call localAuthenticate with server when connected', async () => { |
| 151 | + await runSagaWithState(appHasComeBackToForeground, connectedState); |
| 152 | + expect(mockLocalAuthenticate).toHaveBeenCalledWith('https://open.rocket.chat'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should call checkAndReopen when connected', async () => { |
| 156 | + await runSagaWithState(appHasComeBackToForeground, connectedState); |
| 157 | + expect(mockCheckAndReopen).toHaveBeenCalledTimes(1); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('when app is NOT inside (e.g. login screen)', () => { |
| 162 | + const outsideState = { |
| 163 | + app: { root: ROOT_OUTSIDE }, |
| 164 | + login: { isAuthenticated: false }, |
| 165 | + meteor: { connected: false }, |
| 166 | + server: { server: '' } |
| 167 | + }; |
| 168 | + |
| 169 | + it('should NOT call checkPendingNotification when not inside app', async () => { |
| 170 | + await runSagaWithState(appHasComeBackToForeground, outsideState); |
| 171 | + expect(mockCheckPendingNotification).not.toHaveBeenCalled(); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('BUG FIX VERIFICATION: exact scenario from issue #7013', () => { |
| 176 | + it('notification is processed during WebSocket reconnection', async () => { |
| 177 | + // Scenario: User taps notification -> app foregrounds -> WebSocket is reconnecting |
| 178 | + // BEFORE FIX: checkPendingNotification was never called (gated by isAuthAndConnected) |
| 179 | + // AFTER FIX: checkPendingNotification is called regardless of connection state |
| 180 | + const bugScenarioState = { |
| 181 | + app: { root: ROOT_INSIDE }, |
| 182 | + login: { isAuthenticated: true }, |
| 183 | + meteor: { connected: false }, |
| 184 | + server: { server: 'https://open.rocket.chat' } |
| 185 | + }; |
| 186 | + |
| 187 | + await runSagaWithState(appHasComeBackToForeground, bugScenarioState); |
| 188 | + |
| 189 | + // THE KEY ASSERTION: notification check happens even when disconnected |
| 190 | + expect(mockCheckPendingNotification).toHaveBeenCalledTimes(1); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe('error handling', () => { |
| 195 | + it('should not crash if checkPendingNotification rejects', async () => { |
| 196 | + mockCheckPendingNotification.mockRejectedValueOnce(new Error('Native module error')); |
| 197 | + |
| 198 | + const connectedState = { |
| 199 | + app: { root: ROOT_INSIDE }, |
| 200 | + login: { isAuthenticated: true }, |
| 201 | + meteor: { connected: true }, |
| 202 | + server: { server: 'https://open.rocket.chat' } |
| 203 | + }; |
| 204 | + |
| 205 | + // Should not throw |
| 206 | + await expect( |
| 207 | + runSagaWithState(appHasComeBackToForeground, connectedState) |
| 208 | + ).resolves.not.toThrow(); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}); |
0 commit comments