Skip to content
Merged
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
139 changes: 81 additions & 58 deletions tests/ui/PaginationTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const USER_A_ACCOUNT_ID = 1;
const USER_A_EMAIL = 'user_a@test.com';
const USER_B_ACCOUNT_ID = 2;
const USER_B_EMAIL = 'user_b@test.com';
const TEST_AUTH_TOKEN = 'test-auth-token';
const TEST_AUTO_GENERATED_LOGIN = 'expensify.cash-abc123';

function getReportScreen(reportID = REPORT_ID) {
return screen.getByTestId(`report-screen-${reportID}`);
Expand Down Expand Up @@ -189,74 +191,92 @@ function mockGetNewerActions(messageCount: number) {
}));
}

async function fastSignInWithTestUser() {
await Onyx.multiSet({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be wrapped with act?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this we’re just preloading Onyx data before <App /> renders, so there’s no mounted UI to update yet. So act shouldn't be needed.

[ONYXKEYS.CREDENTIALS]: {
login: USER_A_EMAIL,
autoGeneratedLogin: TEST_AUTO_GENERATED_LOGIN,
autoGeneratedPassword: 'Password1',
},
[ONYXKEYS.ACCOUNT]: {
validated: true,
isUsingExpensifyCard: false,
},
[ONYXKEYS.SESSION]: {
authToken: TEST_AUTH_TOKEN,
accountID: USER_A_ACCOUNT_ID,
email: USER_A_EMAIL,
encryptedAuthToken: TEST_AUTH_TOKEN,
},
[ONYXKEYS.BETAS]: ['all'],
[ONYXKEYS.NVP_PRIVATE_PUSH_NOTIFICATION_ID]: 'randomID',
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[USER_A_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_A_EMAIL, USER_A_ACCOUNT_ID, 'A'),
},
});
await waitForBatchedUpdates();
}

/**
* Sets up a test with a logged in user. Returns the <App/> test instance.
*/
async function signInAndGetApp(): Promise<void> {
// Render the App and sign in as a test user.
render(<App />);
await waitForBatchedUpdatesWithAct();
const hintText = TestHelper.translateLocal('loginForm.loginForm');
const loginForm = await screen.findAllByLabelText(hintText);
expect(loginForm).toHaveLength(1);

await act(async () => {
await TestHelper.signInWithTestUser(USER_A_ACCOUNT_ID, USER_A_EMAIL, undefined, undefined, 'A');
});
await fastSignInWithTestUser();

render(<App />);
await waitForBatchedUpdatesWithAct();

// Start listening for pusher events after navigation settles.
subscribeToUserEvents();

await waitForBatchedUpdates();

await act(async () => {
// Simulate setting an unread report and personal details
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {
reportID: REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastMessageText: 'Test',
participants: {
[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
lastActorAccountID: USER_B_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
});

await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[USER_B_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_B_EMAIL, USER_B_ACCOUNT_ID, 'B'),
});

// Setup a 2nd report to test comment linking.
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${COMMENT_LINKING_REPORT_ID}`, {
reportID: COMMENT_LINKING_REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastMessageText: 'Test',
participants: {[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}},
lastActorAccountID: USER_A_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
});

await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${COMMENT_LINKING_REPORT_ID}`, {
'100': buildCreatedAction('100', format(TEN_MINUTES_AGO, CONST.DATE.FNS_DB_FORMAT_STRING)),
'101': {
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
person: [{type: 'TEXT', style: 'strong', text: 'User B'}],
created: format(addSeconds(TEN_MINUTES_AGO, 10), CONST.DATE.FNS_DB_FORMAT_STRING),
message: [
{
type: 'COMMENT',
html: '<a href="https://dev.new.expensify.com:8082/r/1/5">Link 1</a>',
text: 'Link 1',
},
],
reportActionID: '101',
actorAccountID: USER_A_ACCOUNT_ID,
},
});

// We manually setting the sidebar as loaded since the onLayout event does not fire in tests
await Promise.all([
// Simulate setting an unread report and personal details
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {
reportID: REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastMessageText: 'Test',
participants: {
[USER_B_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS},
},
lastActorAccountID: USER_B_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
}),
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[USER_B_ACCOUNT_ID]: TestHelper.buildPersonalDetails(USER_B_EMAIL, USER_B_ACCOUNT_ID, 'B'),
}),

// Setup a 2nd report to test comment linking.
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${COMMENT_LINKING_REPORT_ID}`, {
reportID: COMMENT_LINKING_REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastMessageText: 'Test',
participants: {[USER_A_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}},
lastActorAccountID: USER_A_ACCOUNT_ID,
type: CONST.REPORT.TYPE.CHAT,
}),
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${COMMENT_LINKING_REPORT_ID}`, {
'100': buildCreatedAction('100', format(TEN_MINUTES_AGO, CONST.DATE.FNS_DB_FORMAT_STRING)),
'101': {
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
person: [{type: 'TEXT', style: 'strong', text: 'User B'}],
created: format(addSeconds(TEN_MINUTES_AGO, 10), CONST.DATE.FNS_DB_FORMAT_STRING),
message: [
{
type: 'COMMENT',
html: '<a href="https://dev.new.expensify.com:8082/r/1/5">Link 1</a>',
text: 'Link 1',
},
],
reportActionID: '101',
actorAccountID: USER_A_ACCOUNT_ID,
},
}),
]);

// Manually mark the sidebar as loaded since onLayout does not fire in tests.
setSidebarLoaded();
});

Expand Down Expand Up @@ -324,10 +344,13 @@ describe('Pagination', () => {
TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 0);

await waitForNetworkPromises();
await waitForBatchedUpdatesWithAct();

// We now have 18 messages. 15 (MIN_INITIAL_REPORT_ACTION_COUNT) from the initial OpenReport and 3 from GetOlderActions.
// GetOlderActions only returns 3 actions since it reaches id '1', which is the created action.
expect(getReportActions()).toHaveLength(18);
await waitFor(() => {

@abzokhattab abzokhattab Nov 4, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShridharGoel Should we add the await waitFor for all other assertions to make it consistent? not so sure. what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We needed it because GetOlderActions resolves after a network mock and the merges finish slightly later, so the length can still be lesser when the assertion first runs.

If we use it for other assertions also, it would add unnecessary polling since those actions don't involve waitForNetworkPromises. What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point i agree.

expect(getReportActions()).toHaveLength(18);
});
});

it('opens a chat and load newer messages', async () => {
Expand Down