Skip to content

Commit

Permalink
Merge pull request #29324 from waterim/feat-29271-remove-parseISO
Browse files Browse the repository at this point in the history
Feat: Remove parseISO
  • Loading branch information
mountiny authored Oct 19, 2023
2 parents 84be670 + c0b98df commit 26c9d84
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -1770,7 +1770,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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -310,7 +310,7 @@ function getTag(transaction: Transaction): string {
*/
function getCreated(transaction: Transaction, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING): string {
const created = transaction?.modifiedCreated ? transaction.modifiedCreated : transaction?.created || '';
const createdDate = parseISO(created);
const createdDate = new Date(created);
if (isValid(createdDate)) {
return format(createdDate, dateFormat);
}
Expand Down
87 changes: 87 additions & 0 deletions tests/unit/ReportActionsUtilsTest.js
Original file line number Diff line number Diff line change
@@ -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 = [
[
Expand Down Expand Up @@ -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(['[email protected]', '[email protected]'], 3, true),
reportID: 1,
};
const action = {
...LHNTestUtils.getFakeReportAction('[email protected]', 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('[email protected]', 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: () => {
Onyx.disconnect(connectionID);
const res = ReportActionsUtils.getLastVisibleAction(report.reportID);
expect(res).toBe(action2);
resolve();
},
});
}),
)
);
});
});
});

0 comments on commit 26c9d84

Please sign in to comment.