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

[No QA] Migrate 'OptionsListUtilsTest.js', 'DateUtilsTest.js', 'SidebarLinks.perf-test.js', 'markdown.js' and 'ReportUtilsTest.js' to Typescript #37206

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
79d9495
refactor(typescript): migrate multiple tests and utils
pac-guerreiro Feb 26, 2024
1b2af89
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Feb 26, 2024
0bd0ce5
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Feb 28, 2024
c66339c
refactor(typescript): apply pull request suggestions
pac-guerreiro Feb 29, 2024
947c99f
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Feb 29, 2024
2a76abb
refactor(typescript): resolve type error
pac-guerreiro Feb 29, 2024
76f4e2e
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 11, 2024
a5131ed
refactor(typescript): resolve type issues
pac-guerreiro Mar 12, 2024
d78e9d1
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 13, 2024
e5d3ef0
fix: wrong data type conversion
pac-guerreiro Mar 13, 2024
16854a9
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 14, 2024
c5b2ada
refactor(typescript): apply pull request suggestions
pac-guerreiro Mar 14, 2024
104f838
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 18, 2024
f051c2e
refactor(typescript): apply pull request suggestion
pac-guerreiro Mar 18, 2024
3e40a53
refactor(typescript): apply pull request feedback
pac-guerreiro Mar 20, 2024
5685095
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 22, 2024
ba09987
refactor(typescript): apply pull request suggestion
pac-guerreiro Mar 22, 2024
9ab0ea3
refactor(typescript): apply pull request feedback
pac-guerreiro Mar 22, 2024
dc6ede9
Merge branch 'main' into pac-guerreiro/refactor/migrate-25309-25310-2…
pac-guerreiro Mar 26, 2024
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
2 changes: 1 addition & 1 deletion src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function formatToLongDateWithWeekday(datetime: string | Date): string {
*
* @returns Sunday
*/
function formatToDayOfWeek(datetime: Date): string {
function formatToDayOfWeek(datetime: string | Date): string {
pac-guerreiro marked this conversation as resolved.
Show resolved Hide resolved
return format(new Date(datetime), CONST.DATE.WEEKDAY_TIME_FORMAT);
pac-guerreiro marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
19 changes: 19 additions & 0 deletions src/types/utils/CollectionDataSet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import type {OnyxEntry} from 'react-native-onyx';
import type {OnyxCollectionKey, OnyxCollectionValuesMapping} from '@src/ONYXKEYS';

/** Helps with typing a collection item update inside Onyx.multiSet call */
type CollectionDataSet<TCollectionKey extends OnyxCollectionKey> = Record<`${TCollectionKey}${string}`, OnyxCollectionValuesMapping[TCollectionKey]>;

const toCollectionDataSet = <TCollectionKey extends OnyxCollectionKey>(
collectionKey: TCollectionKey,
collection: Array<OnyxEntry<OnyxCollectionValuesMapping[TCollectionKey]>>,
idSelector: (collectionValue: OnyxCollectionValuesMapping[TCollectionKey]) => string,
) => {
const collectionDataSet = collection.reduce<CollectionDataSet<TCollectionKey>>((result, collectionValue) => {
if (collectionValue) {
// eslint-disable-next-line no-param-reassign
result[`${collectionKey}${idSelector(collectionValue)}`] = collectionValue;
}
return result;
}, {} as CollectionDataSet<TCollectionKey>);

return collectionDataSet;
};

export default CollectionDataSet;

export {toCollectionDataSet};
5 changes: 2 additions & 3 deletions tests/perf-test/SidebarLinks.perf-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ jest.mock('@components/Icon/Expensicons');
jest.mock('@react-navigation/native');

const getMockedReportsMap = (length = 100) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const mockReports = Array.from({length}, (_, i) => {
const reportID = i + 1;
const mockReports = Array.from({length}, (value, index) => {
const reportID = index + 1;
const participants = [1, 2];
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
const report = LHNTestUtils.getFakeReport(participants, 1, true);
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/OptionsListUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ describe('OptionsListUtils', () => {
if (!ReportUtils.canUserPerformWriteAction(report) || ReportUtils.isExpensifyOnlyParticipantInReport(report)) {
return reports;
}
return {...reports, [reportKey]: report};
// eslint-disable-next-line no-param-reassign
reports[reportKey] = report;
return reports;
pac-guerreiro marked this conversation as resolved.
Show resolved Hide resolved
}, {});

// When we pass an empty search value
Expand Down
92 changes: 47 additions & 45 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report} from '@src/types/onyx';
import type {Policy, Report, ReportAction} from '@src/types/onyx';
import {toCollectionDataSet} from '@src/types/utils/CollectionDataSet';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

Expand Down Expand Up @@ -45,25 +47,26 @@ const participantsPersonalDetails = {
},
} as const;

const policy = {
const policy: Policy = {
id: '1',
name: 'Vikings Policy',
role: 'user',
type: 'free',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
} as const;
};

Onyx.init({keys: ONYXKEYS});

describe('ReportUtils', () => {
beforeAll(() => {
const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [policy], (current) => current.id);
Onyx.multiSet({
[ONYXKEYS.PERSONAL_DETAILS_LIST]: participantsPersonalDetails,
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID},
[ONYXKEYS.COUNTRY_CODE]: 1,
[`${ONYXKEYS.COLLECTION.POLICY}${policy.id}` as const]: policy,
...policyCollectionDataSet,
});
return waitForBatchedUpdates();
});
Expand Down Expand Up @@ -268,14 +271,14 @@ describe('ReportUtils', () => {
});
it('returns false when the matched IOU report does not have an owner accountID', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
ownerAccountID: undefined,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the linked iou report has an oustanding IOU', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
iouReportID: '1',
};
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}1`, {
Expand All @@ -287,38 +290,38 @@ describe('ReportUtils', () => {
});
it('returns false when the report has no outstanding IOU but is waiting for a bank account and the logged user is the report owner', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
ownerAccountID: currentUserAccountID,
isWaitingOnBankAccount: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the report has outstanding IOU and is not waiting for a bank account and the logged user is the report owner', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
ownerAccountID: currentUserAccountID,
isWaitingOnBankAccount: false,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the report has no oustanding IOU but is waiting for a bank account and the logged user is not the report owner', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
ownerAccountID: 97,
isWaitingOnBankAccount: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns true when the report has an unread mention', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
isUnreadWithMention: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(true);
});
it('returns true when the report is an outstanding task', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.TASK,
managerID: currentUserAccountID,
isUnreadWithMention: false,
Expand All @@ -329,7 +332,7 @@ describe('ReportUtils', () => {
});
it('returns true when the report has oustanding child request', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
ownerAccountID: 99,
hasOutstandingChildRequest: true,
isWaitingOnBankAccount: false,
Expand Down Expand Up @@ -363,7 +366,7 @@ describe('ReportUtils', () => {

it('it is a room with no participants except self', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, null, [currentUserAccountID]);
Expand All @@ -372,7 +375,7 @@ describe('ReportUtils', () => {

it('its not your policy expense chat', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: false,
};
Expand All @@ -382,7 +385,7 @@ describe('ReportUtils', () => {

it('its paid IOU report', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
};
Expand All @@ -392,7 +395,7 @@ describe('ReportUtils', () => {

it('its approved Expense report', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
Expand All @@ -403,7 +406,7 @@ describe('ReportUtils', () => {

it('its paid Expense report', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
};
Expand All @@ -417,7 +420,7 @@ describe('ReportUtils', () => {
isOwnPolicyExpenseChat: false,
}).then(() => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
parentReportID: '100',
type: CONST.REPORT.TYPE.EXPENSE,
};
Expand All @@ -433,7 +436,7 @@ describe('ReportUtils', () => {
isOwnPolicyExpenseChat: true,
}).then(() => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
Expand Down Expand Up @@ -463,7 +466,7 @@ describe('ReportUtils', () => {
CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
].every((chatType) => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType,
};
const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, null, [currentUserAccountID, participantsAccountIDs[0]]);
Expand All @@ -474,7 +477,7 @@ describe('ReportUtils', () => {

it('has multiple participants excluding self', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, null, [currentUserAccountID, ...participantsAccountIDs]);
Expand All @@ -484,7 +487,7 @@ describe('ReportUtils', () => {

it('user has send money permission', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, null, [currentUserAccountID, ...participantsAccountIDs]);
Expand All @@ -494,7 +497,7 @@ describe('ReportUtils', () => {

it("it's a group DM report", () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.CHAT,
participantsAccountIDs: [currentUserAccountID, ...participantsAccountIDs],
};
Expand All @@ -512,7 +515,7 @@ describe('ReportUtils', () => {
isOwnPolicyExpenseChat: true,
}).then(() => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
parentReportID: '102',
type: CONST.REPORT.TYPE.EXPENSE,
};
Expand All @@ -529,7 +532,7 @@ describe('ReportUtils', () => {
isOwnPolicyExpenseChat: true,
}).then(() => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
Expand All @@ -551,7 +554,7 @@ describe('ReportUtils', () => {

it('it is an IOU report in submitted state', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
Expand All @@ -563,7 +566,7 @@ describe('ReportUtils', () => {

it('it is an IOU report in submitted state even with send money permissions', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
Expand All @@ -577,7 +580,7 @@ describe('ReportUtils', () => {
describe('return multiple money request option if', () => {
it("it is user's own policy expense chat", () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
};
Expand All @@ -589,7 +592,7 @@ describe('ReportUtils', () => {

it('it is a 1:1 DM', () => {
const report = {
...(LHNTestUtils.getFakeReport() as Report),
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.CHAT,
};
const moneyRequestOptions = ReportUtils.getMoneyRequestOptions(report, null, [currentUserAccountID, participantsAccountIDs[0]]);
Expand Down Expand Up @@ -618,7 +621,7 @@ describe('ReportUtils', () => {

describe('sortReportsByLastRead', () => {
it('should filter out report without reportID & lastReadTime and sort lastReadTime in ascending order', () => {
const reports = [
const reports: Array<OnyxEntry<Report>> = [
{reportID: '1', lastReadTime: '2023-07-08 07:15:44.030'},
{reportID: '2', lastReadTime: undefined},
{reportID: '3', lastReadTime: '2023-07-06 07:15:44.030'},
Expand All @@ -627,42 +630,41 @@ describe('ReportUtils', () => {
{reportID: '6'},
null,
];
const sortedReports = [
const sortedReports: Array<OnyxEntry<Report>> = [
{reportID: '3', lastReadTime: '2023-07-06 07:15:44.030'},
{reportID: '4', lastReadTime: '2023-07-07 07:15:44.030', type: CONST.REPORT.TYPE.IOU},
{reportID: '1', lastReadTime: '2023-07-08 07:15:44.030'},
] as const;
];
expect(ReportUtils.sortReportsByLastRead(reports, null)).toEqual(sortedReports);
});
});

describe('getAllAncestorReportActions', () => {
const reports = [
const reports: Report[] = [
{reportID: '1', lastReadTime: '2024-02-01 04:56:47.233', reportName: 'Report'},
{reportID: '2', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '1', parentReportID: '1', reportName: 'Report'},
{reportID: '3', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '2', parentReportID: '2', reportName: 'Report'},
{reportID: '4', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '3', parentReportID: '3', reportName: 'Report'},
{reportID: '5', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '4', parentReportID: '4', reportName: 'Report'},
] as const;
];

const reportActions = [
const reportActions: ReportAction[] = [
{reportActionID: '1', created: '2024-02-01 04:42:22.965', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '2', created: '2024-02-01 04:42:28.003', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '3', created: '2024-02-01 04:42:31.742', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '4', created: '2024-02-01 04:42:35.619', actionName: 'MARKEDREIMBURSED'},
] as const;
];

beforeAll(() => {
const reportCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.REPORT, reports, (report) => report.reportID);
const reportActionCollectionDataSet = toCollectionDataSet(
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
reportActions.map((reportAction) => ({[reportAction.reportActionID]: reportAction})),
(actions) => Object.values(actions)[0].reportActionID,
);
Onyx.multiSet({
pac-guerreiro marked this conversation as resolved.
Show resolved Hide resolved
[`${ONYXKEYS.COLLECTION.REPORT}${reports[0].reportID}` as const]: reports[0],
[`${ONYXKEYS.COLLECTION.REPORT}${reports[1].reportID}` as const]: reports[1],
[`${ONYXKEYS.COLLECTION.REPORT}${reports[2].reportID}` as const]: reports[2],
[`${ONYXKEYS.COLLECTION.REPORT}${reports[3].reportID}` as const]: reports[3],
[`${ONYXKEYS.COLLECTION.REPORT}${reports[4].reportID}` as const]: reports[4],
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reports[0].reportID}` as const]: {[reportActions[0].reportActionID]: reportActions[0]},
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reports[1].reportID}` as const]: {[reportActions[1].reportActionID]: reportActions[1]},
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reports[2].reportID}` as const]: {[reportActions[2].reportActionID]: reportActions[2]},
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reports[3].reportID}` as const]: {[reportActions[3].reportActionID]: reportActions[3]},
...reportCollectionDataSet,
...reportActionCollectionDataSet,
});
return waitForBatchedUpdates();
});
Expand Down
Loading