diff --git a/src/libs/actions/Session/index.ts b/src/libs/actions/Session/index.ts index c126543c499b..e50eba7e596f 100644 --- a/src/libs/actions/Session/index.ts +++ b/src/libs/actions/Session/index.ts @@ -6,6 +6,7 @@ import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import * as PersistedRequests from '@libs/actions/PersistedRequests'; +import {resolveDuplicationConflictAction} from '@libs/actions/RequestConflictUtils'; import * as API from '@libs/API'; import type { AuthenticatePusherParams, @@ -183,7 +184,14 @@ function signOut() { shouldRetry: false, }; - API.write(WRITE_COMMANDS.LOG_OUT, params); + API.write( + WRITE_COMMANDS.LOG_OUT, + params, + {}, + { + checkAndFixConflictingRequest: (persistedRequests) => resolveDuplicationConflictAction(persistedRequests, (request) => request.command === WRITE_COMMANDS.LOG_OUT), + }, + ); } /** diff --git a/tests/actions/SessionTest.ts b/tests/actions/SessionTest.ts index 0f23cef757da..b7113126a587 100644 --- a/tests/actions/SessionTest.ts +++ b/tests/actions/SessionTest.ts @@ -10,6 +10,7 @@ import PushNotification from '@libs/Notification/PushNotification'; import '@libs/Notification/PushNotification/subscribePushNotification'; import * as PersistedRequests from '@userActions/PersistedRequests'; import CONST from '@src/CONST'; +import * as SessionUtil from '@src/libs/actions/Session'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Credentials, Session} from '@src/types/onyx'; import * as TestHelper from '../utils/TestHelper'; @@ -183,4 +184,24 @@ describe('Session', () => { expect(PersistedRequests.getAll().length).toBe(0); }); + + test('LogOut should replace same requests from the queue instead of adding new one', async () => { + await TestHelper.signInWithTestUser(); + await Onyx.set(ONYXKEYS.NETWORK, {isOffline: true}); + + SessionUtil.signOut(); + SessionUtil.signOut(); + SessionUtil.signOut(); + SessionUtil.signOut(); + SessionUtil.signOut(); + + await waitForBatchedUpdates(); + + expect(PersistedRequests.getAll().length).toBe(1); + expect(PersistedRequests.getAll().at(0)?.command).toBe(WRITE_COMMANDS.LOG_OUT); + + await Onyx.set(ONYXKEYS.NETWORK, {isOffline: false}); + + expect(PersistedRequests.getAll().length).toBe(0); + }); });