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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* A map linking the optimistic MOVED_TRANSACTION or UNREPORTED_TRANSACTION reportActionID to the transactionID.
* movedReportActionID is omitted when the move doesn't create a system message (moves out of a draft report).
* movedReportActionID is omitted when the move doesn't create a system message (moves into a draft report).
* If we're creating the transactionThread as part of moving the transaction, we should also send the optimistic
* transactionThreadReportID and transactionThreadCreatedReportActionID. If the transaction is held before moving to selfDM,
* we should also send the unholdReportActionID.
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ function getChangeTransactionsReportOnyxData({
let movedAction;
if (reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
movedAction = buildOptimisticUnreportedTransactionAction(transactionThreadReportID, oldReportID);
} else if (!isOpenReport(oldReport)) {
} else if (!isOpenReport(newReport)) {
movedAction = buildOptimisticMovedTransactionAction(transactionThreadReportID, oldReportID);
}

Expand Down
53 changes: 32 additions & 21 deletions tests/actions/TransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ describe('actions/Transaction', () => {
* Seeds an expense sitting in a source report (whose state/status decide whether it is a draft),
* along with its IOU action and transaction thread, and moves it to `newReport`.
*/
async function moveExpenseOutOf(sourceReportStatus: Pick<Report, 'stateNum' | 'statusNum'>, newReport: Report | undefined) {
async function moveExpenseFromTo(sourceReportStatus: Pick<Report, 'stateNum' | 'statusNum'>, newReport: Report | undefined) {
const policyID = generatePolicyID();
const policy: Policy = {...createRandomPolicy(3, CONST.POLICY.TYPE.TEAM, 'Moved Message Workspace'), id: policyID};

Expand Down Expand Up @@ -631,33 +631,44 @@ describe('actions/Transaction', () => {
const draftReportStatus = {stateNum: CONST.REPORT.STATE_NUM.OPEN, statusNum: CONST.REPORT.STATUS_NUM.OPEN};
const submittedReportStatus = {stateNum: CONST.REPORT.STATE_NUM.SUBMITTED, statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED};

const destinationReport = {
reportID: DESTINATION_REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: RORY_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
} as Report;
const buildDestinationReport = (destinationReportStatus: Pick<Report, 'stateNum' | 'statusNum'>) =>
({
reportID: DESTINATION_REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: RORY_ACCOUNT_ID,
...destinationReportStatus,
}) as Report;

const countMovedActions = (actions: Array<ReportAction | undefined>) => actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION).length;

it('should not create a MOVED_TRANSACTION action when the expense is moved out of a draft report', async () => {
// Given an expense in a draft (open) report, when it is moved to another report
const actions = await moveExpenseOutOf(draftReportStatus, destinationReport);
it.each([
['draft', draftReportStatus],
['submitted', submittedReportStatus],
])('should not create a MOVED_TRANSACTION action when the expense is moved from a %s report into a draft report', async (_sourceLabel, sourceReportStatus) => {
// Given an expense being moved into a draft (open) report, which the backend never adopts the message on
const actions = await moveExpenseFromTo(sourceReportStatus, buildDestinationReport(draftReportStatus));

// Then no moved system message is created, because moves between drafts aren't part of the audit trail
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION)).toHaveLength(0);
// Then no moved system message is created, so the App doesn't hold an action the server will never return
expect(countMovedActions(actions)).toBe(0);
});

it('should create a MOVED_TRANSACTION action when the expense is moved out of a submitted report', async () => {
// Given an expense in a submitted report, when it is moved to another report
const actions = await moveExpenseOutOf(submittedReportStatus, destinationReport);
it.each([
['draft', draftReportStatus],
['submitted', submittedReportStatus],
])('should create a MOVED_TRANSACTION action when the expense is moved from a %s report into a submitted report', async (_sourceLabel, sourceReportStatus) => {
// Given an expense being moved into a submitted report, which the backend does create the message on
const actions = await moveExpenseFromTo(sourceReportStatus, buildDestinationReport(submittedReportStatus));

// Then the moved system message is still created, since the audit trail starts once a report is submitted
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MOVED_TRANSACTION)).toHaveLength(1);
// Then the moved system message is created optimistically to match the backend
expect(countMovedActions(actions)).toBe(1);
});

it('should create an UNREPORTED_TRANSACTION action when the expense is moved to personal space from a draft report', async () => {
// Given an expense in a draft report, when it is moved to personal space (no destination report)
const actions = await moveExpenseOutOf(draftReportStatus, undefined);
it.each([
['draft', draftReportStatus],
['submitted', submittedReportStatus],
])('should create an UNREPORTED_TRANSACTION action when the expense is moved to personal space from a %s report', async (_sourceLabel, sourceReportStatus) => {
// Given an expense being moved to personal space (no destination report), which the backend always creates the message on
const actions = await moveExpenseFromTo(sourceReportStatus, undefined);

// Then the moved message is still created, because the expense leaves the report entirely
expect(actions.filter((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION)).toHaveLength(1);
Expand Down
41 changes: 26 additions & 15 deletions tests/unit/TransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2074,22 +2074,23 @@ describe('Transaction', () => {
expect(updatedTransaction?.comment?.customUnit?.customUnitRateID).toBe(validRateID);
});

it('should not create MOVED_TRANSACTION action when moving expenses from a Draft report', async () => {
it('should not create MOVED_TRANSACTION action when moving expenses into a Draft report', async () => {
const mockAPIWrite = jest.spyOn(API, 'write').mockImplementation(() => Promise.resolve());

const draftReport = {
const submittedReport = {
...createRandomReport(10, undefined),
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
currency: CONST.CURRENCY.USD,
};
const transaction = generateTransaction({reportID: draftReport.reportID});
const transaction = generateTransaction({reportID: submittedReport.reportID});
const oldIOUAction = createIOUAction(transaction);

await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${draftReport.reportID}`, draftReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${draftReport.reportID}`, {[oldIOUAction.reportActionID]: oldIOUAction});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${submittedReport.reportID}`, submittedReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${submittedReport.reportID}`, {[oldIOUAction.reportActionID]: oldIOUAction});

// FAKE_NEW_REPORT_ID is an open (draft) report, which the backend never creates the moved message on
const report = await getReportFromUseOnyx(FAKE_NEW_REPORT_ID);
const allTransactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction,
Expand All @@ -2105,7 +2106,7 @@ describe('Transaction', () => {
allTransactions,
policyTagList: undefined,
transactionViolations: {},
reports: {[`${ONYXKEYS.COLLECTION.REPORT}${draftReport.reportID}`]: draftReport},
reports: {[`${ONYXKEYS.COLLECTION.REPORT}${submittedReport.reportID}`]: submittedReport},
isTrackIntentUser: false,
});
await waitForBatchedUpdates();
Expand All @@ -2120,23 +2121,30 @@ describe('Transaction', () => {
mockAPIWrite.mockRestore();
});

it('should create MOVED_TRANSACTION action when moving expenses from a non-Draft report', async () => {
it('should create MOVED_TRANSACTION action when moving expenses into a non-Draft report', async () => {
const mockAPIWrite = jest.spyOn(API, 'write').mockImplementation(() => Promise.resolve());

const submittedReport = {
const draftReport = {
...createRandomReport(11, undefined),
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
currency: CONST.CURRENCY.USD,
};
const submittedDestinationReport = {
...createRandomReport(12, undefined),
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
currency: CONST.CURRENCY.USD,
};
const transaction = generateTransaction({reportID: submittedReport.reportID});
const transaction = generateTransaction({reportID: draftReport.reportID});
const oldIOUAction = createIOUAction(transaction);

await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${submittedReport.reportID}`, submittedReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${submittedReport.reportID}`, {[oldIOUAction.reportActionID]: oldIOUAction});
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${draftReport.reportID}`, draftReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${submittedDestinationReport.reportID}`, submittedDestinationReport);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${draftReport.reportID}`, {[oldIOUAction.reportActionID]: oldIOUAction});

const report = await getReportFromUseOnyx(FAKE_NEW_REPORT_ID);
const report = await getReportFromUseOnyx(submittedDestinationReport.reportID);
const allTransactions = {
[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`]: transaction,
};
Expand All @@ -2151,7 +2159,10 @@ describe('Transaction', () => {
allTransactions,
policyTagList: undefined,
transactionViolations: {},
reports: {[`${ONYXKEYS.COLLECTION.REPORT}${submittedReport.reportID}`]: submittedReport},
reports: {
[`${ONYXKEYS.COLLECTION.REPORT}${draftReport.reportID}`]: draftReport,
[`${ONYXKEYS.COLLECTION.REPORT}${submittedDestinationReport.reportID}`]: submittedDestinationReport,
},
isTrackIntentUser: false,
});
await waitForBatchedUpdates();
Expand Down
Loading