Skip to content

Commit

Permalink
test: format trackExpense test to following test guide
Browse files Browse the repository at this point in the history
  • Loading branch information
linhvovan29546 committed Dec 28, 2024
1 parent 3a9db14 commit 05dcf26
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ describe('actions/IOU', () => {
return Onyx.clear().then(waitForBatchedUpdates);
});
describe('trackExpense', () => {
it('categorize the tracked expense self DM', async () => {
// Setup fake data
it('category a distance expense of selfDM report', async () => {
// Given a participant of the report
const participant = {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID};

// Given waypoints of the transaction
const fakeWayPoints = {
waypoint0: {
keyForList: '88 Kearny Street_1735023533854',
Expand All @@ -99,16 +101,24 @@ describe('actions/IOU', () => {
name: 'Golden Gate Bridge Vista Point',
},
};

// Given a selfDM report
const selfDMReport = {
...createRandomReport(1),
chatType: CONST.REPORT.CHAT_TYPE.SELF_DM,
};

// Given a policyExpenseChat report
const expenseReport = {
...createRandomReport(1),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
};

// Given policy categories and a policy
const fakeCategories = createRandomPolicyCategories(3);
const fakePolicy = createRandomPolicy(1);

// Given a transaction with a distance request type
const fakeTransaction = {
...createRandomTransaction(1),
iouRequestType: CONST.IOU.REQUEST_TYPE.DISTANCE,
Expand All @@ -122,10 +132,11 @@ describe('actions/IOU', () => {
},
};

// Save the transaction draft in Onyx
// When the transaction is saved to draft before being submitted
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${fakeTransaction.transactionID}`, fakeTransaction);
mockFetch?.pause?.();
// Submit the tracked expense

// When the user submits the transaction to the selfDM report
IOU.trackExpense(
selfDMReport,
fakeTransaction.amount,
Expand Down Expand Up @@ -156,16 +167,23 @@ describe('actions/IOU', () => {
);
await waitForBatchedUpdates();
await mockFetch?.resume?.();

// Then the transaction must remain a distance request
const transaction = await new Promise<OnyxEntry<OnyxTypes.Transaction>>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions) => {
Onyx.disconnect(connection);
resolve(Object.values(transactions ?? {}).at(0));
const transaction = Object.values(transactions ?? {}).at(0);

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

View workflow job for this annotation

GitHub Actions / ESLint check

'transaction' is already declared in the upper scope on line 172 column 19

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

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'transaction' is already declared in the upper scope on line 172 column 19
const isDistanceRequest = TransactionUtils.isDistanceRequest(transaction);
expect(isDistanceRequest).toBe(true);
resolve(transaction);
},
});
});

// Given all report actions of the selfDM report
const allReportActions = await new Promise<OnyxCollection<OnyxTypes.ReportActions>>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
Expand All @@ -176,17 +194,17 @@ describe('actions/IOU', () => {
},
});
});
const selfDMReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`];

// The IOU report should have a ACTIONABLE_TRACK_EXPENSE_WHISPER action and IOU action
// Then the selfDM report should have an actionable track expense whisper action and an IOU action
const selfDMReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`];
expect(Object.values(selfDMReportActions ?? {}).length).toBe(2);

// Simulate step clear cache, then the local flag iouRequestType is removed
// When the cache is cleared before categorizing the tracked expense
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction?.transactionID}`, {
iouRequestType: null,
});

// Simulate step create draft transaction in the report screen
// When the transaction is saved to draft by selecting a category in the selfDM report
const reportAction = Object.values(selfDMReportActions ?? {}).find((reportAction) => ReportActionsUtils.isActionableTrackExpense(reportAction));

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

View workflow job for this annotation

GitHub Actions / ESLint check

'reportAction' is already declared in the upper scope on line 208 column 19

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

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'reportAction' is already declared in the upper scope on line 208 column 19
ReportUtils.createDraftTransactionAndNavigateToParticipantSelector(
transaction?.transactionID ?? '',

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

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Expand All @@ -195,6 +213,8 @@ describe('actions/IOU', () => {
reportAction?.reportActionID,
);
await waitForBatchedUpdates();

// Then the transaction draft should be saved successfully
const allTransactionsDraft = await new Promise<OnyxCollection<OnyxTypes.Transaction>>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
Expand All @@ -206,7 +226,8 @@ describe('actions/IOU', () => {
});
});
const transactionDraft = allTransactionsDraft?.[`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction?.transactionID}`];
// Categorize the tracked expense

// When the user confirms the category for the tracked expense
IOU.trackExpense(
expenseReport,
transactionDraft?.amount ?? fakeTransaction.amount,
Expand Down Expand Up @@ -238,15 +259,21 @@ describe('actions/IOU', () => {
await waitForBatchedUpdates();
await mockFetch?.resume?.();

// Verify the transaction remains a distance request
// Then the expense should be categorized successfully
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (transactions) => {
Onyx.disconnect(connection);
const isDistanceRequest = TransactionUtils.isDistanceRequest(transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction?.transactionID}`]);
const categorizedTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction?.transactionID}`];

// Then the transaction must remain a distance request
const isDistanceRequest = TransactionUtils.isDistanceRequest(categorizedTransaction);
expect(isDistanceRequest).toBe(true);

// Then the transaction category must match the original category
expect(categorizedTransaction?.category).toBe(Object.keys(fakeCategories).at(0) ?? '');
resolve();
},
});
Expand Down

0 comments on commit 05dcf26

Please sign in to comment.