Skip to content

Commit

Permalink
Merge pull request Expensify#30909 from OlimpiaZurek/feat/SidebarUtil…
Browse files Browse the repository at this point in the history
…s-reassure-tests

[NoQA] chore: add perf tests for SidebarUtils
  • Loading branch information
mountiny authored Nov 30, 2023
2 parents 7ebe763 + 5551ab0 commit fa96a12
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ type ActorDetails = {
*/
function getOptionData(
report: Report,
reportActions: Record<string, ReportAction[]>,
reportActions: Record<string, ReportAction>,
personalDetails: Record<number, PersonalDetails>,
preferredLocale: ValueOf<typeof CONST.LOCALES>,
policy: Policy,
Expand Down
97 changes: 97 additions & 0 deletions tests/perf-test/SidebarUtils.perf-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Onyx, {OnyxCollection} from 'react-native-onyx';
import {measureFunction} from 'reassure';
import SidebarUtils from '@libs/SidebarUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {PersonalDetails} from '@src/types/onyx';
import Policy from '@src/types/onyx/Policy';
import Report from '@src/types/onyx/Report';
import ReportAction, {ReportActions} from '@src/types/onyx/ReportAction';
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 createRandomReport from '../utils/collections/reports';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

beforeAll(() =>
Onyx.init({
keys: ONYXKEYS,
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
}),
);

// Clear out Onyx after each test so that each test starts with a clean slate
afterEach(() => {
Onyx.clear();
});

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

const reportActions = createCollection<ReportAction>(
(item) => `${item.reportActionID}`,
(index) => createRandomReportAction(index),
);

const personalDetails = createCollection<PersonalDetails>(
(item) => item.accountID,
(index) => createPersonalDetails(index),
);

const mockedResponseMap = getMockedReports(5000) as Record<`${typeof ONYXKEYS.COLLECTION.REPORT}`, Report>;
const runs = CONST.PERFORMANCE_TESTS.RUNS;

test('getOptionData on 5k reports', async () => {
const report = createRandomReport(1);
const preferredLocale = 'en';
const policy = createRandomPolicy(1);
const parentReportAction = createRandomReportAction(1);

Onyx.multiSet({
...mockedResponseMap,
});

await waitForBatchedUpdates();
await measureFunction(() => SidebarUtils.getOptionData(report, reportActions, personalDetails, preferredLocale, policy, parentReportAction), {runs});
});

test('getOrderedReportIDs on 5k reports', async () => {
const currentReportId = '1';
const allReports = getMockedReports();
const betas = [CONST.BETAS.DEFAULT_ROOMS, CONST.BETAS.POLICY_ROOMS];

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

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

Onyx.multiSet({
...mockedResponseMap,
});

await waitForBatchedUpdates();
await measureFunction(() => SidebarUtils.getOrderedReportIDs(currentReportId, allReports, betas, policies, CONST.PRIORITY_MODE.DEFAULT, allReportActions), {runs});
});
2 changes: 1 addition & 1 deletion tests/utils/collections/createCollection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function createCollection<T>(createKey: (item: T, index: number) => string, createItem: (index: number) => T, length = 500): Record<string, T> {
export default function createCollection<T>(createKey: (item: T, index: number) => string | number, createItem: (index: number) => T, length = 500): Record<string, T> {
const map: Record<string, T> = {};

for (let i = 0; i < length; i++) {
Expand Down
12 changes: 12 additions & 0 deletions tests/utils/collections/personalDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {randAvatar, randEmail, randWord} from '@ngneat/falso';
import type {PersonalDetails} from '@src/types/onyx';

export default function createPersonalDetails(index: number): PersonalDetails {
return {
accountID: index,
avatar: randAvatar(),
displayName: randWord(),
lastName: randWord(),
login: randEmail(),
};
}

0 comments on commit fa96a12

Please sign in to comment.