Skip to content

Commit

Permalink
Merge pull request #29476 from waterim/feat-28764-add-submitting-IOU-…
Browse files Browse the repository at this point in the history
…tests

[NoQA] Feature: IOUTests for submitReport
  • Loading branch information
mountiny authored Oct 17, 2023
2 parents 190e1d0 + d3e0626 commit 5dbf817
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
203 changes: 203 additions & 0 deletions tests/actions/IOUTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
},
});
}),
);
});
});
});

0 comments on commit 5dbf817

Please sign in to comment.