diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 422dd8fbb03a..b3fa78d07614 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -2647,7 +2647,7 @@ function submitReport(expenseReport) { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, value: { - state: CONST.REPORT.STATE.OPEN, + statusNum: CONST.REPORT.STATUS.OPEN, stateNum: CONST.REPORT.STATE_NUM.OPEN, }, }, diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 63fd7a0dd78b..f530e5892e94 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -13,6 +13,7 @@ import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager'; import waitForNetworkPromises from '../utils/waitForNetworkPromises'; import * as ReportUtils from '../../src/libs/ReportUtils'; import * as ReportActionsUtils from '../../src/libs/ReportActionsUtils'; +import * as PolicyActions from '../../src/libs/actions/Policy'; import * as PersonalDetailsUtils from '../../src/libs/PersonalDetailsUtils'; import * as User from '../../src/libs/actions/User'; import PusherHelper from '../utils/PusherHelper'; @@ -2158,4 +2159,206 @@ describe('actions/IOU', () => { expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(chatReport.reportID)); }); }); + + describe('submitReport', () => { + it('correctly submits a report', () => { + const amount = 10000; + const comment = '💸💸💸💸'; + const merchant = 'NASDAQ'; + let expenseReport = {}; + let chatReport = {}; + return waitForBatchedUpdates() + .then(() => { + PolicyActions.createWorkspace(CARLOS_EMAIL, true, "Carlos's Workspace"); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + chatReport = _.find(allReports, (report) => report.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + + resolve(); + }, + }); + }), + ) + .then(() => { + IOU.requestMoney( + chatReport, + amount, + CONST.CURRENCY.USD, + '', + merchant, + RORY_EMAIL, + RORY_ACCOUNT_ID, + {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, + comment, + ); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + Onyx.merge(`report_${expenseReport.reportID}`, { + statusNum: 0, + stateNum: 0, + }); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + + // Verify report is a draft + expect(expenseReport.stateNum).toBe(0); + expect(expenseReport.statusNum).toBe(0); + resolve(); + }, + }); + }), + ) + .then(() => { + IOU.submitReport(expenseReport); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + + // Report was submitted correctly + expect(expenseReport.stateNum).toBe(1); + expect(expenseReport.statusNum).toBe(1); + resolve(); + }, + }); + }), + ); + }); + it('correctly implements error handling', () => { + const amount = 10000; + const comment = '💸💸💸💸'; + const merchant = 'NASDAQ'; + let expenseReport = {}; + let chatReport = {}; + return waitForBatchedUpdates() + .then(() => { + PolicyActions.createWorkspace(CARLOS_EMAIL, true, "Carlos's Workspace"); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + chatReport = _.find(allReports, (report) => report.chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT); + + resolve(); + }, + }); + }), + ) + .then(() => { + IOU.requestMoney( + chatReport, + amount, + CONST.CURRENCY.USD, + '', + merchant, + RORY_EMAIL, + RORY_ACCOUNT_ID, + {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID, isPolicyExpenseChat: true, reportID: chatReport.reportID}, + comment, + ); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + Onyx.merge(`report_${expenseReport.reportID}`, { + statusNum: 0, + stateNum: 0, + }); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + + // Verify report is a draft + expect(expenseReport.stateNum).toBe(0); + expect(expenseReport.statusNum).toBe(0); + resolve(); + }, + }); + }), + ) + .then(() => { + fetch.fail(); + IOU.submitReport(expenseReport); + return waitForBatchedUpdates(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + expenseReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.EXPENSE); + + // Report was submitted with some fail + expect(expenseReport.stateNum).toBe(0); + expect(expenseReport.statusNum).toBe(0); + resolve(); + }, + }); + }), + ); + }); + }); });