From 0a926b428524894bfd3ad73a70b2e39906e20261 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 22 Feb 2024 03:14:02 +0100 Subject: [PATCH 1/5] Make OpenApp a write command --- src/libs/API/types.ts | 4 ++-- src/libs/Middleware/SaveResponseInOnyx.ts | 4 ++-- src/libs/actions/App.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 7c7695ebef57..d9b1613973d1 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -10,6 +10,7 @@ type ApiRequest = ValueOf; const WRITE_COMMANDS = { DISMISS_REFERRAL_BANNER: 'DismissReferralBanner', UPDATE_PREFERRED_LOCALE: 'UpdatePreferredLocale', + OPEN_APP: 'OpenApp', RECONNECT_APP: 'ReconnectApp', OPEN_PROFILE: 'OpenProfile', HANDLE_RESTRICTED_EVENT: 'HandleRestrictedEvent', @@ -154,6 +155,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.DISMISS_REFERRAL_BANNER]: Parameters.DismissReferralBannerParams; [WRITE_COMMANDS.UPDATE_PREFERRED_LOCALE]: Parameters.UpdatePreferredLocaleParams; [WRITE_COMMANDS.RECONNECT_APP]: Parameters.ReconnectAppParams; + [WRITE_COMMANDS.OPEN_APP]: Parameters.OpenAppParams; [WRITE_COMMANDS.OPEN_PROFILE]: Parameters.OpenProfileParams; [WRITE_COMMANDS.HANDLE_RESTRICTED_EVENT]: Parameters.HandleRestrictedEventParams; [WRITE_COMMANDS.OPEN_REPORT]: Parameters.OpenReportParams; @@ -293,7 +295,6 @@ type WriteCommandParameters = { }; const READ_COMMANDS = { - OPEN_APP: 'OpenApp', OPEN_REIMBURSEMENT_ACCOUNT_PAGE: 'OpenReimbursementAccountPage', OPEN_WORKSPACE_VIEW: 'OpenWorkspaceView', GET_MAPBOX_ACCESS_TOKEN: 'GetMapboxAccessToken', @@ -327,7 +328,6 @@ const READ_COMMANDS = { type ReadCommand = ValueOf; type ReadCommandParameters = { - [READ_COMMANDS.OPEN_APP]: Parameters.OpenAppParams; [READ_COMMANDS.OPEN_REIMBURSEMENT_ACCOUNT_PAGE]: Parameters.OpenReimbursementAccountPageParams; [READ_COMMANDS.OPEN_WORKSPACE_VIEW]: EmptyObject; [READ_COMMANDS.GET_MAPBOX_ACCESS_TOKEN]: EmptyObject; diff --git a/src/libs/Middleware/SaveResponseInOnyx.ts b/src/libs/Middleware/SaveResponseInOnyx.ts index 8e357b0f2251..3b5cd60d84a4 100644 --- a/src/libs/Middleware/SaveResponseInOnyx.ts +++ b/src/libs/Middleware/SaveResponseInOnyx.ts @@ -1,4 +1,4 @@ -import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types'; +import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import * as MemoryOnlyKeys from '@userActions/MemoryOnlyKeys/MemoryOnlyKeys'; import * as OnyxUpdates from '@userActions/OnyxUpdates'; import CONST from '@src/CONST'; @@ -7,7 +7,7 @@ import type Middleware from './types'; // If we're executing any of these requests, we don't need to trigger our OnyxUpdates flow to update the current data even if our current value is out of // date because all these requests are updating the app to the most current state. -const requestsToIgnoreLastUpdateID: string[] = [READ_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.GET_MISSING_ONYX_MESSAGES]; +const requestsToIgnoreLastUpdateID: string[] = [WRITE_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.GET_MISSING_ONYX_MESSAGES]; const SaveResponseInOnyx: Middleware = (requestResponse, request) => requestResponse.then((response = {}) => { diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index d2c650d6f1c0..6442f2ec0eef 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -15,7 +15,7 @@ import type { ReconnectAppParams, UpdatePreferredLocaleParams, } from '@libs/API/parameters'; -import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; +import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import * as Browser from '@libs/Browser'; import DateUtils from '@libs/DateUtils'; import Log from '@libs/Log'; @@ -211,7 +211,7 @@ function openApp() { getPolicyParamsForOpenOrReconnect().then((policyParams: PolicyParamsForOpenOrReconnect) => { const params: OpenAppParams = {enablePriorityModeFilter: true, ...policyParams}; - API.read(READ_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true)); + API.write(WRITE_COMMANDS.OPEN_APP, params, getOnyxDataForOpenOrReconnect(true)); }); } From 456cc9fddf16708a673bf84d8c3b6cf9cf21870f Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Thu, 22 Feb 2024 03:38:30 +0100 Subject: [PATCH 2/5] Lint --- src/libs/HttpUtils.ts | 4 ++-- src/libs/Network/NetworkStore.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/HttpUtils.ts b/src/libs/HttpUtils.ts index cf35cb6c4d29..51418cffebe5 100644 --- a/src/libs/HttpUtils.ts +++ b/src/libs/HttpUtils.ts @@ -7,7 +7,7 @@ import type {RequestType} from '@src/types/onyx/Request'; import type Response from '@src/types/onyx/Response'; import * as NetworkActions from './actions/Network'; import * as UpdateRequired from './actions/UpdateRequired'; -import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS} from './API/types'; +import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from './API/types'; import * as ApiUtils from './ApiUtils'; import HttpsError from './Errors/HttpsError'; @@ -31,7 +31,7 @@ let cancellationController = new AbortController(); /** * The API commands that require the skew calculation */ -const addSkewList: string[] = [SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, READ_COMMANDS.OPEN_APP]; +const addSkewList: string[] = [SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, WRITE_COMMANDS.OPEN_APP]; /** * Regex to get API command from the command diff --git a/src/libs/Network/NetworkStore.ts b/src/libs/Network/NetworkStore.ts index 5b93c9adc11a..9f79064d5acc 100644 --- a/src/libs/Network/NetworkStore.ts +++ b/src/libs/Network/NetworkStore.ts @@ -1,5 +1,5 @@ import Onyx from 'react-native-onyx'; -import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS} from '@libs/API/types'; +import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; import ONYXKEYS from '@src/ONYXKEYS'; import type Credentials from '@src/types/onyx/Credentials'; @@ -96,7 +96,7 @@ function getAuthToken(): string | null { } function isSupportRequest(command: string): boolean { - return [READ_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT].some((cmd) => cmd === command); + return [WRITE_COMMANDS.OPEN_APP, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, SIDE_EFFECT_REQUEST_COMMANDS.OPEN_REPORT].some((cmd) => cmd === command); } function getSupportAuthToken(): string | null { From 1869a6b46739d86ffd403c05a4942363899a763d Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 25 Feb 2024 05:22:58 +0100 Subject: [PATCH 3/5] Fix test --- tests/unit/NetworkTest.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/NetworkTest.js b/tests/unit/NetworkTest.js index 9a90a100769f..487a86f5e4cc 100644 --- a/tests/unit/NetworkTest.js +++ b/tests/unit/NetworkTest.js @@ -4,6 +4,7 @@ import CONST from '../../src/CONST'; import * as App from '../../src/libs/actions/App'; import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager'; import * as PersistedRequests from '../../src/libs/actions/PersistedRequests'; +import * as PersonalDetails from '../../src/libs/actions/PersonalDetails'; import * as Session from '../../src/libs/actions/Session'; import HttpUtils from '../../src/libs/HttpUtils'; import Log from '../../src/libs/Log'; @@ -137,7 +138,7 @@ describe('NetworkTests', () => { HttpUtils.xhr = jest.fn(); HttpUtils.xhr - // And mock the first call to OpenApp return with an expired session code + // And mock the first call to openPersonalDetails return with an expired session code .mockImplementationOnce(() => Promise.resolve({ jsonCode: CONST.JSON_CODE.NOT_AUTHENTICATED, @@ -168,17 +169,17 @@ describe('NetworkTests', () => { // And then make 3 API requests in quick succession with an expired authToken and handle the response // It doesn't matter which requests these are really as all the response is mocked we just want to see // that we get re-authenticated - App.openApp(); - App.openApp(); - App.openApp(); + PersonalDetails.openPersonalDetails(); + PersonalDetails.openPersonalDetails(); + PersonalDetails.openPersonalDetails(); return waitForBatchedUpdates(); }) .then(() => { // We should expect to see the three calls to OpenApp, but only one call to Authenticate. // And we should also see the reconnection callbacks triggered. - const callsToOpenApp = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenApp'); + const callsToAReadCommand = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenPersonalDetailsPage'); const callsToAuthenticate = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'Authenticate'); - expect(callsToOpenApp.length).toBe(3); + expect(callsToAReadCommand.length).toBe(3); expect(callsToAuthenticate.length).toBe(1); expect(reconnectionCallbacksSpy.mock.calls.length).toBe(3); }); From 53feadb8022d33b5fa392523f01b27ad44ebadfd Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Sun, 25 Feb 2024 05:38:38 +0100 Subject: [PATCH 4/5] Fix another test --- tests/unit/NetworkTest.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/unit/NetworkTest.js b/tests/unit/NetworkTest.js index 487a86f5e4cc..0f2d927a4d66 100644 --- a/tests/unit/NetworkTest.js +++ b/tests/unit/NetworkTest.js @@ -1,7 +1,6 @@ import Onyx from 'react-native-onyx'; import _ from 'underscore'; import CONST from '../../src/CONST'; -import * as App from '../../src/libs/actions/App'; import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager'; import * as PersistedRequests from '../../src/libs/actions/PersistedRequests'; import * as PersonalDetails from '../../src/libs/actions/PersonalDetails'; @@ -102,8 +101,7 @@ describe('NetworkTests', () => { ); // This should first trigger re-authentication and then a Failed to fetch - App.confirmReadyToOpenApp(); - App.openApp(); + PersonalDetails.openPersonalDetails(); return waitForBatchedUpdates() .then(() => Onyx.set(ONYXKEYS.NETWORK, {isOffline: false})) .then(() => { @@ -114,11 +112,11 @@ describe('NetworkTests', () => { return waitForBatchedUpdates(); }) .then(() => { - // Then we will eventually have 3 calls to chatList and 2 calls to Authenticate - const callsToOpenApp = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenApp'); + // Then we will eventually have 1 call to OpenPersonalDetailsPage and 1 calls to Authenticate + const callsToOpenPersonalDetails = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenPersonalDetailsPage'); const callsToAuthenticate = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'Authenticate'); - expect(callsToOpenApp.length).toBe(1); + expect(callsToOpenPersonalDetails.length).toBe(1); expect(callsToAuthenticate.length).toBe(1); }); }); @@ -177,9 +175,9 @@ describe('NetworkTests', () => { .then(() => { // We should expect to see the three calls to OpenApp, but only one call to Authenticate. // And we should also see the reconnection callbacks triggered. - const callsToAReadCommand = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenPersonalDetailsPage'); + const callsToOpenPersonalDetails = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'OpenPersonalDetailsPage'); const callsToAuthenticate = _.filter(HttpUtils.xhr.mock.calls, ([command]) => command === 'Authenticate'); - expect(callsToAReadCommand.length).toBe(3); + expect(callsToOpenPersonalDetails.length).toBe(3); expect(callsToAuthenticate.length).toBe(1); expect(reconnectionCallbacksSpy.mock.calls.length).toBe(3); }); From e22fa7dead418cbf842f611b39bf8f19b6bad863 Mon Sep 17 00:00:00 2001 From: Youssef Lourayad Date: Tue, 27 Feb 2024 02:39:29 +0100 Subject: [PATCH 5/5] Update comment --- tests/unit/NetworkTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/NetworkTest.js b/tests/unit/NetworkTest.js index 0f2d927a4d66..29f5e344b35a 100644 --- a/tests/unit/NetworkTest.js +++ b/tests/unit/NetworkTest.js @@ -164,7 +164,7 @@ describe('NetworkTests', () => { }), ); - // And then make 3 API requests in quick succession with an expired authToken and handle the response + // And then make 3 API READ requests in quick succession with an expired authToken and handle the response // It doesn't matter which requests these are really as all the response is mocked we just want to see // that we get re-authenticated PersonalDetails.openPersonalDetails();