@@ -33,7 +33,7 @@ import {
3333 updateSplitExpenseAmountField ,
3434} from '@libs/actions/IOU' ;
3535import { createWorkspace , deleteWorkspace , generatePolicyID , setWorkspaceApprovalMode } from '@libs/actions/Policy/Policy' ;
36- import { addComment , deleteReport , notifyNewAction , openReport } from '@libs/actions/Report' ;
36+ import { addComment , createNewReport , deleteReport , notifyNewAction , openReport } from '@libs/actions/Report' ;
3737import { clearAllRelatedReportActionErrors } from '@libs/actions/ReportActions' ;
3838import { subscribeToUserEvents } from '@libs/actions/User' ;
3939import { WRITE_COMMANDS } from '@libs/API/types' ;
@@ -63,7 +63,7 @@ import * as API from '@src/libs/API';
6363import DateUtils from '@src/libs/DateUtils' ;
6464import ONYXKEYS from '@src/ONYXKEYS' ;
6565import ROUTES from '@src/ROUTES' ;
66- import type { Policy , Report , ReportNameValuePairs , SearchResults } from '@src/types/onyx' ;
66+ import type { PersonalDetailsList , Policy , Report , ReportNameValuePairs , SearchResults } from '@src/types/onyx' ;
6767import type { Accountant } from '@src/types/onyx/IOU' ;
6868import type { Participant , ReportCollectionDataSet } from '@src/types/onyx/Report' ;
6969import type { ReportActions , ReportActionsCollectionDataSet } from '@src/types/onyx/ReportAction' ;
@@ -72,6 +72,7 @@ import type {TransactionCollectionDataSet} from '@src/types/onyx/Transaction';
7272import type Transaction from '@src/types/onyx/Transaction' ;
7373import { toCollectionDataSet } from '@src/types/utils/CollectionDataSet' ;
7474import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
75+ import { changeTransactionsReport } from '../../src/libs/actions/Transaction' ;
7576import * as InvoiceData from '../data/Invoice' ;
7677import type { InvoiceTestData } from '../data/Invoice' ;
7778import createRandomPolicy , { createCategoryTaxExpenseRules } from '../utils/collections/policies' ;
@@ -6134,4 +6135,115 @@ describe('actions/IOU', () => {
61346135 expect ( updatedSnapshot ?. data ?. [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` ] ?. receipt ?. source ) . toBe ( source ) ;
61356136 } ) ;
61366137 } ) ;
6138+
6139+ describe ( 'changeTransactionsReport' , ( ) => {
6140+ it ( 'should set the correct optimistic onyx data for reporting a tracked expense' , async ( ) => {
6141+ let personalDetailsList : OnyxEntry < PersonalDetailsList > ;
6142+ let expenseReport : OnyxEntry < Report > ;
6143+ let transaction : OnyxEntry < Transaction > ;
6144+
6145+ // Given a signed in account, which owns a workspace, and has a policy expense chat
6146+ Onyx . set ( ONYXKEYS . SESSION , { email : CARLOS_EMAIL , accountID : CARLOS_ACCOUNT_ID } ) ;
6147+ const creatorPersonalDetails = personalDetailsList ?. [ CARLOS_ACCOUNT_ID ] ?? { accountID : CARLOS_ACCOUNT_ID } ;
6148+
6149+ const policyID = generatePolicyID ( ) ;
6150+ createWorkspace ( CARLOS_EMAIL , true , "Carlos's Workspace" , policyID ) ;
6151+ createNewReport ( creatorPersonalDetails , policyID ) ;
6152+ // Create a tracked expense
6153+ const selfDMReport : Report = {
6154+ ...createRandomReport ( 1 ) ,
6155+ reportID : '10' ,
6156+ chatType : CONST . REPORT . CHAT_TYPE . SELF_DM ,
6157+ } ;
6158+
6159+ const amount = 100 ;
6160+
6161+ trackExpense ( {
6162+ report : selfDMReport ,
6163+ isDraftPolicy : true ,
6164+ action : CONST . IOU . ACTION . CREATE ,
6165+ participantParams : {
6166+ payeeEmail : RORY_EMAIL ,
6167+ payeeAccountID : RORY_ACCOUNT_ID ,
6168+ participant : { accountID : RORY_ACCOUNT_ID } ,
6169+ } ,
6170+ transactionParams : {
6171+ amount,
6172+ currency : CONST . CURRENCY . USD ,
6173+ created : format ( new Date ( ) , CONST . DATE . FNS_FORMAT_STRING ) ,
6174+ merchant : 'merchant' ,
6175+ billable : false ,
6176+ } ,
6177+ } ) ;
6178+ await getOnyxData ( {
6179+ key : ONYXKEYS . COLLECTION . TRANSACTION ,
6180+ waitForCollectionCallback : true ,
6181+ callback : ( allTransactions ) => {
6182+ transaction = Object . values ( allTransactions ?? { } ) . find ( ( t ) => ! ! t ) ;
6183+ } ,
6184+ } ) ;
6185+
6186+ await getOnyxData ( {
6187+ key : ONYXKEYS . COLLECTION . REPORT ,
6188+ waitForCollectionCallback : true ,
6189+ callback : ( allReports ) => {
6190+ expenseReport = Object . values ( allReports ?? { } ) . find ( ( r ) => r ?. type === CONST . REPORT . TYPE . EXPENSE ) ;
6191+ } ,
6192+ } ) ;
6193+
6194+ let iouReportActionOnSelfDMReport : OnyxEntry < ReportAction > ;
6195+ let trackExpenseActionableWhisper : OnyxEntry < ReportAction > ;
6196+
6197+ await getOnyxData ( {
6198+ key : ONYXKEYS . COLLECTION . REPORT_ACTIONS ,
6199+ waitForCollectionCallback : true ,
6200+ callback : ( allReportActions ) => {
6201+ iouReportActionOnSelfDMReport = Object . values ( allReportActions ?. [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ selfDMReport . reportID } ` ] ?? { } ) . find (
6202+ ( r ) => r ?. actionName === CONST . REPORT . ACTIONS . TYPE . IOU ,
6203+ ) ;
6204+ trackExpenseActionableWhisper = Object . values ( allReportActions ?. [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ selfDMReport ?. reportID } ` ] ?? { } ) . find (
6205+ ( r ) => r ?. actionName === CONST . REPORT . ACTIONS . TYPE . ACTIONABLE_TRACK_EXPENSE_WHISPER ,
6206+ ) ;
6207+ } ,
6208+ } ) ;
6209+
6210+ expect ( isMoneyRequestAction ( iouReportActionOnSelfDMReport ) ? getOriginalMessage ( iouReportActionOnSelfDMReport ) ?. IOUTransactionID : undefined ) . toBe ( transaction ?. transactionID ) ;
6211+ expect ( trackExpenseActionableWhisper ) . toBeDefined ( ) ;
6212+
6213+ if ( ! transaction || ! expenseReport ) {
6214+ return ;
6215+ }
6216+
6217+ changeTransactionsReport ( [ transaction ?. transactionID ] , expenseReport ?. reportID ) ;
6218+
6219+ let updatedTransaction : OnyxEntry < Transaction > ;
6220+ let updatedIOUReportActionOnSelfDMReport : OnyxEntry < ReportAction > ;
6221+ let updatedTrackExpenseActionableWhisper : OnyxEntry < ReportAction > ;
6222+
6223+ await getOnyxData ( {
6224+ key : ONYXKEYS . COLLECTION . TRANSACTION ,
6225+ waitForCollectionCallback : true ,
6226+ callback : ( allTransactions ) => {
6227+ updatedTransaction = Object . values ( allTransactions ?? { } ) . find ( ( t ) => t ?. transactionID === transaction ?. transactionID ) ;
6228+ } ,
6229+ } ) ;
6230+
6231+ await getOnyxData ( {
6232+ key : ONYXKEYS . COLLECTION . REPORT_ACTIONS ,
6233+ waitForCollectionCallback : true ,
6234+ callback : ( allReportActions ) => {
6235+ updatedIOUReportActionOnSelfDMReport = Object . values ( allReportActions ?. [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ selfDMReport . reportID } ` ] ?? { } ) . find (
6236+ ( r ) => r ?. actionName === CONST . REPORT . ACTIONS . TYPE . IOU ,
6237+ ) ;
6238+ updatedTrackExpenseActionableWhisper = Object . values ( allReportActions ?. [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ selfDMReport ?. reportID } ` ] ?? { } ) . find (
6239+ ( r ) => r ?. actionName === CONST . REPORT . ACTIONS . TYPE . ACTIONABLE_TRACK_EXPENSE_WHISPER ,
6240+ ) ;
6241+ } ,
6242+ } ) ;
6243+
6244+ expect ( updatedTransaction ?. reportID ) . toBe ( expenseReport ?. reportID ) ;
6245+ expect ( isMoneyRequestAction ( updatedIOUReportActionOnSelfDMReport ) ? getOriginalMessage ( updatedIOUReportActionOnSelfDMReport ) ?. IOUTransactionID : undefined ) . toBe ( undefined ) ;
6246+ expect ( updatedTrackExpenseActionableWhisper ) . toBe ( undefined ) ;
6247+ } ) ;
6248+ } ) ;
61376249} ) ;
0 commit comments