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
15 changes: 7 additions & 8 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1457,11 +1457,9 @@ function openReport(params: OpenReportActionParams) {
const participantLoginList = participants.map((p) => p.login).filter((login) => !!login);
// TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656
const participantAccountIDList = participants.map((p) => p.accountID).filter((id): id is number => id !== undefined);
const optimisticReport = reportActionsExist(reportID)
? {}
: {
reportName: allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.reportName ?? CONST.REPORT.DEFAULT_REPORT_NAME,
};
const existingReportName = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.reportName;
const isCreatingNewReport = !isEmptyObject(newReportObject);
const optimisticReport: Partial<Pick<Report, 'reportName'>> = reportActionsExist(reportID) || !existingReportName ? {} : {reportName: existingReportName};
Comment thread
abbasifaizan70 marked this conversation as resolved.

const optimisticData: Array<
OnyxUpdate<
Expand Down Expand Up @@ -1490,8 +1488,10 @@ function openReport(params: OpenReportActionParams) {
},
];

// Only add the report update if optimisticReport has data
if (Object.keys(optimisticReport).length > 0) {
// We need a report update for both:
// 1) existing reports with a known name, and
// 2) new reports, because the new-report flow mutates optimisticData.at(0) into a SET on REPORT_<id>.
if (isCreatingNewReport || Object.keys(optimisticReport).length > 0) {
optimisticData.unshift({
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
Expand Down Expand Up @@ -1691,7 +1691,6 @@ function openReport(params: OpenReportActionParams) {
}

// If we are creating a new report, we need to add the optimistic report data and a report action
const isCreatingNewReport = !isEmptyObject(newReportObject);
if (isCreatingNewReport) {
// Change the method to set for new reports because it doesn't exist yet, is faster,
// and we need the data to be available when we navigate to the chat page
Expand Down
50 changes: 50 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,56 @@ describe('actions/Report', () => {
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.OPEN_REPORT, 1);
});

it('should not optimistically inject default report name when report has no known name', async () => {
const REPORT_ID = 'openReport_noKnownName';

setHasRadio(false);
await waitForBatchedUpdates();

Report.openReport({
reportID: REPORT_ID,
introSelected: undefined,
betas: undefined,
});
await waitForBatchedUpdates();

const report = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`);
expect(report?.reportName).toBeUndefined();

setHasRadio(true);
await waitForBatchedUpdates();
});

it('should keep optimistic new report write on report collection key', async () => {
const REPORT_ID = 'openReport_newReportOptimistic';

setHasRadio(false);
await waitForBatchedUpdates();

Report.openReport({
reportID: REPORT_ID,
introSelected: undefined,
betas: undefined,
newReportObject: {
reportID: REPORT_ID,
},
});
await waitForBatchedUpdates();

const report = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`);
expect(report).toMatchObject({
reportID: REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
});

const loadingState = await getOnyxValue(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${REPORT_ID}`);
expect(loadingState).not.toHaveProperty('reportID');
expect(loadingState).not.toHaveProperty('reportName');

setHasRadio(true);
await waitForBatchedUpdates();
});

it('openReport legacy preview fallback stores action under correct Onyx key and preserves existing actions', async () => {
global.fetch = TestHelper.getGlobalFetchMock();

Expand Down
Loading