Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor IOU.requestMoney function #52231

Merged
merged 12 commits into from
Nov 22, 2024
Prev Previous commit
Next Next commit
resolve conflict
mkzie2 committed Nov 20, 2024

Verified

This commit was signed with the committer’s verified signature.
commit 9bd88a2ac22f9273e27fd1f41fd4090cea8e8920
345 changes: 345 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
@@ -1683,6 +1683,351 @@
});
});

describe('edit expense', () => {
const amount = 10000;
const comment = '💸💸💸💸';
const merchant = 'NASDAQ';

afterEach(() => {
mockFetch?.resume?.();
});

it('updates the IOU request and IOU report when offline', () => {
let thread: OptimisticChatReport;
let iouReport: OnyxEntry<OnyxTypes.Report>;
let iouAction: OnyxEntry<OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>;
let transaction: OnyxEntry<OnyxTypes.Transaction>;

mockFetch?.pause?.();
IOU.requestMoney(
{
report: {reportID: ''},
payeeEmail: RORY_EMAIL,
payeeAccountID: RORY_ACCOUNT_ID,
participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID},
},
{
amount,
attendees: [],
currency: CONST.CURRENCY.USD,
created: '',
merchant,
comment,
},
);
return waitForBatchedUpdates()
.then(() => {
Onyx.set(ONYXKEYS.SESSION, {email: RORY_EMAIL, accountID: RORY_ACCOUNT_ID});
return waitForBatchedUpdates();
})
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connection);
iouReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.IOU);

resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`,
waitForCollectionCallback: false,
callback: (reportActionsForIOUReport) => {
Onyx.disconnect(connection);

[iouAction] = Object.values(reportActionsForIOUReport ?? {}).filter(
(reportAction): reportAction is OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => ReportActionsUtils.isMoneyRequestAction(reportAction),
);
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (allTransactions) => {
Onyx.disconnect(connection);

transaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t));
resolve();
},
});
}),
)
.then(() => {
thread = ReportUtils.buildTransactionThread(iouAction, iouReport) ?? null;
Onyx.set(`report_${thread?.reportID ?? '-1'}`, thread);
return waitForBatchedUpdates();
})
.then(() => {
if (transaction) {
IOU.editMoneyRequest(

Check failure on line 1777 in tests/actions/IOUTest.ts

GitHub Actions / Changed files ESLint check

Unsafe call of an `any` typed value

Check failure on line 1777 in tests/actions/IOUTest.ts

GitHub Actions / typecheck

Property 'editMoneyRequest' does not exist on type 'typeof import("/home/runner/work/App/App/src/libs/actions/IOU")'. Did you mean 'initMoneyRequest'?
transaction,
thread.reportID,
{amount: 20000, comment: 'Double the amount!'},
{
id: '123',
role: 'user',
type: CONST.POLICY.TYPE.TEAM,
name: '',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
},
{},
{},
);
}
return waitForBatchedUpdates();
})
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (allTransactions) => {
Onyx.disconnect(connection);

const updatedTransaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t));
expect(updatedTransaction?.modifiedAmount).toBe(20000);
expect(updatedTransaction?.comment).toMatchObject({comment: 'Double the amount!'});
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
waitForCollectionCallback: false,
callback: (allActions) => {
Onyx.disconnect(connection);
const updatedAction = Object.values(allActions ?? {}).find((reportAction) => !isEmptyObject(reportAction));
expect(updatedAction?.actionName).toEqual('MODIFIEDEXPENSE');
expect(updatedAction && ReportActionsUtils.getOriginalMessage(updatedAction)).toEqual(
expect.objectContaining({amount: 20000, newComment: 'Double the amount!', oldAmount: amount, oldComment: comment}),
);
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connection);
const updatedIOUReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.IOU);
const updatedChatReport = Object.values(allReports ?? {}).find((report) => report?.reportID === iouReport?.chatReportID);
expect(updatedIOUReport).toEqual(
expect.objectContaining({
total: 20000,
cachedTotal: '$200.00',
lastMessageHtml: 'submitted $200.00',
lastMessageText: 'submitted $200.00',
}),
);
expect(updatedChatReport).toEqual(
expect.objectContaining({
lastMessageHtml: `${CARLOS_EMAIL} owes $200.00`,
lastMessageText: `${CARLOS_EMAIL} owes $200.00`,
}),
);
resolve();
},
});
}),
)
.then(() => {
mockFetch?.resume?.();
});
});

it('resets the IOU request and IOU report when api returns an error', () => {
let thread: OptimisticChatReport;
let iouReport: OnyxEntry<OnyxTypes.Report>;
let iouAction: OnyxEntry<OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>>;
let transaction: OnyxEntry<OnyxTypes.Transaction>;

IOU.requestMoney(
{
report: {reportID: ''},
payeeEmail: RORY_EMAIL,
payeeAccountID: RORY_ACCOUNT_ID,
participant: {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID},
},
{
amount,
attendees: [],
currency: CONST.CURRENCY.USD,
created: '',
merchant,
comment,
},
);
return waitForBatchedUpdates()
.then(() => {
Onyx.set(ONYXKEYS.SESSION, {email: RORY_EMAIL, accountID: RORY_ACCOUNT_ID});
return waitForBatchedUpdates();
})
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connection);
[iouReport] = Object.values(allReports ?? {}).filter((report) => report?.type === CONST.REPORT.TYPE.IOU);
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport?.reportID}`,
waitForCollectionCallback: false,
callback: (reportActionsForIOUReport) => {
Onyx.disconnect(connection);

[iouAction] = Object.values(reportActionsForIOUReport ?? {}).filter(
(reportAction): reportAction is OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => ReportActionsUtils.isMoneyRequestAction(reportAction),
);
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (allTransactions) => {
Onyx.disconnect(connection);

transaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t));
resolve();
},
});
}),
)
.then(() => {
thread = ReportUtils.buildTransactionThread(iouAction, iouReport);
Onyx.set(`report_${thread.reportID}`, thread);
return waitForBatchedUpdates();
})
.then(() => {
mockFetch?.fail?.();

if (transaction) {
IOU.editMoneyRequest(

Check failure on line 1947 in tests/actions/IOUTest.ts

GitHub Actions / Changed files ESLint check

Unsafe call of an `any` typed value

Check failure on line 1947 in tests/actions/IOUTest.ts

GitHub Actions / typecheck

Property 'editMoneyRequest' does not exist on type 'typeof import("/home/runner/work/App/App/src/libs/actions/IOU")'. Did you mean 'initMoneyRequest'?
transaction,
thread.reportID,
{amount: 20000, comment: 'Double the amount!'},
{
id: '123',
role: 'user',
type: CONST.POLICY.TYPE.TEAM,
name: '',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
},
{},
{},
);
}
return waitForBatchedUpdates();
})
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (allTransactions) => {
Onyx.disconnect(connection);

const updatedTransaction = Object.values(allTransactions ?? {}).find((t) => !isEmptyObject(t));
expect(updatedTransaction?.modifiedAmount).toBe(undefined);
expect(updatedTransaction?.amount).toBe(10000);
expect(updatedTransaction?.comment).toMatchObject({comment});
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
waitForCollectionCallback: false,
callback: (allActions) => {
Onyx.disconnect(connection);
const updatedAction = Object.values(allActions ?? {}).find((reportAction) => !isEmptyObject(reportAction));
expect(updatedAction?.actionName).toEqual('MODIFIEDEXPENSE');
expect(Object.values(updatedAction?.errors ?? {}).at(0)).toEqual(Localize.translateLocal('iou.error.genericEditFailureMessage'));
resolve();
},
});
}),
)
.then(
() =>
new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (allReports) => {
Onyx.disconnect(connection);
const updatedIOUReport = Object.values(allReports ?? {}).find((report) => report?.type === CONST.REPORT.TYPE.IOU);
const updatedChatReport = Object.values(allReports ?? {}).find((report) => report?.reportID === iouReport?.chatReportID);
expect(updatedIOUReport).toEqual(
expect.objectContaining({
total: 10000,
cachedTotal: '$100.00',
lastMessageHtml: `submitted $${amount / 100}.00 for ${comment}`,
lastMessageText: `submitted $${amount / 100}.00 for ${comment}`,
}),
);
expect(updatedChatReport).toEqual(
expect.objectContaining({
lastMessageHtml: '',
}),
);
resolve();
},
});
}),
);
});
});

describe('pay expense report via ACH', () => {
const amount = 10000;
const comment = '💸💸💸💸';