Skip to content
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
20 changes: 20 additions & 0 deletions jest/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-classes-per-file */
import '@shopify/flash-list/jestSetup';
import 'react-native-gesture-handler/jestSetup';
import type * as RNKeyboardController from 'react-native-keyboard-controller';
Expand Down Expand Up @@ -78,3 +79,22 @@ jest.mock('@src/libs/actions/Timing', () => ({
start: jest.fn(),
end: jest.fn(),
}));

// This makes FlatList render synchronously for easier testing.
jest.mock(
'@react-native/virtualized-lists/Interaction/Batchinator',
() =>
class SyncBachinator {
#callback: () => void;

constructor(callback: () => void) {
this.#callback = callback;
}

schedule() {
this.#callback();
}

dispose() {}
},
);
3 changes: 1 addition & 2 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,9 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro

useEffect(() => {
// Call OpenReport only if we are not linking to a message or the report is not available yet
if (isLoadingReportOnyx || (reportActionIDFromRoute && report.reportID && isLinkedMessagePageReady)) {
if (isLoadingReportOnyx || reportActionIDFromRoute) {
return;
}

fetchReportIfNeeded();
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [isLoadingReportOnyx]);
Expand Down
33 changes: 24 additions & 9 deletions tests/ui/PaginationTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,7 @@ describe('Pagination', () => {
expect(getReportActions()).toHaveLength(18);
});

// Currently broken on main by https://github.com/Expensify/App/pull/42582.
// TODO: Investigate and re-enable.
it.skip('opens a chat and load newer messages', async () => {
it('opens a chat and load newer messages', async () => {
mockOpenReport(5, '5');
mockGetNewerActions(5);

Expand All @@ -325,26 +323,43 @@ describe('Pagination', () => {
});
// ReportScreen relies on the onLayout event to receive updates from onyx.
triggerListLayout();
await waitForBatchedUpdatesWithAct();

expect(getReportActions()).toHaveLength(5);
// Here we have 5 messages from the initial OpenReport and 5 from the initial GetNewerActions.
expect(getReportActions()).toHaveLength(10);

// There is 1 extra call here because of the comment linking report.
TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 2);
TestHelper.expectAPICommandToHaveBeenCalledWith('OpenReport', 1, {reportID: REPORT_ID, reportActionID: '5'});
TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0);
TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 0);
TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 0, {reportID: REPORT_ID, reportActionID: '5'});

// Simulate the maintainVisibleContentPosition scroll adjustment, so it is now possible to scroll down more.
scrollToOffset(500);
scrollToOffset(0);
await waitForBatchedUpdatesWithAct();

TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 2);
TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0);
TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 1);
TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 0, {reportID: REPORT_ID, reportActionID: '5'});
TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 2);
TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 1, {reportID: REPORT_ID, reportActionID: '10'});

// We now have 15 messages. 5 from the initial OpenReport and 10 from the 2 GetNewerActions calls.
expect(getReportActions()).toHaveLength(15);

// Simulate the backend returning no new messages to simulate reaching the start of the chat.
mockGetNewerActions(0);

scrollToOffset(500);
scrollToOffset(0);
await waitForBatchedUpdatesWithAct();

// We now have 10 messages. 5 from the initial OpenReport and 5 from GetNewerActions.
expect(getReportActions()).toHaveLength(10);
TestHelper.expectAPICommandToHaveBeenCalled('OpenReport', 2);
TestHelper.expectAPICommandToHaveBeenCalled('GetOlderActions', 0);
TestHelper.expectAPICommandToHaveBeenCalled('GetNewerActions', 3);
TestHelper.expectAPICommandToHaveBeenCalledWith('GetNewerActions', 2, {reportID: REPORT_ID, reportActionID: '15'});

// We still have 15 messages. 5 from the initial OpenReport and 10 from the 2 GetNewerActions calls.
expect(getReportActions()).toHaveLength(15);
});
});