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
8 changes: 4 additions & 4 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2491,13 +2491,13 @@ function deleteReportField(reportID: string, reportField: PolicyReportField) {
API.write(WRITE_COMMANDS.DELETE_REPORT_FIELD, parameters, {optimisticData, failureData, successData});
}

function updateDescription(reportID: string, previousValue: string, newValue: string) {
function updateDescription(reportID: string, currentDescription: string, newMarkdownValue: string) {
// No change needed
if (previousValue === newValue) {
if (Parser.htmlToMarkdown(currentDescription) === newMarkdownValue) {
return;
}

const parsedDescription = getParsedComment(newValue, {reportID});
const parsedDescription = getParsedComment(newMarkdownValue, {reportID});
const optimisticDescriptionUpdatedReportAction = buildOptimisticRoomDescriptionUpdatedReportAction(parsedDescription);
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];

Expand Down Expand Up @@ -2526,7 +2526,7 @@ function updateDescription(reportID: string, previousValue: string, newValue: st
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
description: previousValue,
description: currentDescription,
pendingFields: {description: null},
lastActorAccountID: report?.lastActorAccountID,
lastVisibleActionCreated: report?.lastVisibleActionCreated,
Expand Down
34 changes: 34 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import getIsUsingFakeTimers from '../utils/getIsUsingFakeTimers';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import PusherHelper from '../utils/PusherHelper';
import * as TestHelper from '../utils/TestHelper';
import type {MockFetch} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import waitForNetworkPromises from '../utils/waitForNetworkPromises';

Expand Down Expand Up @@ -1648,4 +1649,37 @@ describe('actions/Report', () => {
});
});
});

describe('updateDescription', () => {
it('should not call UpdateRoomDescription API if the description is not changed', async () => {
global.fetch = TestHelper.getGlobalFetchMock();
Report.updateDescription('1', '<h1>test</h1>', '# test');

await waitForBatchedUpdates();

expect(global.fetch).toHaveBeenCalledTimes(0);
});

it('should revert to correct previous description if UpdateRoomDescription API fails', async () => {
const report: OnyxTypes.Report = {
...createRandomReport(1),
description: '<h1>test</h1>',
};
const mockFetch = fetch as MockFetch;

await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);

mockFetch?.fail?.();
Report.updateDescription('1', '<h1>test</h1>', '# test1');

await waitForBatchedUpdates();
let updateReport: OnyxEntry<OnyxTypes.Report>;

await TestHelper.getOnyxData({
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
callback: (val) => (updateReport = val),
});
expect(updateReport?.description).toBe('<h1>test</h1>');
});
});
});