Skip to content

Commit e4256d8

Browse files
MelvinBottruph01
andcommitted
Add unit tests for receipt field editing on closed reports
Tests that canEditFieldOfMoneyRequest returns false for the RECEIPT field when the expense report is closed, and true when open. Co-authored-by: truph01 <truph01@users.noreply.github.com>
1 parent b067a44 commit e4256d8

1 file changed

Lines changed: 103 additions & 0 deletions

File tree

tests/unit/canEditFieldOfMoneyRequestTest.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,107 @@ describe('canEditFieldOfMoneyRequest', () => {
297297
});
298298
});
299299
});
300+
301+
describe('receipt field', () => {
302+
const RECEIPT_IOU_REPORT_ID = '5001';
303+
const RECEIPT_IOU_TRANSACTION_ID = '5002';
304+
const RECEIPT_AMOUNT = 100;
305+
const receiptPolicyID = '5003';
306+
307+
const randomReportAction = createRandomReportAction(501);
308+
const adminPolicy = {...createRandomPolicy(Number(receiptPolicyID), CONST.POLICY.TYPE.TEAM), role: CONST.POLICY.ROLE.ADMIN};
309+
310+
const reportAction = {
311+
...randomReportAction,
312+
actionName: CONST.REPORT.ACTIONS.TYPE.IOU,
313+
actorAccountID: currentUserAccountID,
314+
childStateNum: CONST.REPORT.STATE_NUM.OPEN,
315+
childStatusNum: CONST.REPORT.STATUS_NUM.OPEN,
316+
originalMessage: {
317+
// eslint-disable-next-line @typescript-eslint/no-deprecated
318+
...randomReportAction.originalMessage,
319+
IOUReportID: RECEIPT_IOU_REPORT_ID,
320+
IOUTransactionID: RECEIPT_IOU_TRANSACTION_ID,
321+
type: CONST.IOU.ACTION.CREATE,
322+
amount: RECEIPT_AMOUNT,
323+
currency: CONST.CURRENCY.USD,
324+
},
325+
};
326+
327+
const moneyRequestTransaction = {
328+
...createRandomTransaction(Number(RECEIPT_IOU_TRANSACTION_ID)),
329+
reportID: RECEIPT_IOU_REPORT_ID,
330+
transactionID: RECEIPT_IOU_TRANSACTION_ID,
331+
amount: RECEIPT_AMOUNT,
332+
managedCard: false,
333+
status: CONST.TRANSACTION.STATUS.POSTED,
334+
};
335+
336+
beforeAll(() => {
337+
Onyx.init({keys: ONYXKEYS});
338+
339+
Onyx.multiSet({
340+
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID},
341+
});
342+
initOnyxDerivedValues();
343+
344+
return waitForBatchedUpdates();
345+
});
346+
347+
beforeEach(() => {
348+
const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [adminPolicy], (current) => current.id);
349+
Onyx.multiSet({
350+
[`${ONYXKEYS.COLLECTION.TRANSACTION}${RECEIPT_IOU_TRANSACTION_ID}`]: moneyRequestTransaction,
351+
...policyCollectionDataSet,
352+
});
353+
return waitForBatchedUpdates();
354+
});
355+
356+
afterEach(() => {
357+
Onyx.clear();
358+
return waitForBatchedUpdates();
359+
});
360+
361+
it('should return false for receipt field when the expense report is closed', async () => {
362+
// Given a closed expense report where the current user is an admin
363+
const closedExpenseReport = {
364+
...createExpenseReport(Number(RECEIPT_IOU_REPORT_ID)),
365+
policyID: receiptPolicyID,
366+
ownerAccountID: currentUserAccountID,
367+
managerID: secondUserAccountID,
368+
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
369+
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
370+
};
371+
372+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${RECEIPT_IOU_REPORT_ID}`, closedExpenseReport);
373+
await waitForBatchedUpdates();
374+
375+
// When the admin tries to edit the receipt field
376+
const canEditReceipt = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
377+
378+
// Then they should not be able to edit the receipt on a closed report
379+
expect(canEditReceipt).toBe(false);
380+
});
381+
382+
it('should return true for receipt field when the expense report is open', async () => {
383+
// Given an open expense report where the current user is an admin
384+
const openExpenseReport = {
385+
...createExpenseReport(Number(RECEIPT_IOU_REPORT_ID)),
386+
policyID: receiptPolicyID,
387+
ownerAccountID: currentUserAccountID,
388+
managerID: secondUserAccountID,
389+
stateNum: CONST.REPORT.STATE_NUM.OPEN,
390+
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
391+
};
392+
393+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${RECEIPT_IOU_REPORT_ID}`, openExpenseReport);
394+
await waitForBatchedUpdates();
395+
396+
// When the admin tries to edit the receipt field
397+
const canEditReceipt = canEditFieldOfMoneyRequest(reportAction, CONST.EDIT_REQUEST_FIELD.RECEIPT);
398+
399+
// Then they should be able to edit the receipt on an open report
400+
expect(canEditReceipt).toBe(true);
401+
});
402+
});
300403
});

0 commit comments

Comments
 (0)