Skip to content

Commit

Permalink
Refactor the save method in PersistedRequests, refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WojtekBoman committed Nov 16, 2023
1 parent 0498a0d commit bed4e19
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
15 changes: 2 additions & 13 deletions src/libs/actions/PersistedRequests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import isEqual from 'lodash/isEqual';
import mergeWith from 'lodash/mergeWith';
import Onyx, {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import {Request} from '@src/types/onyx';

Expand All @@ -18,22 +17,12 @@ function clear() {
return Onyx.set(ONYXKEYS.PERSISTED_REQUESTS, []);
}

function createUpdatedRequest(oldRequest: Request, newRequest: Request): Request {
// Merge the requests together, but concat Onyx update arrays together
return mergeWith(oldRequest, newRequest, (objValue, srcValue) => {
if (!Array.isArray(objValue) || !objValue.some((obj) => 'onyxMethod' in obj)) {
return;
}
return (objValue as OnyxUpdate[]).concat(srcValue);
});
}

function save(requestToPersist: Request) {
// Check for a request w/ matching idempotencyKey in the queue
const existingRequestIndex = persistedRequests.findIndex((request) => request.data?.idempotencyKey && request.data?.idempotencyKey === requestToPersist.data?.idempotencyKey);
if (existingRequestIndex > -1) {
// Merge the new request into the existing one, keeping its place in the queue
persistedRequests.splice(existingRequestIndex, 1, createUpdatedRequest(persistedRequests[existingRequestIndex], requestToPersist));
persistedRequests.splice(existingRequestIndex, 1, requestToPersist);
} else {
// If not, push the new request to the end of the queue
persistedRequests.push(requestToPersist);
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p

// Add the createdReportActionID parameter to the API call
params.createdReportActionID = optimisticCreatedAction.reportActionID;
params.idempotencyKey = `${params.idempotencyKey}_NewReport_${optimisticCreatedAction.reportActionID}`;

// If we are creating a thread, ensure the report action has childReportID property added
if (newReportObject.parentReportID && parentReportActionID) {
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/PersistedRequestsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ afterEach(() => {
});

describe('PersistedRequests', () => {
it('save a new request with an idempotency key which currently exists in PersistedRequests', () => {
it('save a new request with an idempotency key which currently exists in the PersistedRequests array', () => {
PersistedRequests.save(request);
expect(PersistedRequests.getAll().length).toBe(1);
});
Expand All @@ -36,7 +36,7 @@ describe('PersistedRequests', () => {
expect(PersistedRequests.getAll().length).toBe(2);
});

it('merge a new request with one existing in PersistedRequests array', () => {
it('replace a request existing in the PersistedRequests array with a new one', () => {
const newRequest: Request = {
command: 'OpenReport',
data: {
Expand All @@ -54,8 +54,10 @@ describe('PersistedRequests', () => {

const mergedRequest = persistedRequests[0];

expect(mergedRequest.successData?.length).toBe(2);
expect(mergedRequest.failureData?.length).toBe(2);
expect(mergedRequest.successData?.length).toBe(1);
expect(mergedRequest.failureData?.length).toBe(1);
expect(mergedRequest.successData?.[0]?.key).toBe('reportMetadata_3');
expect(mergedRequest.failureData?.[0]?.key).toBe('reportMetadata_4');
});

it('remove a request from the PersistedRequests array', () => {
Expand Down

0 comments on commit bed4e19

Please sign in to comment.