Skip to content

Commit

Permalink
Merge pull request #47197 from janicduplessis/@janic/newer-actions-test
Browse files Browse the repository at this point in the history
Fix double OpenReport call when comment linking, enable test
  • Loading branch information
pecanoro authored Aug 30, 2024
2 parents 9c83528 + 3d1d878 commit 1f71ce6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
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 @@ -524,10 +524,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);
});
});

0 comments on commit 1f71ce6

Please sign in to comment.