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

[NOQA] Redesign Reassure tests for getOrderedReportIDs #37762

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
129 changes: 79 additions & 50 deletions tests/perf-test/SidebarUtils.perf-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {rand} from '@ngneat/falso';
import type {OnyxCollection} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {measureFunction} from 'reassure';
Expand All @@ -13,16 +14,26 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject';
import createCollection from '../utils/collections/createCollection';
import createPersonalDetails from '../utils/collections/personalDetails';
import createRandomPolicy from '../utils/collections/policies';
import createRandomReportAction from '../utils/collections/reportActions';
import createRandomReportAction, {getRandomDate} from '../utils/collections/reportActions';
import createRandomReport from '../utils/collections/reports';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

const getMockedReports = (length = 500) =>
createCollection<Report>(
(item) => `${ONYXKEYS.COLLECTION.REPORT}${item.reportID}`,
(index) => createRandomReport(index),
length,
);
const REPORTS_COUNT = 15000;
const REPORT_TRESHOLD = 5;
const PERSONAL_DETAILS_LIST_COUNT = 1000;

const allReports = createCollection<Report>(
(item) => `${ONYXKEYS.COLLECTION.REPORT}${item.reportID}`,
(index) => ({
...createRandomReport(index),
type: rand(Object.values(CONST.REPORT.TYPE)),
lastVisibleActionCreated: getRandomDate(),
// add status and state to every 5th report to mock nonarchived reports
statusNum: index % REPORT_TRESHOLD ? 0 : CONST.REPORT.STATUS_NUM.CLOSED,
stateNum: index % REPORT_TRESHOLD ? 0 : CONST.REPORT.STATE_NUM.APPROVED,
}),
REPORTS_COUNT,
);

const reportActions = createCollection<ReportAction>(
(item) => `${item.reportActionID}`,
Expand All @@ -32,9 +43,48 @@ const reportActions = createCollection<ReportAction>(
const personalDetails = createCollection<PersonalDetails>(
(item) => item.accountID,
(index) => createPersonalDetails(index),
PERSONAL_DETAILS_LIST_COUNT,
);

const policies = createCollection<Policy>(
(item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`,
(index) => createRandomPolicy(index),
);

const mockedResponseMap = getMockedReports(1000) as Record<`${typeof ONYXKEYS.COLLECTION.REPORT}`, Report>;
const mockedBetas = Object.values(CONST.BETAS);

const allReportActions = Object.fromEntries(
Object.keys(reportActions).map((key) => [
`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${key}`,
{
[reportActions[key].reportActionID]: {
errors: reportActions[key].errors ?? [],
message: [
{
moderationDecision: {
decision: reportActions[key].message?.[0]?.moderationDecision?.decision,
},
},
],
reportActionID: reportActions[key].reportActionID,
},
},
]),
) as unknown as OnyxCollection<ReportActions>;

const currentReportId = '1';
const transactionViolations = {} as OnyxCollection<TransactionViolation[]>;

const reportKeys = Object.keys(allReports);
const reportIDsWithErrors = reportKeys.reduce((errorsMap, reportKey) => {
const report = allReports[reportKey];
const allReportsActions = allReportActions?.[reportKey.replace('report_', 'reportActions_')] ?? null;
const errors = OptionsListUtils.getAllReportErrors(report, allReportsActions) || {};
if (isEmptyObject(errors)) {
return errorsMap;
}
return {...errorsMap, [reportKey.replace('report_', '')]: errors};
}, {});

describe('SidebarUtils', () => {
beforeAll(() => {
Expand All @@ -44,15 +94,15 @@ describe('SidebarUtils', () => {
});

Onyx.multiSet({
...mockedResponseMap,
[ONYXKEYS.PERSONAL_DETAILS_LIST]: personalDetails,
});
});

afterAll(() => {
Onyx.clear();
});

test('[SidebarUtils] getOptionData on 1k reports', async () => {
test('[SidebarUtils] getOptionData', async () => {
const report = createRandomReport(1);
const preferredLocale = 'en';
const policy = createRandomPolicy(1);
Expand All @@ -73,54 +123,33 @@ describe('SidebarUtils', () => {
);
});

test('[SidebarUtils] getOrderedReportIDs on 1k reports', async () => {
const currentReportId = '1';
const allReports = getMockedReports();
const betas = [CONST.BETAS.DEFAULT_ROOMS];
const transactionViolations = {} as OnyxCollection<TransactionViolation[]>;

const policies = createCollection<Policy>(
(item) => `${ONYXKEYS.COLLECTION.POLICY}${item.id}`,
(index) => createRandomPolicy(index),
test('[SidebarUtils] getOrderedReportIDs on 15k reports for default priorityMode', async () => {
await waitForBatchedUpdates();
await measureFunction(() =>
SidebarUtils.getOrderedReportIDs(
currentReportId,
allReports,
mockedBetas,
policies,
CONST.PRIORITY_MODE.DEFAULT,
allReportActions,
transactionViolations,
undefined,
undefined,
reportIDsWithErrors,
),
);
});

const allReportActions = Object.fromEntries(
Object.keys(reportActions).map((key) => [
key,
{
[reportActions[key].reportActionID]: {
errors: reportActions[key].errors ?? [],
message: [
{
moderationDecision: {
decision: reportActions[key].message?.[0]?.moderationDecision?.decision,
},
},
],
},
},
]),
) as unknown as OnyxCollection<ReportActions>;

const reportKeys = Object.keys(allReports);
const reportIDsWithErrors = reportKeys.reduce((errorsMap, reportKey) => {
const report = allReports[reportKey];
const allReportsActions = allReportActions?.[reportKey.replace('report_', 'reportActions_')] ?? null;
const errors = OptionsListUtils.getAllReportErrors(report, allReportsActions) || {};
if (isEmptyObject(errors)) {
return errorsMap;
}
return {...errorsMap, [reportKey.replace('report_', '')]: errors};
}, {});

test('[SidebarUtils] getOrderedReportIDs on 15k reports for GSD priorityMode', async () => {
await waitForBatchedUpdates();
await measureFunction(() =>
SidebarUtils.getOrderedReportIDs(
currentReportId,
allReports,
betas,
mockedBetas,
policies,
CONST.PRIORITY_MODE.DEFAULT,
CONST.PRIORITY_MODE.GSD,
allReportActions,
transactionViolations,
undefined,
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/collections/reportActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ export default function createRandomReportAction(index: number): ReportAction {
isAttachment: randBoolean(),
};
}

export {getRandomDate};
Loading