Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Remove parseISO #29324

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1757,7 +1757,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();
},
});
}),
)
);
});
});
});
Loading