From 90a499240fede48cc1ac3eca4dfa329231fd5362 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Wed, 11 Oct 2023 16:38:04 +0200 Subject: [PATCH 1/4] remove parseISO --- src/libs/ReportActionsUtils.js | 7 ++++--- src/libs/ReportUtils.js | 4 ++-- src/libs/TransactionUtils.ts | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libs/ReportActionsUtils.js b/src/libs/ReportActionsUtils.js index 258582d9f653..aa8b9cb2c516 100644 --- a/src/libs/ReportActionsUtils.js +++ b/src/libs/ReportActionsUtils.js @@ -1,7 +1,7 @@ /* eslint-disable rulesdir/prefer-underscore-method */ import lodashGet from 'lodash/get'; import _ from 'underscore'; -import {max, parseISO, isEqual} from 'date-fns'; +import {max, isEqual} from 'date-fns'; import lodashFindLast from 'lodash/findLast'; import Onyx from 'react-native-onyx'; import * as CollectionUtils from './CollectionUtils'; @@ -400,8 +400,9 @@ function getLastVisibleAction(reportID, actionsToMerge = {}) { if (visibleActions.length === 0) { return {}; } - const maxDate = max(visibleActions.map((action) => parseISO(action.created))); - const maxAction = visibleActions.find((action) => isEqual(parseISO(action.created), maxDate)); + + const maxDate = max(visibleActions.map((action) => new Date(action.created))); + const maxAction = visibleActions.find((action) => isEqual(new Date(action.created), maxDate)); return maxAction; } diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index d688bc544fec..924212e66197 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -1,6 +1,6 @@ /* eslint-disable rulesdir/prefer-underscore-method */ import _ from 'underscore'; -import {format, parseISO} from 'date-fns'; +import {format} from 'date-fns'; import Str from 'expensify-common/lib/str'; import lodashGet from 'lodash/get'; import lodashIntersection from 'lodash/intersection'; @@ -1640,7 +1640,7 @@ function getModifiedExpenseMessage(reportAction) { const hasModifiedCreated = _.has(reportActionOriginalMessage, 'oldCreated') && _.has(reportActionOriginalMessage, 'created'); if (hasModifiedCreated) { // Take only the YYYY-MM-DD value as the original date includes timestamp - let formattedOldCreated = parseISO(reportActionOriginalMessage.oldCreated); + let formattedOldCreated = new Date(reportActionOriginalMessage.oldCreated); formattedOldCreated = format(formattedOldCreated, CONST.DATE.FNS_FORMAT_STRING); return getProperSchemaForModifiedExpenseMessage(reportActionOriginalMessage.created, formattedOldCreated, Localize.translateLocal('common.date'), false); } diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index b5b8271c35a3..7c2b407ecb8e 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -1,5 +1,5 @@ import Onyx, {OnyxCollection} from 'react-native-onyx'; -import {format, parseISO, isValid} from 'date-fns'; +import {format, isValid} from 'date-fns'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; import DateUtils from './DateUtils'; @@ -263,7 +263,7 @@ function getTag(transaction: Transaction): string { */ function getCreated(transaction: Transaction): string { const created = transaction?.modifiedCreated ? transaction.modifiedCreated : transaction?.created || ''; - const createdDate = parseISO(created); + const createdDate = new Date(created); if (isValid(createdDate)) { return format(createdDate, CONST.DATE.FNS_FORMAT_STRING); } From db0b38396906cfdc37224ca4b2e760b5ee4a8ef0 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Thu, 19 Oct 2023 13:56:44 +0200 Subject: [PATCH 2/4] added test --- tests/unit/ReportActionsUtilsTest.js | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/unit/ReportActionsUtilsTest.js b/tests/unit/ReportActionsUtilsTest.js index 71d46fb5a3ea..ecfb30f5fefb 100644 --- a/tests/unit/ReportActionsUtilsTest.js +++ b/tests/unit/ReportActionsUtilsTest.js @@ -1,7 +1,32 @@ +import Onyx from 'react-native-onyx'; import CONST from '../../src/CONST'; +import ONYXKEYS from '../../src/ONYXKEYS'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; +import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; import * as ReportActionsUtils from '../../src/libs/ReportActionsUtils'; +import * as LHNTestUtils from '../utils/LHNTestUtils'; describe('ReportActionsUtils', () => { + beforeAll(() => + Onyx.init({ + keys: ONYXKEYS, + registerStorageEventListener: () => {}, + safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], + }), + ); + + beforeEach(() => { + // Wrap Onyx each onyx action with waitForBatchedUpdates + wrapOnyxWithWaitForBatchedUpdates(Onyx); + // Initialize the network key for OfflineWithFeedback + return Onyx.merge(ONYXKEYS.NETWORK, {isOffline: false}); + }); + + // Clear out Onyx after each test so that each test starts with a clean slate + afterEach(() => { + Onyx.clear(); + }); + describe('getSortedReportActions', () => { const cases = [ [ @@ -229,4 +254,66 @@ describe('ReportActionsUtils', () => { expect(result).toStrictEqual(input); }); }); + + describe('getLastVisibleAction', () => { + it('should return the last visible action for a report', () => { + const report = { + ...LHNTestUtils.getFakeReport(['email1@test.com', 'email2@test.com'], 3, true), + reportID: 1, + }; + const action = { + ...LHNTestUtils.getFakeReportAction('email1@test.com', 3, true), + created: '2023-08-01 16:00:00', + reportActionID: 'action1', + actionName: 'ADDCOMMENT', + originalMessage: { + policyName: 'Vikings Policy', + reason: 'policyDeleted', + }, + message: { + policyName: 'Vikings Policy', + reason: 'policyDeleted', + }, + }; + const action2 = { + ...LHNTestUtils.getFakeReportAction('email2@test.com', 3, true), + created: '2023-08-01 18:00:00', + reportActionID: 'action2', + actionName: 'ADDCOMMENT', + originalMessage: { + policyName: 'Vikings Policy', + reason: 'policyDeleted', + }, + message: { + policyName: 'Vikings Policy', + reason: 'policyDeleted', + }, + }; + return ( + waitForBatchedUpdates() + // When Onyx is updated with the data and the sidebar re-renders + .then(() => + Onyx.multiSet({ + [`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]: report, + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`]: {[action.reportActionID]: action, [action2.reportActionID]: action2}, + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, + waitForCollectionCallback: true, + callback: (actions) => { + Onyx.disconnect(connectionID); + const res = ReportActionsUtils.getLastVisibleAction(report.reportID); + expect(res).toBe(action2); + resolve(); + }, + }); + }), + ) + ); + }); + }); }); From 1b3367105ed924e6218fd9bfa99beca987e5429c Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Thu, 19 Oct 2023 16:11:25 +0200 Subject: [PATCH 3/4] move to ts --- src/libs/ReportActionsUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 16260e0edea6..f0c1458f04f3 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1,4 +1,4 @@ -import {isEqual, max, parseISO} from 'date-fns'; +import {isEqual, max} from 'date-fns'; import _ from 'lodash'; import lodashFindLast from 'lodash/findLast'; import Onyx, {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; @@ -382,8 +382,8 @@ function getLastVisibleAction(reportID: string, actionsToMerge: ReportActions = if (visibleActions.length === 0) { return null; } - const maxDate = max(visibleActions.map((action) => parseISO(action.created))); - const maxAction = visibleActions.find((action) => isEqual(parseISO(action.created), maxDate)); + const maxDate = max(visibleActions.map((action) => new Date(action.created))); + const maxAction = visibleActions.find((action) => isEqual(new Date(action.created), maxDate)); return maxAction ?? null; } From c0b98df5982fad3990a6971d219718d04678e5dd Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Thu, 19 Oct 2023 16:33:08 +0200 Subject: [PATCH 4/4] lint fix --- tests/unit/ReportActionsUtilsTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ReportActionsUtilsTest.js b/tests/unit/ReportActionsUtilsTest.js index ecfb30f5fefb..2f02203bad64 100644 --- a/tests/unit/ReportActionsUtilsTest.js +++ b/tests/unit/ReportActionsUtilsTest.js @@ -304,7 +304,7 @@ describe('ReportActionsUtils', () => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, waitForCollectionCallback: true, - callback: (actions) => { + callback: () => { Onyx.disconnect(connectionID); const res = ReportActionsUtils.getLastVisibleAction(report.reportID); expect(res).toBe(action2);