From aaa19d8e2ce4351b2cc7205130f2ab6c812bf4f1 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:19:10 +0000 Subject: [PATCH 01/24] add delete a money request successfully test case --- tests/actions/IOUTest.js | 156 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 6fbbe19cec8e..f93ac0460a4e 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1370,4 +1370,160 @@ describe('actions/IOU', () => { ); }); }); + + describe('deleteMoneyRequest', () => { + const amount = 10000; + const comment = 'Send me money please'; + let chatReport; + let iouReport; + let createIOUAction; + let transaction; + + beforeEach(() => { + // Create a money request + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); + return waitForPromisesToResolve() + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + + expect(_.size(allReports)).toBe(2); + + chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); + expect(chatReport).toBeTruthy(); + expect(chatReport).toHaveProperty('reportID'); + expect(chatReport).toHaveProperty('iouReportID'); + expect(chatReport.hasOutstandingIOU).toBe(true); + + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + expect(chatReport.iouReportID).toBe(iouReport.reportID); + expect(iouReport.chatReportID).toBe(chatReport.reportID); + + expect(chatReport.pendingFields).toBeFalsy(); + expect(iouReport.pendingFields).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (allReportActions) => { + Onyx.disconnect(connectionID); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; + + createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeTruthy(); + expect(createIOUAction.originalMessage.IOUReportID).toBe(iouReport.reportID); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (allTransactions) => { + Onyx.disconnect(connectionID); + expect(_.size(allTransactions)).toBe(1); + transaction = _.find(allTransactions, (t) => t); + expect(transaction).toBeTruthy(); + expect(transaction.amount).toBe(amount); + expect(transaction.reportID).toBe(iouReport.reportID); + expect(createIOUAction.originalMessage.IOUTransactionID).toBe(transaction.transactionID); + resolve(); + }, + }); + }), + ); + }); + it('delete a money request (IOU Action and transaction) successfully', () => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + return waitForPromisesToResolve() + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.chatReportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + iouAction = _.find(reportActionsForReport, (reportAction) => reportAction.reportActionID === createIOUAction.reportActionID); + expect(iouAction).toBeFalsy(); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, + waitForCollectionCallback: true, + callback: (transaction) => { + Onyx.disconnect(connectionID); + + expect(transaction).toBeFalsy(); + + resolve(); + }, + }); + }), + ) + + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.chatReportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + iouAction = _.find(reportActionsForReport, (reportAction) => reportAction.reportActionID === createIOUAction.reportActionID); + expect(iouAction).toBeFalsy(); + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, + waitForCollectionCallback: true, + callback: (transaction) => { + Onyx.disconnect(connectionID); + + expect(transaction).toBeFalsy(); + + resolve(); + }, + }); + }), + ); + }); + }); }); From 07273f39f026b3222dc38f9165a8fb374aaa7063 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:20:26 +0000 Subject: [PATCH 02/24] delete the IOU report when there are no visible comments left in the IOU report test case --- tests/actions/IOUTest.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index f93ac0460a4e..ceb2834d8f52 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1525,5 +1525,39 @@ describe('actions/IOU', () => { }), ); }); + it('delete the IOU report when there are no visible comments left in the IOU report', () => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + return waitForPromisesToResolve() + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeFalsy(); + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeFalsy(); + resolve(); + }, + }); + }), + ); + }); }); }); From a34be23db01fda87b536607f7442c8568bc914e1 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:23:18 +0000 Subject: [PATCH 03/24] draft: add does not delete the IOU report when there are visible comments left in the IOU report test case --- tests/actions/IOUTest.js | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index ceb2834d8f52..b3d9bbd51599 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1559,5 +1559,74 @@ describe('actions/IOU', () => { }), ); }); + it('does not delete the IOU report when there are visible comments left in the IOU report', () => { + let reportActions; + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + callback: (val) => (reportActions = val), + }); + Report.addComment(iouReport.reportID, 'Test Comment'); + return waitForPromisesToResolve() + .then(() => { + const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + expect(action).toBeTruthy(); + expect(action.message[0].text).toBe('Test Comment'); + Onyx.disconnect(connectionID); + }) + .then(() => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + return waitForPromisesToResolve(); + }) + .then(() => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + callback: (val) => (reportActions = val), + }); + const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + expect(action).toBeTruthy(); + expect(action.message[0].text).toBe('Test Comment'); + Onyx.disconnect(connectionID); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + resolve(); + }, + }); + }), + ); + }); }); }); From d02e3cb30fcc8a931e2c961aacad486fb9f5415b Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:42:47 +0000 Subject: [PATCH 04/24] fix lint errors --- tests/actions/IOUTest.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index b3d9bbd51599..a32dd93d1e6b 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1378,6 +1378,7 @@ describe('actions/IOU', () => { let iouReport; let createIOUAction; let transaction; + let iouAction; beforeEach(() => { // Create a money request @@ -1481,10 +1482,10 @@ describe('actions/IOU', () => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, waitForCollectionCallback: true, - callback: (transaction) => { + callback: (t) => { Onyx.disconnect(connectionID); - expect(transaction).toBeFalsy(); + expect(t).toBeFalsy(); resolve(); }, @@ -1514,10 +1515,10 @@ describe('actions/IOU', () => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, waitForCollectionCallback: true, - callback: (transaction) => { + callback: (tr) => { Onyx.disconnect(connectionID); - expect(transaction).toBeFalsy(); + expect(tr).toBeFalsy(); resolve(); }, @@ -1561,7 +1562,7 @@ describe('actions/IOU', () => { }); it('does not delete the IOU report when there are visible comments left in the IOU report', () => { let reportActions; - const connectionID = Onyx.connect({ + const connection = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, callback: (val) => (reportActions = val), }); @@ -1586,7 +1587,7 @@ describe('actions/IOU', () => { const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); expect(action).toBeTruthy(); expect(action.message[0].text).toBe('Test Comment'); - Onyx.disconnect(connectionID); + Onyx.disconnect(connection); }) .then( () => From 22ba7a900bc07b5264cf70e491af537cd01492bb Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:47:24 +0000 Subject: [PATCH 05/24] fix lint errors --- tests/actions/IOUTest.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index a32dd93d1e6b..a882e3ca9f26 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1572,7 +1572,7 @@ describe('actions/IOU', () => { const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); expect(action).toBeTruthy(); expect(action.message[0].text).toBe('Test Comment'); - Onyx.disconnect(connectionID); + Onyx.disconnect(connection); }) .then(() => { fetch.pause(); @@ -1587,7 +1587,7 @@ describe('actions/IOU', () => { const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); expect(action).toBeTruthy(); expect(action.message[0].text).toBe('Test Comment'); - Onyx.disconnect(connection); + Onyx.disconnect(connectionID); }) .then( () => From b498321a054fe03e6afc1518eb3563c58573a0bb Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Thu, 31 Aug 2023 13:54:45 +0000 Subject: [PATCH 06/24] add remaining test cases (draft) --- tests/actions/IOUTest.js | 672 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 640 insertions(+), 32 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index a882e3ca9f26..784478bbdc48 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -9,6 +9,27 @@ import DateUtils from '../../src/libs/DateUtils'; import * as NumberUtils from '../../src/libs/NumberUtils'; import * as ReportActions from '../../src/libs/actions/ReportActions'; import * as Report from '../../src/libs/actions/Report'; +import * as ReportUtils from '../../src/libs/ReportUtils'; +import * as PersonalDetailsUtils from '../../src/libs/PersonalDetailsUtils'; +import * as User from '../../src/libs/actions/User'; +import PusherHelper from '../utils/PusherHelper'; +import Navigation from '../../src/libs/Navigation/Navigation'; +import ROUTES from '../../src/ROUTES'; + +jest.mock('../../src/libs/actions/Report', () => { + const originalModule = jest.requireActual('../../src/libs/actions/Report'); + + return { + ...originalModule, + showReportActionNotification: jest.fn(), + }; +}); + +jest.mock('../../src/libs/Navigation/Navigation', () => ({ + navigate: jest.fn(), + dismissModal: jest.fn(), + goBack: jest.fn(), +})); const CARLOS_EMAIL = 'cmartins@expensifail.com'; const CARLOS_ACCOUNT_ID = 1; @@ -1378,12 +1399,37 @@ describe('actions/IOU', () => { let iouReport; let createIOUAction; let transaction; - let iouAction; + let thread; + const TEST_USER_ACCOUNT_ID = 1; + const TEST_USER_LOGIN = 'test@test.com'; + let REPORT_ID; + let reportActionID; + const REPORT_ACTION = { + actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, + actorAccountID: TEST_USER_ACCOUNT_ID, + automatic: false, + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/avatar_3.png', + message: [{type: 'COMMENT', html: 'Testing a comment', text: 'Testing a comment', translationKey: ''}], + person: [{type: 'TEXT', style: 'strong', text: 'test@test.com'}], + shouldShow: true, + }; + + let reportActions; beforeEach(() => { - // Create a money request - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment); - return waitForPromisesToResolve() + jest.clearAllMocks(); + PusherHelper.setup(); + // Set up Onyx with some test user data + return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN) + .then(() => { + User.subscribeToUserEvents(); + return waitForPromisesToResolve(); + }) + .then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID)) + .then(() => { + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment); + return waitForPromisesToResolve(); + }) .then( () => new Promise((resolve) => { @@ -1409,8 +1455,66 @@ describe('actions/IOU', () => { expect(chatReport.iouReportID).toBe(iouReport.reportID); expect(iouReport.chatReportID).toBe(chatReport.reportID); - expect(chatReport.pendingFields).toBeFalsy(); - expect(iouReport.pendingFields).toBeFalsy(); + REPORT_ID = chatReport.iouReportID; + + resolve(); + }, + }); + }), + ) + .then(() => { + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + reportID: chatReport.reportID, + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: { + reportID: iouReport.reportID, + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + ]); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + + expect(_.size(allReports)).toBe(2); + + chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + + expect(chatReport.iouReportID).toBe(iouReport.reportID); + expect(iouReport.chatReportID).toBe(chatReport.reportID); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + waitForCollectionCallback: true, + callback: (val) => { + Onyx.disconnect(connectionID); + + reportActions = val; resolve(); }, @@ -1457,6 +1561,8 @@ describe('actions/IOU', () => { }), ); }); + afterEach(PusherHelper.teardown); + it('delete a money request (IOU Action and transaction) successfully', () => { fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -1465,12 +1571,12 @@ describe('actions/IOU', () => { () => new Promise((resolve) => { const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.chatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, waitForCollectionCallback: true, callback: (reportActionsForReport) => { Onyx.disconnect(connectionID); - iouAction = _.find(reportActionsForReport, (reportAction) => reportAction.reportActionID === createIOUAction.reportActionID); - expect(iouAction).toBeFalsy(); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeFalsy(); resolve(); }, }); @@ -1498,12 +1604,12 @@ describe('actions/IOU', () => { () => new Promise((resolve) => { const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.chatReportID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, waitForCollectionCallback: true, callback: (reportActionsForReport) => { Onyx.disconnect(connectionID); - iouAction = _.find(reportActionsForReport, (reportAction) => reportAction.reportActionID === createIOUAction.reportActionID); - expect(iouAction).toBeFalsy(); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeFalsy(); resolve(); }, }); @@ -1526,6 +1632,7 @@ describe('actions/IOU', () => { }), ); }); + it('delete the IOU report when there are no visible comments left in the IOU report', () => { fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -1560,34 +1667,378 @@ describe('actions/IOU', () => { }), ); }); - it('does not delete the IOU report when there are visible comments left in the IOU report', () => { - let reportActions; - const connection = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - callback: (val) => (reportActions = val), - }); - Report.addComment(iouReport.reportID, 'Test Comment'); - return waitForPromisesToResolve() + + it('does not delete the IOU report when there are visible comments left in the IOU report', () => + waitForPromisesToResolve() + .then(() => { + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + callback: (val) => (reportActions = val), + }); + return waitForPromisesToResolve(); + }) + .then(() => { + Report.addComment(REPORT_ID, 'Testing a comment'); + return waitForPromisesToResolve(); + }) + .then(() => { - const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - expect(action).toBeTruthy(); - expect(action.message[0].text).toBe('Test Comment'); - Onyx.disconnect(connection); + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, + value: { + reportID: REPORT_ID, + notificationPreference: 'always', + lastVisibleActionCreated: '2023-08-29 03:48:27.267', + lastMessageText: 'Testing a comment', + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + value: { + [reportActionID]: {pendingAction: null}, + }, + }, + ]); + return waitForPromisesToResolve(); + }) + .then(() => { + // Verify there is three action (created + iou + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(3); + + const resultAction = reportActions[reportActionID]; + + // Verify that our action is no longer in the loading state + expect(resultAction.pendingAction).toBeNull(); }) .then(() => { fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); + + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + resolve(); + }, + }); + }), + )); + + it('delete the transaction thread if there are no visible comments in the thread', () => + waitForPromisesToResolve() + .then(() => { + thread = ReportUtils.buildTransactionThread(createIOUAction); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then(() => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeFalsy(); + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeFalsy(); + resolve(); + }, + }); + }), + )); + + it('does not delete the transaction thread if there are visible comments in the thread', () => + waitForPromisesToResolve() + .then(() => { + thread = ReportUtils.buildTransactionThread(createIOUAction); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + }) + .then(() => { + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + callback: (val) => (reportActions = val), + }); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then(() => { + Report.addComment(thread.reportID, 'Testing a comment'); + return waitForPromisesToResolve(); + }) + + .then(() => { + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + value: { + reportID: thread.reportID, + notificationPreference: 'always', + lastVisibleActionCreated: '2023-08-29 03:48:27.267', + lastMessageText: 'Testing a comment', + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + value: { + [reportActionID]: {pendingAction: null}, + }, + }, + ]); + return waitForPromisesToResolve(); + }) + .then(() => { + // Verify there is 2 action (created + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(2); + + const resultAction = reportActions[reportActionID]; + + // Verify that our action is no longer in the loading state + expect(resultAction.pendingAction).toBeNull(); + }) + .then(() => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); return waitForPromisesToResolve(); }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }), + )); + + it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', () => + waitForPromisesToResolve() + .then(() => { + thread = ReportUtils.buildTransactionThread(createIOUAction); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + }) .then(() => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); - const action = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - expect(action).toBeTruthy(); - expect(action.message[0].text).toBe('Test Comment'); - Onyx.disconnect(connectionID); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then(() => { + Report.addComment(thread.reportID, 'Testing a comment'); + return waitForPromisesToResolve(); + }) + + .then(() => { + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + value: { + reportID: thread.reportID, + notificationPreference: 'always', + lastVisibleActionCreated: '2023-08-29 03:48:27.267', + lastMessageText: 'Testing a comment', + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + value: { + [reportActionID]: {pendingAction: null}, + }, + }, + ]); + return waitForPromisesToResolve(); + }) + .then(() => { + // Verify there is 2 action (created + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(2); + + const resultAction = reportActions[reportActionID]; + + // Verify that our action is no longer in the loading state + expect(resultAction.pendingAction).toBeNull(); + }) + .then(() => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); + resolve(); + }, + }); + }), + ) + .then(fetch.resume) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); + resolve(); + }, + }); + }), + )); + + it('update IOU report and reportPreview with new totals and messages if the IOU report is not deleted', () => { + const amount2 = 20000; + const comment2 = 'Send me money please 2'; + return waitForPromisesToResolve() + .then(() => { + IOU.requestMoney({}, amount2, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment2); + return waitForPromisesToResolve(); }) .then( () => @@ -1597,12 +2048,80 @@ describe('actions/IOU', () => { waitForCollectionCallback: true, callback: (allReports) => { Onyx.disconnect(connectionID); + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(30000); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (allReportActions) => { + Onyx.disconnect(connectionID); + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; + + const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); + expect(ioupreview).toBeTruthy(); + expect(ioupreview.message[0].text).toBe('Test owes $300.00'); + + resolve(); + }, + }); + }), + ) + .then(() => { + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + return waitForPromisesToResolve(); + }) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (allReports) => { + Onyx.disconnect(connectionID); iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(20000); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (allReportActions) => { + Onyx.disconnect(connectionID); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; + + const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); + expect(ioupreview).toBeTruthy(); + expect(ioupreview.message[0].text).toBe('Test owes $200.00'); + resolve(); }, }); @@ -1617,17 +2136,106 @@ describe('actions/IOU', () => { waitForCollectionCallback: true, callback: (allReports) => { Onyx.disconnect(connectionID); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(20000); + + resolve(); + }, + }); + }), + ) + .then( + () => + new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (allReportActions) => { + Onyx.disconnect(connectionID); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; + + const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); + expect(ioupreview).toBeFalsy(); + expect(ioupreview.total).toBe(20000); + expect(ioupreview.message[0].text).toBe('Test owes $200.00'); + resolve(); }, }); }), ); }); + + it('navigate the user correctly to the iou Report when appropriate', () => + waitForPromisesToResolve() + .then(() => { + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + callback: (val) => (reportActions = val), + }); + return waitForPromisesToResolve(); + }) + .then(() => { + Report.addComment(REPORT_ID, 'Testing a comment'); + return waitForPromisesToResolve(); + }) + + .then(() => { + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, + value: { + reportID: REPORT_ID, + notificationPreference: 'always', + lastVisibleActionCreated: '2023-08-29 03:48:27.267', + lastMessageText: 'Testing a comment', + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + value: { + [reportActionID]: {pendingAction: null}, + }, + }, + ]); + return waitForPromisesToResolve(); + }) + .then(() => { + // Verify there is three action (created + iou + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(3); + + const resultAction = reportActions[reportActionID]; + + // Verify that our action is no longer in the loading state + expect(resultAction.pendingAction).toBeNull(); + }) + .then(() => { + jest.clearAllMocks(); + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(iouReport.reportID)); + })); + + it('navigate the user correctly to the chat Report when appropriate', () => { + jest.clearAllMocks(); + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(chatReport.reportID)); + }); }); }); From 4f2b1cf58142c24ef99604bc030b6f62db164c1f Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Mon, 4 Sep 2023 15:27:11 +0200 Subject: [PATCH 07/24] remove unecessary mock --- tests/actions/IOUTest.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 784478bbdc48..dfef6300cdba 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -16,15 +16,6 @@ import PusherHelper from '../utils/PusherHelper'; import Navigation from '../../src/libs/Navigation/Navigation'; import ROUTES from '../../src/ROUTES'; -jest.mock('../../src/libs/actions/Report', () => { - const originalModule = jest.requireActual('../../src/libs/actions/Report'); - - return { - ...originalModule, - showReportActionNotification: jest.fn(), - }; -}); - jest.mock('../../src/libs/Navigation/Navigation', () => ({ navigate: jest.fn(), dismissModal: jest.fn(), From ac25761903d8b6d912689ee56e49fdb6cc3476a8 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 17:52:30 +0200 Subject: [PATCH 08/24] refactor beforeEach to use async/await instead --- tests/actions/IOUTest.js | 241 ++++++++++++++++----------------------- 1 file changed, 100 insertions(+), 141 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index fc1f53857b6a..3e6a0566a27a 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1409,150 +1409,109 @@ describe('actions/IOU', () => { let reportActions; - beforeEach(() => { + beforeEach(async () => { jest.clearAllMocks(); PusherHelper.setup(); // Set up Onyx with some test user data - return TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN) - .then(() => { - User.subscribeToUserEvents(); - return waitForPromisesToResolve(); - }) - .then(() => TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID)) - .then(() => { - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - - expect(_.size(allReports)).toBe(2); - - chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); - expect(chatReport).toBeTruthy(); - expect(chatReport).toHaveProperty('reportID'); - expect(chatReport).toHaveProperty('iouReportID'); - expect(chatReport.hasOutstandingIOU).toBe(true); - - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); - - expect(chatReport.iouReportID).toBe(iouReport.reportID); - expect(iouReport.chatReportID).toBe(chatReport.reportID); - - REPORT_ID = chatReport.iouReportID; - - resolve(); - }, - }); - }), - ) - .then(() => { - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - value: { - reportID: chatReport.reportID, - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - value: { - reportID: iouReport.reportID, - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - ]); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - - expect(_.size(allReports)).toBe(2); - - chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - - expect(chatReport.iouReportID).toBe(iouReport.reportID); - expect(iouReport.chatReportID).toBe(chatReport.reportID); - - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, - waitForCollectionCallback: true, - callback: (val) => { - Onyx.disconnect(connectionID); - - reportActions = val; - - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - - const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; - - createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - expect(createIOUAction).toBeTruthy(); - expect(createIOUAction.originalMessage.IOUReportID).toBe(iouReport.reportID); - - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.TRANSACTION, - waitForCollectionCallback: true, - callback: (allTransactions) => { - Onyx.disconnect(connectionID); - expect(_.size(allTransactions)).toBe(1); - transaction = _.find(allTransactions, (t) => t); - expect(transaction).toBeTruthy(); - expect(transaction.amount).toBe(amount); - expect(transaction.reportID).toBe(iouReport.reportID); - expect(createIOUAction.originalMessage.IOUTransactionID).toBe(transaction.transactionID); - resolve(); - }, - }); - }), - ); + await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); + User.subscribeToUserEvents(); + await waitForPromisesToResolve(); + await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID); + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment); + await waitForPromisesToResolve(); + let allReports = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + Onyx.disconnect(connectionID); + resolve(reports); + }, + }); + }); + + expect(_.size(allReports)).toBe(2); + + chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); + expect(chatReport).toBeTruthy(); + expect(chatReport).toHaveProperty('reportID'); + expect(chatReport).toHaveProperty('iouReportID'); + expect(chatReport.hasOutstandingIOU).toBe(true); + + iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + expect(chatReport.iouReportID).toBe(iouReport.reportID); + expect(iouReport.chatReportID).toBe(chatReport.reportID); + + REPORT_ID = chatReport.iouReportID; + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + reportID: chatReport.reportID, + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + value: { + reportID: iouReport.reportID, + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + ]); + + await waitForPromisesToResolve(); + + reportActions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + waitForCollectionCallback: true, + callback: (val) => { + Onyx.disconnect(connectionID); + resolve(val); + }, + }); + }); + + const allReportActions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (actions) => { + Onyx.disconnect(connectionID); + resolve(actions); + }, + }); + }); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; + createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeTruthy(); + expect(createIOUAction.originalMessage.IOUReportID).toBe(iouReport.reportID); + + const allTransactions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.TRANSACTION, + waitForCollectionCallback: true, + callback: (transactions) => { + Onyx.disconnect(connectionID); + resolve(transactions); + }, + }); + }); + + transaction = _.find(allTransactions, (t) => t); + expect(transaction).toBeTruthy(); + expect(transaction.amount).toBe(amount); + expect(transaction.reportID).toBe(iouReport.reportID); + expect(createIOUAction.originalMessage.IOUTransactionID).toBe(transaction.transactionID); }); afterEach(PusherHelper.teardown); From 8c437b9e163ef226069a08d25e7e02ed7700d04f Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 17:53:49 +0200 Subject: [PATCH 09/24] fix does not delete the IOU report when there are visible comments left in the IOU report test case --- tests/actions/IOUTest.js | 155 +++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 88 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 3e6a0566a27a..eb623b1031a4 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1620,101 +1620,80 @@ describe('actions/IOU', () => { ); }); - it('does not delete the IOU report when there are visible comments left in the IOU report', () => - waitForPromisesToResolve() - .then(() => { - Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, - callback: (val) => (reportActions = val), - }); - return waitForPromisesToResolve(); - }) - .then(() => { - Report.addComment(REPORT_ID, 'Testing a comment'); - return waitForPromisesToResolve(); - }) + it('does not delete the IOU report when there are visible comments left in the IOU report', async () => { + await waitForPromisesToResolve(); - .then(() => { - const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - reportActionID = resultAction.reportActionID; - expect(resultAction.message).toEqual(REPORT_ACTION.message); - expect(resultAction.person).toEqual(REPORT_ACTION.person); - expect(resultAction.pendingAction).toBeNull(); + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, - value: { - reportID: REPORT_ID, - notificationPreference: 'always', - lastVisibleActionCreated: '2023-08-29 03:48:27.267', - lastMessageText: 'Testing a comment', - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, - value: { - [reportActionID]: {pendingAction: null}, - }, - }, - ]); - return waitForPromisesToResolve(); - }) - .then(() => { - // Verify there is three action (created + iou + addcomment) and our optimistic comment has been removed - expect(_.size(reportActions)).toBe(3); + jest.advanceTimersByTime(10); - const resultAction = reportActions[reportActionID]; + Report.addComment(REPORT_ID, 'Testing a comment'); + await waitForPromisesToResolve(); - // Verify that our action is no longer in the loading state - expect(resultAction.pendingAction).toBeNull(); - }) - .then(() => { - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); + await waitForPromisesToResolve(); - resolve(); - }, - }); - }), - )); + // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(3); + + const resultActionAfterUpdate = reportActions[reportActionID]; + //expect(resultActionAfterUpdate).toBeFalsy(); + + + // Verify that our action is no longer in the loading state + expect(resultActionAfterUpdate.pendingAction).toBeNull(); + + fetch.pause(); + + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + await waitForPromisesToResolve(); + + let allReports = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + Onyx.disconnect(connectionID); + resolve(reports); + }, + }); + }); + + await waitForPromisesToResolve(); + + iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + fetch.resume(); + + allReports = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + Onyx.disconnect(connectionID); + resolve(reports); + }, + }); + }); + + iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + }); it('delete the transaction thread if there are no visible comments in the thread', () => waitForPromisesToResolve() From 93dd9d60b2be1fba5ccd9dde44de55ff0645c722 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:16:29 +0200 Subject: [PATCH 10/24] fix delete the transaction thread if there are no visible comments in the thread test case --- tests/actions/IOUTest.js | 134 +++++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 55 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index eb623b1031a4..a57d1c9ce561 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1695,61 +1695,85 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); }); - it('delete the transaction thread if there are no visible comments in the thread', () => - waitForPromisesToResolve() - .then(() => { - thread = ReportUtils.buildTransactionThread(createIOUAction); - const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); - Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeTruthy(); - resolve(); - }, - }); - }), - ) - .then(() => { - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeFalsy(); - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeFalsy(); - resolve(); - }, - }); - }), - )); + it('delete the transaction thread if there are no visible comments in the thread', async () => { + await waitForPromisesToResolve(); + + + jest.advanceTimersByTime(10); + thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); + + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); + + jest.advanceTimersByTime(10); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + await waitForPromisesToResolve(); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + value: { + [createIOUAction.reportActionID]: {childReportID: thread.reportID}, + }, + }, + ]); + + await waitForPromisesToResolve(); + + const allReportActions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (actions) => { + Onyx.disconnect(connectionID); + resolve(actions); + }, + }); + }); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; + createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.childReportID).toBe(thread.reportID); + + await waitForPromisesToResolve(); + + fetch.pause(); + jest.advanceTimersByTime(10); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + await waitForPromisesToResolve(); + + report = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (reportData) => { + Onyx.disconnect(connectionID); + resolve(reportData); + }, + }); + }); + + expect(report).toBeFalsy(); + fetch.resume(); + + report = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (reportData) => { + Onyx.disconnect(connectionID); + resolve(reportData); + }, + }); + }); + + expect(report).toBeFalsy(); + }); it('does not delete the transaction thread if there are visible comments in the thread', () => waitForPromisesToResolve() From 907c3dc026927ebf026bd8b8f9090e87715ed751 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:28:34 +0200 Subject: [PATCH 11/24] fix navigate the user correctly to the iou Report when appropriate test case --- tests/actions/IOUTest.js | 198 +++++++++++++++++++++++++-------------- 1 file changed, 129 insertions(+), 69 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index a57d1c9ce561..eb99223d5fd7 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1649,7 +1649,6 @@ describe('actions/IOU', () => { const resultActionAfterUpdate = reportActions[reportActionID]; //expect(resultActionAfterUpdate).toBeFalsy(); - // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); @@ -1697,22 +1696,21 @@ describe('actions/IOU', () => { it('delete the transaction thread if there are no visible comments in the thread', async () => { await waitForPromisesToResolve(); - - + jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); - + Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); await waitForPromisesToResolve(); - + jest.advanceTimersByTime(10); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); await waitForPromisesToResolve(); - + PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, @@ -1722,9 +1720,9 @@ describe('actions/IOU', () => { }, }, ]); - + await waitForPromisesToResolve(); - + const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -1735,18 +1733,18 @@ describe('actions/IOU', () => { }, }); }); - + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction.childReportID).toBe(thread.reportID); - + await waitForPromisesToResolve(); - + fetch.pause(); jest.advanceTimersByTime(10); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); - + report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1757,10 +1755,10 @@ describe('actions/IOU', () => { }, }); }); - + expect(report).toBeFalsy(); fetch.resume(); - + report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1771,7 +1769,7 @@ describe('actions/IOU', () => { }, }); }); - + expect(report).toBeFalsy(); }); @@ -2127,64 +2125,126 @@ describe('actions/IOU', () => { ); }); - it('navigate the user correctly to the iou Report when appropriate', () => - waitForPromisesToResolve() - .then(() => { - Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, - callback: (val) => (reportActions = val), - }); - return waitForPromisesToResolve(); - }) - .then(() => { - Report.addComment(REPORT_ID, 'Testing a comment'); - return waitForPromisesToResolve(); - }) + it('navigate the user correctly to the iou Report when appropriate', async () => { + await waitForPromisesToResolve(); - .then(() => { - const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - reportActionID = resultAction.reportActionID; - expect(resultAction.message).toEqual(REPORT_ACTION.message); - expect(resultAction.person).toEqual(REPORT_ACTION.person); - expect(resultAction.pendingAction).toBeNull(); + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, - value: { - reportID: REPORT_ID, - notificationPreference: 'always', - lastVisibleActionCreated: '2023-08-29 03:48:27.267', - lastMessageText: 'Testing a comment', - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, - value: { - [reportActionID]: {pendingAction: null}, - }, - }, - ]); - return waitForPromisesToResolve(); - }) - .then(() => { - // Verify there is three action (created + iou + addcomment) and our optimistic comment has been removed - expect(_.size(reportActions)).toBe(3); + jest.advanceTimersByTime(10); - const resultAction = reportActions[reportActionID]; + Report.addComment(REPORT_ID, 'Testing a comment'); + await waitForPromisesToResolve(); - // Verify that our action is no longer in the loading state - expect(resultAction.pendingAction).toBeNull(); - }) - .then(() => { - jest.clearAllMocks(); - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(iouReport.reportID)); - })); + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); + + await waitForPromisesToResolve(); + + // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(3); + + const resultActionAfterUpdate = reportActions[reportActionID]; + //expect(resultActionAfterUpdate).toBeFalsy(); + + // Verify that our action is no longer in the loading state + expect(resultActionAfterUpdate.pendingAction).toBeNull(); + + await waitForPromisesToResolve(); + + jest.advanceTimersByTime(10); + thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); + + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); + + jest.advanceTimersByTime(10); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + await waitForPromisesToResolve(); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + value: { + [createIOUAction.reportActionID]: {childReportID: thread.reportID}, + }, + }, + ]); + + await waitForPromisesToResolve(); + + const allReportActions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (actions) => { + Onyx.disconnect(connectionID); + resolve(actions); + }, + }); + }); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; + createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.childReportID).toBe(thread.reportID); + + await waitForPromisesToResolve(); + + fetch.pause(); + + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); + await waitForPromisesToResolve(); + + let allReports = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + Onyx.disconnect(connectionID); + resolve(reports); + }, + }); + }); + + await waitForPromisesToResolve(); + + iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + fetch.resume(); + + allReports = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (reports) => { + Onyx.disconnect(connectionID); + resolve(reports); + }, + }); + }); + + iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + + expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(REPORT_ID)); + }); it('navigate the user correctly to the chat Report when appropriate', () => { jest.clearAllMocks(); From 10ca6f7ef77035ef8c1e157bdf6de325c0fd6005 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:47:20 +0200 Subject: [PATCH 12/24] fix update the moneyRequestPreview to show [Deleted request] when appropriate test case --- tests/actions/IOUTest.js | 230 ++++++++++++++++++++++----------------- 1 file changed, 128 insertions(+), 102 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index eb99223d5fd7..b5ea93cf2b07 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1878,112 +1878,138 @@ describe('actions/IOU', () => { }), )); - it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', () => - waitForPromisesToResolve() - .then(() => { - thread = ReportUtils.buildTransactionThread(createIOUAction); - const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); - Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - }) - .then(() => { - Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, - callback: (val) => (reportActions = val), - }); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeTruthy(); - resolve(); - }, - }); - }), - ) - .then(() => { - Report.addComment(thread.reportID, 'Testing a comment'); - return waitForPromisesToResolve(); - }) + it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { + await waitForPromisesToResolve(); - .then(() => { - const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - reportActionID = resultAction.reportActionID; - expect(resultAction.message).toEqual(REPORT_ACTION.message); - expect(resultAction.person).toEqual(REPORT_ACTION.person); + jest.advanceTimersByTime(10); + thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - value: { - reportID: thread.reportID, - notificationPreference: 'always', - lastVisibleActionCreated: '2023-08-29 03:48:27.267', - lastMessageText: 'Testing a comment', - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, - value: { - [reportActionID]: {pendingAction: null}, - }, - }, - ]); - return waitForPromisesToResolve(); - }) - .then(() => { - // Verify there is 2 action (created + addcomment) and our optimistic comment has been removed - expect(_.size(reportActions)).toBe(2); + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); - const resultAction = reportActions[reportActionID]; + jest.advanceTimersByTime(10); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + await waitForPromisesToResolve(); - // Verify that our action is no longer in the loading state - expect(resultAction.pendingAction).toBeNull(); - }) - .then(() => { - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (reportActionsForReport) => { - Onyx.disconnect(connectionID); - createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (reportActionsForReport) => { - Onyx.disconnect(connectionID); - createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); - resolve(); - }, - }); - }), - )); + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + value: { + [createIOUAction.reportActionID]: {childReportID: thread.reportID}, + }, + }, + ]); + + await waitForPromisesToResolve(); + + const allReportActions = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (actions) => { + Onyx.disconnect(connectionID); + resolve(actions); + }, + }); + }); + + const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; + createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.childReportID).toBe(thread.reportID); + + await waitForPromisesToResolve(); + + jest.advanceTimersByTime(10); + + Report.addComment(thread.reportID, 'Testing a comment'); + await waitForPromisesToResolve(); + + let resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); + + await waitForPromisesToResolve(); + + // Verify there are three actions (created + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(2); + + let resultActionAfterUpdate = reportActions[reportActionID]; + //expect(resultActionAfterUpdate).toBeFalsy(); + + // Verify that our action is no longer in the loading state + expect(resultActionAfterUpdate.pendingAction).toBeNull(); + + await waitForPromisesToResolve(); + + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); + + jest.advanceTimersByTime(10); + + Report.addComment(REPORT_ID, 'Testing a comment'); + await waitForPromisesToResolve(); + + resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + expect(resultAction.pendingAction).toBeNull(); + + await waitForPromisesToResolve(); + + // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(3); + + resultActionAfterUpdate = reportActions[reportActionID]; + //expect(resultActionAfterUpdate).toBeFalsy(); + + // Verify that our action is no longer in the loading state + expect(resultActionAfterUpdate.pendingAction).toBeNull(); + + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + await waitForPromisesToResolve(); + + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); + resolve(); + }, + }); + }); + + fetch.resume(); + + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (reportActionsForReport) => { + Onyx.disconnect(connectionID); + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction.message[0].isDeletedParentAction).toBeTruthy(); + resolve(); + }, + }); + }); + }); it('update IOU report and reportPreview with new totals and messages if the IOU report is not deleted', () => { const amount2 = 20000; From fa04a990b098fb3516828d316a7e61e704c99f07 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:34:23 +0200 Subject: [PATCH 13/24] fix update IOU report and reportPreview with new totals and messages if the IOU report is not deleted test case --- tests/actions/IOUTest.js | 176 +++++++++------------------------------ 1 file changed, 38 insertions(+), 138 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index b5ea93cf2b07..f58215265bbf 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -11,6 +11,7 @@ import * as ReportActions from '../../src/libs/actions/ReportActions'; import * as Report from '../../src/libs/actions/Report'; import OnyxUpdateManager from '../../src/libs/actions/OnyxUpdateManager'; import * as ReportUtils from '../../src/libs/ReportUtils'; +import * as ReportActionsUtils from '../../src/libs/ReportActionsUtils'; import * as PersonalDetailsUtils from '../../src/libs/PersonalDetailsUtils'; import * as User from '../../src/libs/actions/User'; import PusherHelper from '../utils/PusherHelper'; @@ -1403,7 +1404,7 @@ describe('actions/IOU', () => { automatic: false, avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/avatar_3.png', message: [{type: 'COMMENT', html: 'Testing a comment', text: 'Testing a comment', translationKey: ''}], - person: [{type: 'TEXT', style: 'strong', text: 'test@test.com'}], + person: [{type: 'TEXT', style: 'strong', text: 'Test User'}], shouldShow: true, }; @@ -1417,7 +1418,7 @@ describe('actions/IOU', () => { User.subscribeToUserEvents(); await waitForPromisesToResolve(); await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID); - IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment); + IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment); await waitForPromisesToResolve(); let allReports = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -2011,144 +2012,48 @@ describe('actions/IOU', () => { }); }); - it('update IOU report and reportPreview with new totals and messages if the IOU report is not deleted', () => { - const amount2 = 20000; - const comment2 = 'Send me money please 2'; - return waitForPromisesToResolve() - .then(() => { - IOU.requestMoney({}, amount2, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: TEST_USER_LOGIN, accountID: TEST_USER_ACCOUNT_ID}, comment2); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); - - expect(iouReport.hasOutstandingIOU).toBeTruthy(); - expect(iouReport.total).toBe(30000); - - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - - const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; - - const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); - expect(ioupreview).toBeTruthy(); - expect(ioupreview.message[0].text).toBe('Test owes $300.00'); - - resolve(); - }, - }); - }), - ) - .then(() => { - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); - - expect(iouReport.hasOutstandingIOU).toBeTruthy(); - expect(iouReport.total).toBe(20000); - - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); - - const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; + it('update IOU report and reportPreview with new totals and messages if the IOU report is not deleted', async () => { + await waitForPromisesToResolve(); + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + callback: (val) => (iouReport = val), + }); + await waitForPromisesToResolve(); + let reportActionsForIOUReport; - const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); - expect(ioupreview).toBeTruthy(); - expect(ioupreview.message[0].text).toBe('Test owes $200.00'); + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + callback: (val) => (reportActionsForIOUReport = val), + }); + await waitForPromisesToResolve(); - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (allReports) => { - Onyx.disconnect(connectionID); - iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); - expect(iouReport).toBeTruthy(); - expect(iouReport).toHaveProperty('reportID'); - expect(iouReport).toHaveProperty('chatReportID'); + jest.advanceTimersByTime(10); + const amount2 = 20000; + const comment2 = 'Send me money please 2'; + IOU.requestMoney(chatReport, amount2, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment2); - expect(iouReport.hasOutstandingIOU).toBeTruthy(); - expect(iouReport.total).toBe(20000); + await waitForPromisesToResolve(); - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - waitForCollectionCallback: true, - callback: (allReportActions) => { - Onyx.disconnect(connectionID); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(30000); - const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`]; + const ioupreview = ReportActionsUtils.getReportPreviewAction(chatReport.reportID, iouReport.reportID); + expect(ioupreview).toBeTruthy(); + expect(ioupreview.message[0].text).toBe('rory@expensifail.com owes $300.00'); - const ioupreview = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW); - expect(ioupreview).toBeFalsy(); - expect(ioupreview.total).toBe(20000); - expect(ioupreview.message[0].text).toBe('Test owes $200.00'); + fetch.pause(); + jest.advanceTimersByTime(10); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + await waitForPromisesToResolve(); - resolve(); - }, - }); - }), - ); + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(20000); }); it('navigate the user correctly to the iou Report when appropriate', async () => { @@ -2170,7 +2075,6 @@ describe('actions/IOU', () => { expect(resultAction.message).toEqual(REPORT_ACTION.message); expect(resultAction.person).toEqual(REPORT_ACTION.person); - expect(resultAction.pendingAction).toBeNull(); await waitForPromisesToResolve(); @@ -2178,10 +2082,6 @@ describe('actions/IOU', () => { expect(_.size(reportActions)).toBe(3); const resultActionAfterUpdate = reportActions[reportActionID]; - //expect(resultActionAfterUpdate).toBeFalsy(); - - // Verify that our action is no longer in the loading state - expect(resultActionAfterUpdate.pendingAction).toBeNull(); await waitForPromisesToResolve(); From c6cd3e4a1939fc832987ac7a999477f39b53beb4 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:49:39 +0200 Subject: [PATCH 14/24] fix lint errors --- tests/actions/IOUTest.js | 48 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index f58215265bbf..d1a470a358ea 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1396,7 +1396,7 @@ describe('actions/IOU', () => { let thread; const TEST_USER_ACCOUNT_ID = 1; const TEST_USER_LOGIN = 'test@test.com'; - let REPORT_ID; + let IOU_REPORT_ID; let reportActionID; const REPORT_ACTION = { actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT, @@ -1420,7 +1420,7 @@ describe('actions/IOU', () => { await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID); IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment); await waitForPromisesToResolve(); - let allReports = await new Promise((resolve) => { + const allReports = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, @@ -1447,7 +1447,7 @@ describe('actions/IOU', () => { expect(chatReport.iouReportID).toBe(iouReport.reportID); expect(iouReport.chatReportID).toBe(chatReport.reportID); - REPORT_ID = chatReport.iouReportID; + IOU_REPORT_ID = chatReport.iouReportID; PusherHelper.emitOnyxUpdate([ { @@ -1472,7 +1472,7 @@ describe('actions/IOU', () => { reportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, waitForCollectionCallback: true, callback: (val) => { Onyx.disconnect(connectionID); @@ -1625,14 +1625,14 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - Report.addComment(REPORT_ID, 'Testing a comment'); + Report.addComment(IOU_REPORT_ID, 'Testing a comment'); await waitForPromisesToResolve(); const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); @@ -1648,7 +1648,6 @@ describe('actions/IOU', () => { expect(_.size(reportActions)).toBe(3); const resultActionAfterUpdate = reportActions[reportActionID]; - //expect(resultActionAfterUpdate).toBeFalsy(); // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); @@ -1699,7 +1698,7 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); + thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -1715,7 +1714,7 @@ describe('actions/IOU', () => { PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, value: { [createIOUAction.reportActionID]: {childReportID: thread.reportID}, }, @@ -1746,7 +1745,7 @@ describe('actions/IOU', () => { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); - report = await new Promise((resolve) => { + let report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, waitForCollectionCallback: true, @@ -1883,7 +1882,7 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); + thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -1899,7 +1898,7 @@ describe('actions/IOU', () => { PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, value: { [createIOUAction.reportActionID]: {childReportID: thread.reportID}, }, @@ -1943,7 +1942,6 @@ describe('actions/IOU', () => { expect(_.size(reportActions)).toBe(2); let resultActionAfterUpdate = reportActions[reportActionID]; - //expect(resultActionAfterUpdate).toBeFalsy(); // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); @@ -1951,14 +1949,14 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - Report.addComment(REPORT_ID, 'Testing a comment'); + Report.addComment(IOU_REPORT_ID, 'Testing a comment'); await waitForPromisesToResolve(); resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); @@ -1974,7 +1972,6 @@ describe('actions/IOU', () => { expect(_.size(reportActions)).toBe(3); resultActionAfterUpdate = reportActions[reportActionID]; - //expect(resultActionAfterUpdate).toBeFalsy(); // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); @@ -2019,13 +2016,6 @@ describe('actions/IOU', () => { callback: (val) => (iouReport = val), }); await waitForPromisesToResolve(); - let reportActionsForIOUReport; - - Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - callback: (val) => (reportActionsForIOUReport = val), - }); - await waitForPromisesToResolve(); jest.advanceTimersByTime(10); const amount2 = 20000; @@ -2060,14 +2050,14 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - Report.addComment(REPORT_ID, 'Testing a comment'); + Report.addComment(IOU_REPORT_ID, 'Testing a comment'); await waitForPromisesToResolve(); const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); @@ -2081,12 +2071,10 @@ describe('actions/IOU', () => { // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(3); - const resultActionAfterUpdate = reportActions[reportActionID]; - await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - thread = ReportUtils.buildTransactionThread(createIOUAction, REPORT_ID); + thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, @@ -2102,7 +2090,7 @@ describe('actions/IOU', () => { PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, value: { [createIOUAction.reportActionID]: {childReportID: thread.reportID}, }, @@ -2169,7 +2157,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(REPORT_ID)); + expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(IOU_REPORT_ID)); }); it('navigate the user correctly to the chat Report when appropriate', () => { From 6ab7a90dc583043485e6fe3c4b9d7d1358c5701d Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:07:27 +0200 Subject: [PATCH 15/24] refactor other test cases to use async/await instead --- tests/actions/IOUTest.js | 374 ++++++++++++++++++--------------------- 1 file changed, 170 insertions(+), 204 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index d1a470a358ea..5142f9861039 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1470,17 +1470,6 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); - reportActions = await new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, - waitForCollectionCallback: true, - callback: (val) => { - Onyx.disconnect(connectionID); - resolve(val); - }, - }); - }); - const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -1516,109 +1505,100 @@ describe('actions/IOU', () => { }); afterEach(PusherHelper.teardown); - it('delete a money request (IOU Action and transaction) successfully', () => { + it('delete a money request (IOU Action and transaction) successfully', async () => { fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - return waitForPromisesToResolve() - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (reportActionsForReport) => { - Onyx.disconnect(connectionID); - createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - expect(createIOUAction).toBeFalsy(); - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, - waitForCollectionCallback: true, - callback: (t) => { - Onyx.disconnect(connectionID); + await waitForPromisesToResolve(); - expect(t).toBeFalsy(); + let reportActionsForReport = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (actionsForReport) => { + Onyx.disconnect(connectionID); + resolve(actionsForReport); + }, + }); + }); - resolve(); - }, - }); - }), - ) + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeFalsy(); - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (reportActionsForReport) => { - Onyx.disconnect(connectionID); - createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - expect(createIOUAction).toBeFalsy(); - resolve(); - }, - }); - }), - ) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, - waitForCollectionCallback: true, - callback: (tr) => { - Onyx.disconnect(connectionID); + const t = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, + waitForCollectionCallback: true, + callback: (transactionResult) => { + Onyx.disconnect(connectionID); + resolve(transactionResult); + }, + }); + }); - expect(tr).toBeFalsy(); + expect(t).toBeFalsy(); - resolve(); - }, - }); - }), - ); + fetch.resume(); + + reportActionsForReport = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (actionsForReport) => { + Onyx.disconnect(connectionID); + resolve(actionsForReport); + }, + }); + }); + + createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); + expect(createIOUAction).toBeFalsy(); + + const tr = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, + waitForCollectionCallback: true, + callback: (transactionResult) => { + Onyx.disconnect(connectionID); + resolve(transactionResult); + }, + }); + }); + + expect(tr).toBeFalsy(); }); - it('delete the IOU report when there are no visible comments left in the IOU report', () => { + it('delete the IOU report when there are no visible comments left in the IOU report', async () => { fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - return waitForPromisesToResolve() - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeFalsy(); - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeFalsy(); - resolve(); - }, - }); - }), - ); + await waitForPromisesToResolve(); + + let report = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (res) => { + Onyx.disconnect(connectionID); + resolve(res); + }, + }); + }); + + expect(report).toBeFalsy(); + + fetch.resume(); + + report = await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, + waitForCollectionCallback: true, + callback: (res) => { + Onyx.disconnect(connectionID); + resolve(res); + }, + }); + }); + + expect(report).toBeFalsy(); }); it('does not delete the IOU report when there are visible comments left in the IOU report', async () => { @@ -1773,110 +1753,96 @@ describe('actions/IOU', () => { expect(report).toBeFalsy(); }); - it('does not delete the transaction thread if there are visible comments in the thread', () => - waitForPromisesToResolve() - .then(() => { - thread = ReportUtils.buildTransactionThread(createIOUAction); - const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); - Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - }) - .then(() => { - Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, - callback: (val) => (reportActions = val), - }); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeTruthy(); - resolve(); - }, - }); - }), - ) - .then(() => { - Report.addComment(thread.reportID, 'Testing a comment'); - return waitForPromisesToResolve(); - }) + it('does not delete the transaction thread if there are visible comments in the thread', async () => { + await waitForPromisesToResolve(); - .then(() => { - const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); - reportActionID = resultAction.reportActionID; - expect(resultAction.message).toEqual(REPORT_ACTION.message); - expect(resultAction.person).toEqual(REPORT_ACTION.person); - - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - value: { - reportID: thread.reportID, - notificationPreference: 'always', - lastVisibleActionCreated: '2023-08-29 03:48:27.267', - lastMessageText: 'Testing a comment', - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, - value: { - [reportActionID]: {pendingAction: null}, - }, - }, - ]); - return waitForPromisesToResolve(); - }) - .then(() => { - // Verify there is 2 action (created + addcomment) and our optimistic comment has been removed - expect(_.size(reportActions)).toBe(2); + thread = ReportUtils.buildTransactionThread(createIOUAction); + const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - const resultAction = reportActions[reportActionID]; + Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + callback: (val) => (reportActions = val), + }); + await waitForPromisesToResolve(); - // Verify that our action is no longer in the loading state - expect(resultAction.pendingAction).toBeNull(); - }) - .then(() => { - fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - return waitForPromisesToResolve(); - }) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeTruthy(); - resolve(); - }, - }); - }), - ) - .then(fetch.resume) - .then( - () => - new Promise((resolve) => { - const connectionID = Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - waitForCollectionCallback: true, - callback: (report) => { - Onyx.disconnect(connectionID); - expect(report).toBeTruthy(); - resolve(); - }, - }); - }), - )); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }); + + Report.addComment(thread.reportID, 'Testing a comment'); + await waitForPromisesToResolve(); + + const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); + reportActionID = resultAction.reportActionID; + expect(resultAction.message).toEqual(REPORT_ACTION.message); + expect(resultAction.person).toEqual(REPORT_ACTION.person); + + PusherHelper.emitOnyxUpdate([ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + value: { + reportID: thread.reportID, + notificationPreference: 'always', + lastVisibleActionCreated: DateUtils.getDBTime(), + lastMessageText: 'Testing a comment', + lastActorAccountID: TEST_USER_ACCOUNT_ID, + }, + }, + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, + value: { + [reportActionID]: {pendingAction: null}, + }, + }, + ]); + await waitForPromisesToResolve(); + + // Verify there are 2 actions (created + addcomment) and our optimistic comment has been removed + expect(_.size(reportActions)).toBe(2); + const resultActionAfter = reportActions[reportActionID]; + // Verify that our action is no longer in the loading state + expect(resultActionAfter.pendingAction).toBeNull(); + + fetch.pause(); + IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + await waitForPromisesToResolve(); + + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }); + + fetch.resume(); + await new Promise((resolve) => { + const connectionID = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, + waitForCollectionCallback: true, + callback: (report) => { + Onyx.disconnect(connectionID); + expect(report).toBeTruthy(); + resolve(); + }, + }); + }); + }); it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { await waitForPromisesToResolve(); From 9d5ba72f967b7f91f090205584c75adace99ae2f Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:22:48 +0200 Subject: [PATCH 16/24] adding comments --- tests/actions/IOUTest.js | 116 +++++++++++++++++++++++++++++++++++---- 1 file changed, 105 insertions(+), 11 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 5142f9861039..67e670491b27 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1411,15 +1411,21 @@ describe('actions/IOU', () => { let reportActions; beforeEach(async () => { + // Given mocks are cleared and helpers are set up jest.clearAllMocks(); PusherHelper.setup(); - // Set up Onyx with some test user data + + // Given a test user is signed in with Onyx setup and some initial data await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); User.subscribeToUserEvents(); await waitForPromisesToResolve(); await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID); + + // When an IOU request for money is made IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment); await waitForPromisesToResolve(); + + // When fetching all reports from Onyx const allReports = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, @@ -1431,24 +1437,30 @@ describe('actions/IOU', () => { }); }); + // Then we should have exactly 2 reports expect(_.size(allReports)).toBe(2); + // Then one of them should be a chat report with relevant properties chatReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.CHAT); expect(chatReport).toBeTruthy(); expect(chatReport).toHaveProperty('reportID'); expect(chatReport).toHaveProperty('iouReportID'); expect(chatReport.hasOutstandingIOU).toBe(true); + // Then one of them should be an IOU report with relevant properties iouReport = _.find(allReports, (report) => report.type === CONST.REPORT.TYPE.IOU); expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); + // Then their IDs should reference each other expect(chatReport.iouReportID).toBe(iouReport.reportID); expect(iouReport.chatReportID).toBe(chatReport.reportID); + // Storing IOU Report ID for further reference IOU_REPORT_ID = chatReport.iouReportID; + // When updates are emitted to Onyx PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, @@ -1470,6 +1482,7 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // When fetching all report actions from Onyx const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -1481,11 +1494,13 @@ describe('actions/IOU', () => { }); }); + // Then we should find an IOU action with specific properties const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction).toBeTruthy(); expect(createIOUAction.originalMessage.IOUReportID).toBe(iouReport.reportID); + // When fetching all transactions from Onyx const allTransactions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION, @@ -1497,19 +1512,25 @@ describe('actions/IOU', () => { }); }); + // Then we should find a specific transaction with relevant properties transaction = _.find(allTransactions, (t) => t); expect(transaction).toBeTruthy(); expect(transaction.amount).toBe(amount); expect(transaction.reportID).toBe(iouReport.reportID); expect(createIOUAction.originalMessage.IOUTransactionID).toBe(transaction.transactionID); }); + afterEach(PusherHelper.teardown); it('delete a money request (IOU Action and transaction) successfully', async () => { + // Given the fetch operations are paused and a money request is initiated fetch.pause(); + + // When the money request is deleted IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); await waitForPromisesToResolve(); + // Then we check if the IOU report action is removed from the report actions collection let reportActionsForReport = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -1524,6 +1545,7 @@ describe('actions/IOU', () => { createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction).toBeFalsy(); + // Then we check if the transaction is removed from the transactions collection const t = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, @@ -1537,8 +1559,10 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); + // Given fetch operations are resumed fetch.resume(); + // Then we recheck the IOU report action from the report actions collection reportActionsForReport = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -1553,6 +1577,7 @@ describe('actions/IOU', () => { createIOUAction = _.find(reportActionsForReport, (reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction).toBeFalsy(); + // Then we recheck the transaction from the transactions collection const tr = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, @@ -1568,7 +1593,10 @@ describe('actions/IOU', () => { }); it('delete the IOU report when there are no visible comments left in the IOU report', async () => { + // Given an IOU report and a paused fetch state fetch.pause(); + + // When the IOU money request is deleted IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); await waitForPromisesToResolve(); @@ -1583,8 +1611,10 @@ describe('actions/IOU', () => { }); }); + // Then the report should be falsy (indicating deletion) expect(report).toBeFalsy(); + // Given the resumed fetch state fetch.resume(); report = await new Promise((resolve) => { @@ -1598,10 +1628,12 @@ describe('actions/IOU', () => { }); }); + // Then the report should still be falsy (confirming deletion persisted) expect(report).toBeFalsy(); }); it('does not delete the IOU report when there are visible comments left in the IOU report', async () => { + // Given the initial setup is completed await waitForPromisesToResolve(); Onyx.connect({ @@ -1612,9 +1644,11 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); + // When a comment is added to the IOU report Report.addComment(IOU_REPORT_ID, 'Testing a comment'); await waitForPromisesToResolve(); + // Then verify that the comment is correctly added const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); reportActionID = resultAction.reportActionID; @@ -1627,16 +1661,16 @@ describe('actions/IOU', () => { // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(3); + // Then check the loading state of our action const resultActionAfterUpdate = reportActions[reportActionID]; - - // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); + // When we attempt to delete a money request from the IOU report fetch.pause(); - IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); + // Then expect that the IOU report still exists let allReports = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, @@ -1655,6 +1689,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); + // Given the resumed fetch state fetch.resume(); allReports = await new Promise((resolve) => { @@ -1667,7 +1702,7 @@ describe('actions/IOU', () => { }, }); }); - + // Then expect that the IOU report still exists iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); @@ -1675,22 +1710,30 @@ describe('actions/IOU', () => { }); it('delete the transaction thread if there are no visible comments in the thread', async () => { + // Given: All promises are resolved await waitForPromisesToResolve(); - jest.advanceTimersByTime(10); + + // When: Building a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); + await waitForPromisesToResolve(); jest.advanceTimersByTime(10); + + // Given: User logins from the participant accounts const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + + // When: Opening a thread report with the given details Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); await waitForPromisesToResolve(); + // When: Emitting an Onyx update PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, @@ -1703,6 +1746,7 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // Then: The iou action has the transaction report id as a child report ID const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -1713,18 +1757,21 @@ describe('actions/IOU', () => { }, }); }); - const reportActionsForIOUReport = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.iouReportID}`]; createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction.childReportID).toBe(thread.reportID); await waitForPromisesToResolve(); + // Given: Fetch is paused and timers have advanced fetch.pause(); jest.advanceTimersByTime(10); + + // When: Deleting a money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); + // Then: The report for the given thread ID does not exist let report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1739,6 +1786,7 @@ describe('actions/IOU', () => { expect(report).toBeFalsy(); fetch.resume(); + // Then: After resuming fetch, the report for the given thread ID still does not exist report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1754,8 +1802,10 @@ describe('actions/IOU', () => { }); it('does not delete the transaction thread if there are visible comments in the thread', async () => { + // Given initial environment is set up await waitForPromisesToResolve(); + // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); @@ -1778,14 +1828,17 @@ describe('actions/IOU', () => { }); }); + // When a comment is added Report.addComment(thread.reportID, 'Testing a comment'); await waitForPromisesToResolve(); + // Then comment details should match the expected report action const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); reportActionID = resultAction.reportActionID; expect(resultAction.message).toEqual(REPORT_ACTION.message); expect(resultAction.person).toEqual(REPORT_ACTION.person); + // When onyx updates are emitted PusherHelper.emitOnyxUpdate([ { onyxMethod: Onyx.METHOD.MERGE, @@ -1808,16 +1861,17 @@ describe('actions/IOU', () => { ]); await waitForPromisesToResolve(); - // Verify there are 2 actions (created + addcomment) and our optimistic comment has been removed + // Then the report should have 2 actions expect(_.size(reportActions)).toBe(2); const resultActionAfter = reportActions[reportActionID]; - // Verify that our action is no longer in the loading state expect(resultActionAfter.pendingAction).toBeNull(); fetch.pause(); + // When deleting money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); + // Then the transaction thread report should still exist await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1830,6 +1884,8 @@ describe('actions/IOU', () => { }); }); + // When fetch resumes + // Then the transaction thread report should still exist fetch.resume(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -1847,6 +1903,8 @@ describe('actions/IOU', () => { it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { await waitForPromisesToResolve(); + // Given a thread report + jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); @@ -1890,6 +1948,8 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // Given an added comment to the thread report + jest.advanceTimersByTime(10); Report.addComment(thread.reportID, 'Testing a comment'); @@ -1914,6 +1974,8 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // Given an added comment to the IOU report + Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), @@ -1943,9 +2005,12 @@ describe('actions/IOU', () => { expect(resultActionAfterUpdate.pendingAction).toBeNull(); fetch.pause(); + // When we delete the money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); + // Then we expect the moneyRequestPreview to show [Deleted request] + await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -1959,8 +2024,12 @@ describe('actions/IOU', () => { }); }); + // When we resume fetch + fetch.resume(); + // Then we expect the moneyRequestPreview to show [Deleted request] + await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`, @@ -1983,6 +2052,8 @@ describe('actions/IOU', () => { }); await waitForPromisesToResolve(); + // Given a second money request in addition to the first one + jest.advanceTimersByTime(10); const amount2 = 20000; const comment2 = 'Send me money please 2'; @@ -1990,6 +2061,8 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // Then we expect the IOU report and reportPreview to update with new totals + expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); @@ -2000,11 +2073,24 @@ describe('actions/IOU', () => { expect(ioupreview).toBeTruthy(); expect(ioupreview.message[0].text).toBe('rory@expensifail.com owes $300.00'); + // When we delete the first money request fetch.pause(); jest.advanceTimersByTime(10); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); + // Then we expect the IOU report and reportPreview to update with new totals + + expect(iouReport).toBeTruthy(); + expect(iouReport).toHaveProperty('reportID'); + expect(iouReport).toHaveProperty('chatReportID'); + expect(iouReport.hasOutstandingIOU).toBeTruthy(); + expect(iouReport.total).toBe(20000); + + // When we resume fetch + fetch.resume(); + + // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); @@ -2021,6 +2107,8 @@ describe('actions/IOU', () => { }); await waitForPromisesToResolve(); + // Given an added comment to the iou report + jest.advanceTimersByTime(10); Report.addComment(IOU_REPORT_ID, 'Testing a comment'); @@ -2039,6 +2127,8 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // Given a thread report + jest.advanceTimersByTime(10); thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); @@ -2082,6 +2172,8 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); + // When we delete the money request in SingleTransactionView and we should not delete the IOU report + fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -2123,13 +2215,15 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); + // Then we expect to navigate to the iou report + expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(IOU_REPORT_ID)); }); it('navigate the user correctly to the chat Report when appropriate', () => { - jest.clearAllMocks(); - fetch.pause(); + // When we delete the money request and we should delete the IOU report IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); + // Then we expect to navigate to the chat report expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.getReportRoute(chatReport.reportID)); }); }); From 895487c654691b070e5db9e3b43b046339ed3f15 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:54:43 +0200 Subject: [PATCH 17/24] remove unnecessary code --- tests/actions/IOUTest.js | 79 +--------------------------------------- 1 file changed, 1 insertion(+), 78 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 67e670491b27..6f52823eee6c 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1460,26 +1460,6 @@ describe('actions/IOU', () => { // Storing IOU Report ID for further reference IOU_REPORT_ID = chatReport.iouReportID; - // When updates are emitted to Onyx - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, - value: { - reportID: chatReport.reportID, - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, - value: { - reportID: iouReport.reportID, - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - ]); - await waitForPromisesToResolve(); // When fetching all report actions from Onyx @@ -1733,19 +1713,6 @@ describe('actions/IOU', () => { Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); await waitForPromisesToResolve(); - // When: Emitting an Onyx update - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, - value: { - [createIOUAction.reportActionID]: {childReportID: thread.reportID}, - }, - }, - ]); - - await waitForPromisesToResolve(); - // Then: The iou action has the transaction report id as a child report ID const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -1838,27 +1805,6 @@ describe('actions/IOU', () => { expect(resultAction.message).toEqual(REPORT_ACTION.message); expect(resultAction.person).toEqual(REPORT_ACTION.person); - // When onyx updates are emitted - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, - value: { - reportID: thread.reportID, - notificationPreference: 'always', - lastVisibleActionCreated: DateUtils.getDBTime(), - lastMessageText: 'Testing a comment', - lastActorAccountID: TEST_USER_ACCOUNT_ID, - }, - }, - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, - value: { - [reportActionID]: {pendingAction: null}, - }, - }, - ]); await waitForPromisesToResolve(); // Then the report should have 2 actions @@ -1917,18 +1863,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - await waitForPromisesToResolve(); - - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, - value: { - [createIOUAction.reportActionID]: {childReportID: thread.reportID}, - }, - }, - ]); - + await waitForPromisesToResolve(); const allReportActions = await new Promise((resolve) => { @@ -2143,18 +2078,6 @@ describe('actions/IOU', () => { Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); await waitForPromisesToResolve(); - PusherHelper.emitOnyxUpdate([ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, - value: { - [createIOUAction.reportActionID]: {childReportID: thread.reportID}, - }, - }, - ]); - - await waitForPromisesToResolve(); - const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, From 584673a44ee60439609be652a0042e05b5554aa0 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 17 Sep 2023 00:01:30 +0200 Subject: [PATCH 18/24] edit comments --- tests/actions/IOUTest.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 6f52823eee6c..c7966def081b 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1690,11 +1690,11 @@ describe('actions/IOU', () => { }); it('delete the transaction thread if there are no visible comments in the thread', async () => { - // Given: All promises are resolved + // Given all promises are resolved await waitForPromisesToResolve(); jest.advanceTimersByTime(10); - // When: Building a transaction thread + // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction, IOU_REPORT_ID); Onyx.connect({ @@ -1706,14 +1706,14 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); - // Given: User logins from the participant accounts + // Given User logins from the participant accounts const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); - // When: Opening a thread report with the given details + // When Opening a thread report with the given details Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); await waitForPromisesToResolve(); - // Then: The iou action has the transaction report id as a child report ID + // Then The iou action has the transaction report id as a child report ID const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, @@ -1730,15 +1730,15 @@ describe('actions/IOU', () => { await waitForPromisesToResolve(); - // Given: Fetch is paused and timers have advanced + // Given Fetch is paused and timers have advanced fetch.pause(); jest.advanceTimersByTime(10); - // When: Deleting a money request + // When Deleting a money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); await waitForPromisesToResolve(); - // Then: The report for the given thread ID does not exist + // Then The report for the given thread ID does not exist let report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1753,7 +1753,7 @@ describe('actions/IOU', () => { expect(report).toBeFalsy(); fetch.resume(); - // Then: After resuming fetch, the report for the given thread ID still does not exist + // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -1846,7 +1846,7 @@ describe('actions/IOU', () => { }); }); - it('It should update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { + it('update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { await waitForPromisesToResolve(); // Given a thread report From d8dd67b80cdf7e43e9b9b711c1299438deebc990 Mon Sep 17 00:00:00 2001 From: rayane-djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Sun, 17 Sep 2023 00:02:35 +0200 Subject: [PATCH 19/24] run prettier --- tests/actions/IOUTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index c7966def081b..478ea7f8c6be 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1863,7 +1863,7 @@ describe('actions/IOU', () => { jest.advanceTimersByTime(10); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - + await waitForPromisesToResolve(); const allReportActions = await new Promise((resolve) => { From 34e015abfca370e3900737b0720bb89afbe0b38b Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:13:11 +0000 Subject: [PATCH 20/24] use waitForBatchedUpdates instead of waitForPromisesToResolve --- tests/actions/IOUTest.js | 92 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 6a8dac2d8ecb..86fc4b04ea54 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1422,12 +1422,12 @@ describe('actions/IOU', () => { // Given a test user is signed in with Onyx setup and some initial data await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); User.subscribeToUserEvents(); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID); // When an IOU request for money is made IOU.requestMoney({}, amount, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // When fetching all reports from Onyx const allReports = await new Promise((resolve) => { @@ -1464,7 +1464,7 @@ describe('actions/IOU', () => { // Storing IOU Report ID for further reference IOU_REPORT_ID = chatReport.iouReportID; - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // When fetching all report actions from Onyx const allReportActions = await new Promise((resolve) => { @@ -1512,7 +1512,7 @@ describe('actions/IOU', () => { // When the money request is deleted IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then we check if the IOU report action is removed from the report actions collection let reportActionsForReport = await new Promise((resolve) => { @@ -1582,7 +1582,7 @@ describe('actions/IOU', () => { // When the IOU money request is deleted IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); let report = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -1618,19 +1618,19 @@ describe('actions/IOU', () => { it('does not delete the IOU report when there are visible comments left in the IOU report', async () => { // Given the initial setup is completed - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); // When a comment is added to the IOU report Report.addComment(IOU_REPORT_ID, 'Testing a comment'); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then verify that the comment is correctly added const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); @@ -1640,7 +1640,7 @@ describe('actions/IOU', () => { expect(resultAction.person).toEqual(REPORT_ACTION.person); expect(resultAction.pendingAction).toBeNull(); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(3); @@ -1652,7 +1652,7 @@ describe('actions/IOU', () => { // When we attempt to delete a money request from the IOU report fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then expect that the IOU report still exists let allReports = await new Promise((resolve) => { @@ -1666,7 +1666,7 @@ describe('actions/IOU', () => { }); }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); expect(iouReport).toBeTruthy(); @@ -1695,7 +1695,7 @@ describe('actions/IOU', () => { it('delete the transaction thread if there are no visible comments in the thread', async () => { // Given all promises are resolved - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); // Given a transaction thread @@ -1706,7 +1706,7 @@ describe('actions/IOU', () => { callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); @@ -1715,7 +1715,7 @@ describe('actions/IOU', () => { // When Opening a thread report with the given details Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then The iou action has the transaction report id as a child report ID const allReportActions = await new Promise((resolve) => { @@ -1732,7 +1732,7 @@ describe('actions/IOU', () => { createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction.childReportID).toBe(thread.reportID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given Fetch is paused and timers have advanced fetch.pause(); @@ -1740,7 +1740,7 @@ describe('actions/IOU', () => { // When Deleting a money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then The report for the given thread ID does not exist let report = await new Promise((resolve) => { @@ -1774,7 +1774,7 @@ describe('actions/IOU', () => { it('does not delete the transaction thread if there are visible comments in the thread', async () => { // Given initial environment is set up - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction); @@ -1785,7 +1785,7 @@ describe('actions/IOU', () => { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -1801,7 +1801,7 @@ describe('actions/IOU', () => { // When a comment is added Report.addComment(thread.reportID, 'Testing a comment'); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then comment details should match the expected report action const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); @@ -1809,7 +1809,7 @@ describe('actions/IOU', () => { expect(resultAction.message).toEqual(REPORT_ACTION.message); expect(resultAction.person).toEqual(REPORT_ACTION.person); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then the report should have 2 actions expect(_.size(reportActions)).toBe(2); @@ -1819,7 +1819,7 @@ describe('actions/IOU', () => { fetch.pause(); // When deleting money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then the transaction thread report should still exist await new Promise((resolve) => { @@ -1851,7 +1851,7 @@ describe('actions/IOU', () => { }); it('update the moneyRequestPreview to show [Deleted request] when appropriate', async () => { - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given a thread report @@ -1862,13 +1862,13 @@ describe('actions/IOU', () => { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -1885,14 +1885,14 @@ describe('actions/IOU', () => { createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction.childReportID).toBe(thread.reportID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given an added comment to the thread report jest.advanceTimersByTime(10); Report.addComment(thread.reportID, 'Testing a comment'); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); let resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); reportActionID = resultAction.reportActionID; @@ -1901,7 +1901,7 @@ describe('actions/IOU', () => { expect(resultAction.person).toEqual(REPORT_ACTION.person); expect(resultAction.pendingAction).toBeNull(); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Verify there are three actions (created + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(2); @@ -1911,7 +1911,7 @@ describe('actions/IOU', () => { // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate.pendingAction).toBeNull(); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given an added comment to the IOU report @@ -1919,12 +1919,12 @@ describe('actions/IOU', () => { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); Report.addComment(IOU_REPORT_ID, 'Testing a comment'); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); reportActionID = resultAction.reportActionID; @@ -1933,7 +1933,7 @@ describe('actions/IOU', () => { expect(resultAction.person).toEqual(REPORT_ACTION.person); expect(resultAction.pendingAction).toBeNull(); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(3); @@ -1946,7 +1946,7 @@ describe('actions/IOU', () => { fetch.pause(); // When we delete the money request IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then we expect the moneyRequestPreview to show [Deleted request] @@ -1984,12 +1984,12 @@ describe('actions/IOU', () => { }); it('update IOU report and reportPreview with new totals and messages if the IOU report is not deleted', async () => { - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`, callback: (val) => (iouReport = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given a second money request in addition to the first one @@ -1998,7 +1998,7 @@ describe('actions/IOU', () => { const comment2 = 'Send me money please 2'; IOU.requestMoney(chatReport, amount2, CONST.CURRENCY.USD, '', '', TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID, {login: RORY_EMAIL, accountID: RORY_ACCOUNT_ID}, comment2); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then we expect the IOU report and reportPreview to update with new totals @@ -2016,7 +2016,7 @@ describe('actions/IOU', () => { fetch.pause(); jest.advanceTimersByTime(10); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Then we expect the IOU report and reportPreview to update with new totals @@ -2038,20 +2038,20 @@ describe('actions/IOU', () => { }); it('navigate the user correctly to the iou Report when appropriate', async () => { - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${IOU_REPORT_ID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given an added comment to the iou report jest.advanceTimersByTime(10); Report.addComment(IOU_REPORT_ID, 'Testing a comment'); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); const resultAction = _.find(reportActions, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT); reportActionID = resultAction.reportActionID; @@ -2059,12 +2059,12 @@ describe('actions/IOU', () => { expect(resultAction.message).toEqual(REPORT_ACTION.message); expect(resultAction.person).toEqual(REPORT_ACTION.person); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Verify there are three actions (created + iou + addcomment) and our optimistic comment has been removed expect(_.size(reportActions)).toBe(3); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // Given a thread report @@ -2075,12 +2075,12 @@ describe('actions/IOU', () => { key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`, callback: (val) => (reportActions = val), }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); jest.advanceTimersByTime(10); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); const allReportActions = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -2097,14 +2097,14 @@ describe('actions/IOU', () => { createIOUAction = _.find(reportActionsForIOUReport, (ra) => ra.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); expect(createIOUAction.childReportID).toBe(thread.reportID); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); // When we delete the money request in SingleTransactionView and we should not delete the IOU report fetch.pause(); IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); let allReports = await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -2117,7 +2117,7 @@ describe('actions/IOU', () => { }); }); - await waitForPromisesToResolve(); + await waitForBatchedUpdates(); iouReport = _.find(allReports, (report) => ReportUtils.isIOUReport(report)); expect(iouReport).toBeTruthy(); From 6c8b02618e59df8f973370d8d90d429ffcd4c484 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:22:33 +0000 Subject: [PATCH 21/24] fix test error --- tests/actions/IOUTest.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 86fc4b04ea54..baa292a5c4e9 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1814,6 +1814,7 @@ describe('actions/IOU', () => { // Then the report should have 2 actions expect(_.size(reportActions)).toBe(2); const resultActionAfter = reportActions[reportActionID]; + await waitForBatchedUpdates(); expect(resultActionAfter.pendingAction).toBeNull(); fetch.pause(); From 9f616ec57fa2d975532a04cf038e95eb82438312 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:28:12 +0000 Subject: [PATCH 22/24] fix test error --- tests/actions/IOUTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index baa292a5c4e9..23a8d5f555e4 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1813,8 +1813,8 @@ describe('actions/IOU', () => { // Then the report should have 2 actions expect(_.size(reportActions)).toBe(2); - const resultActionAfter = reportActions[reportActionID]; await waitForBatchedUpdates(); + const resultActionAfter = reportActions[reportActionID]; expect(resultActionAfter.pendingAction).toBeNull(); fetch.pause(); From 9658205975fccae22b364e987f502063055337f8 Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:32:48 +0000 Subject: [PATCH 23/24] fix test error --- tests/actions/IOUTest.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index 23a8d5f555e4..fbb4de192022 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1799,6 +1799,8 @@ describe('actions/IOU', () => { }); }); + jest.advanceTimersByTime(10); + // When a comment is added Report.addComment(thread.reportID, 'Testing a comment'); await waitForBatchedUpdates(); @@ -1813,7 +1815,6 @@ describe('actions/IOU', () => { // Then the report should have 2 actions expect(_.size(reportActions)).toBe(2); - await waitForBatchedUpdates(); const resultActionAfter = reportActions[reportActionID]; expect(resultActionAfter.pendingAction).toBeNull(); From 80a624fb124c93ab1b7b208d681b9cdc7e99a15d Mon Sep 17 00:00:00 2001 From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:39:12 +0000 Subject: [PATCH 24/24] fix test error --- tests/actions/IOUTest.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/actions/IOUTest.js b/tests/actions/IOUTest.js index fbb4de192022..7372bb76b9e5 100644 --- a/tests/actions/IOUTest.js +++ b/tests/actions/IOUTest.js @@ -1779,7 +1779,9 @@ describe('actions/IOU', () => { // Given a transaction thread thread = ReportUtils.buildTransactionThread(createIOUAction); const userLogins = PersonalDetailsUtils.getLoginsByAccountIDs(thread.participantAccountIDs); + jest.advanceTimersByTime(10); Report.openReport(thread.reportID, userLogins, thread, createIOUAction.reportActionID); + await waitForBatchedUpdates(); Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,