@@ -42,6 +42,7 @@ import {clearAllRelatedReportActionErrors} from '@libs/actions/ReportActions';
4242import { subscribeToUserEvents } from '@libs/actions/User' ;
4343import { WRITE_COMMANDS } from '@libs/API/types' ;
4444import type { ApiCommand } from '@libs/API/types' ;
45+ import { getMicroSecondOnyxErrorWithTranslationKey } from '@libs/ErrorUtils' ;
4546import { translateLocal } from '@libs/Localize' ;
4647import { rand64 } from '@libs/NumberUtils' ;
4748import { getLoginsByAccountIDs } from '@libs/PersonalDetailsUtils' ;
@@ -4646,6 +4647,68 @@ describe('actions/IOU', () => {
46464647 } ) ;
46474648 } ) ;
46484649 } ) ;
4650+
4651+ test ( 'should rollback unhold request on API failure' , ( ) => {
4652+ const iouReport = buildOptimisticIOUReport ( 1 , 2 , 100 , '1' , 'USD' ) ;
4653+ const transaction = buildOptimisticTransaction ( {
4654+ transactionParams : {
4655+ amount : 100 ,
4656+ currency : 'USD' ,
4657+ reportID : iouReport . reportID ,
4658+ } ,
4659+ } ) ;
4660+
4661+ const transactionCollectionDataSet : TransactionCollectionDataSet = {
4662+ [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transaction . transactionID } ` ] : transaction ,
4663+ } ;
4664+ const iouAction : ReportAction = buildOptimisticIOUReportAction ( {
4665+ type : CONST . IOU . REPORT_ACTION_TYPE . CREATE ,
4666+ amount : transaction . amount ,
4667+ currency : transaction . currency ,
4668+ comment : '' ,
4669+ participants : [ ] ,
4670+ transactionID : transaction . transactionID ,
4671+ } ) ;
4672+ const transactionThread = buildTransactionThread ( iouAction , iouReport ) ;
4673+
4674+ const actions : OnyxInputValue < ReportActions > = { [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ iouAction . reportActionID } ` ] : iouAction } ;
4675+ const reportCollectionDataSet : ReportCollectionDataSet = {
4676+ [ `${ ONYXKEYS . COLLECTION . REPORT } ${ transactionThread . reportID } ` ] : transactionThread ,
4677+ [ `${ ONYXKEYS . COLLECTION . REPORT } ${ iouReport . reportID } ` ] : iouReport ,
4678+ } ;
4679+ const actionCollectionDataSet : ReportActionsCollectionDataSet = { [ `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ iouReport . reportID } ` ] : actions } ;
4680+ const comment = 'hold reason' ;
4681+
4682+ return waitForBatchedUpdates ( )
4683+ . then ( ( ) => Onyx . multiSet ( { ...reportCollectionDataSet , ...transactionCollectionDataSet , ...actionCollectionDataSet } ) )
4684+ . then ( ( ) => {
4685+ putOnHold ( transaction . transactionID , comment , transactionThread . reportID ) ;
4686+ return waitForBatchedUpdates ( ) ;
4687+ } )
4688+ . then ( ( ) => {
4689+ mockFetch . fail ( ) ;
4690+ mockFetch ?. resume ?.( ) ;
4691+ unholdRequest ( transaction . transactionID , transactionThread . reportID ) ;
4692+ return waitForBatchedUpdates ( ) ;
4693+ } )
4694+ . then ( ( ) => {
4695+ return new Promise < void > ( ( resolve ) => {
4696+ const connection = Onyx . connect ( {
4697+ key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transaction . transactionID } ` ,
4698+ callback : ( updatedTransaction ) => {
4699+ Onyx . disconnect ( connection ) ;
4700+ expect ( updatedTransaction ?. pendingAction ) . toBeFalsy ( ) ;
4701+ expect ( updatedTransaction ?. comment ?. hold ) . toBeTruthy ( ) ;
4702+ expect ( Object . values ( updatedTransaction ?. errors ?? { } ) ) . toEqual (
4703+ Object . values ( getMicroSecondOnyxErrorWithTranslationKey ( 'iou.error.genericUnholdExpenseFailureMessage' ) ?? { } ) ,
4704+ ) ;
4705+
4706+ resolve ( ) ;
4707+ } ,
4708+ } ) ;
4709+ } ) ;
4710+ } ) ;
4711+ } ) ;
46494712 } ) ;
46504713
46514714 describe ( 'sendInvoice' , ( ) => {
0 commit comments