Skip to content

Commit

Permalink
Merge pull request #30482 from adhorodyski/reassure/get-last-visible-…
Browse files Browse the repository at this point in the history
…action

[NoQA] test(reassure): get last visible action
  • Loading branch information
mountiny authored Nov 1, 2023
2 parents fd89a41 + f8b184a commit 6cc68ac
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/perf-test/ReportActionsUtils.perf-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Onyx from 'react-native-onyx';
import {measureFunction} from 'reassure';
import _ from 'underscore';
import CONST from '../../src/CONST';
import * as ReportActionsUtils from '../../src/libs/ReportActionsUtils';
import ONYXKEYS from '../../src/ONYXKEYS';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

jest.setTimeout(60000);

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

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

const getMockedReportActionsMap = (reportsLength = 10, actionsPerReportLength = 100) => {
const mockReportActions = Array.from({length: actionsPerReportLength}, (_reportAction, i) => {
const reportActionKey = i + 1;
const email = `actor+${reportActionKey}@mail.com`;
const reportAction = LHNTestUtils.getFakeReportAction(email);

return {[reportActionKey]: reportAction};
});

const reportKeysMap = Array.from({length: reportsLength}, (_report, i) => {
const key = i + 1;

return {[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${key}`]: _.assign({}, ...mockReportActions)};
});

return _.assign({}, ...reportKeysMap);
};

const mockedReportActionsMap = getMockedReportActionsMap(2, 10000);

/**
* This function will be executed 20 times and the average time will be used on the comparison.
* It will fail based on the CI configuration around Reassure:
* @see /.github/workflows/reassurePerformanceTests.yml
*
* Max deviation on the duration is set to 20% at the time of writing.
*
* More on the measureFunction API:
* @see https://callstack.github.io/reassure/docs/api#measurefunction-function
*/
test('getLastVisibleAction on 10k reportActions', async () => {
const reportId = '1';

await Onyx.multiSet({
...mockedReportActionsMap,
});
await waitForBatchedUpdates();
await measureFunction(() => ReportActionsUtils.getLastVisibleAction(reportId), {runs: 20});
});

test('getLastVisibleAction on 10k reportActions with actionsToMerge', async () => {
const reportId = '1';
const parentReportActionId = '1';
const fakeParentAction = mockedReportActionsMap[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportId}`][parentReportActionId];
const actionsToMerge = {
[parentReportActionId]: {
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE,
previousMessage: fakeParentAction.message,
message: [
{
translationKey: '',
type: 'COMMENT',
html: '',
text: '',
isEdited: true,
isDeletedParentAction: true,
},
],
errors: null,
linkMetaData: [],
},
};

await Onyx.multiSet({
...mockedReportActionsMap,
});
await waitForBatchedUpdates();
await measureFunction(() => ReportActionsUtils.getLastVisibleAction(reportId, actionsToMerge), {runs: 20});
});
50 changes: 50 additions & 0 deletions tests/utils/LHNTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function getFakeReportAction(actor = '[email protected]', millisecondsInThePast =
actor,
actorAccountID: 1,
reportActionID: `${++lastFakeReportActionID}`,
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
shouldShow: true,
timestamp,
reportActionTimestamp: timestamp,
Expand All @@ -142,6 +143,55 @@ function getFakeReportAction(actor = '[email protected]', millisecondsInThePast =
],
whisperedToAccountIDs: [],
automatic: false,
message: [
{
type: 'COMMENT',
html: 'hey',
text: 'hey',
isEdited: false,
whisperedTo: [],
isDeletedParentAction: false,
reactions: [
{
emoji: 'heart',
users: [
{
accountID: 1,
skinTone: -1,
},
],
},
],
},
],
originalMessage: {
childReportID: `${++lastFakeReportActionID}`,
emojiReactions: {
heart: {
createdAt: '2023-08-28 15:27:52',
users: {
1: {
skinTones: {
'-1': '2023-08-28 15:27:52',
},
},
},
},
},
html: 'hey',
lastModified: '2023-08-28 15:28:12.432',
reactions: [
{
emoji: 'heart',
users: [
{
accountID: 1,
skinTone: -1,
},
],
},
],
},
};
}

Expand Down

0 comments on commit 6cc68ac

Please sign in to comment.