@@ -135,9 +135,23 @@ const renderComponent = (configProps = {}) => {
135135 ) ;
136136} ;
137137
138+ const setOrderResources = ( {
139+ order = { } ,
140+ ...rest
141+ } = { } ) => {
142+ usePurchaseOrderResources . mockReturnValue ( {
143+ ...orderRelatedData ,
144+ ...rest ,
145+ order : {
146+ ...ORDER ,
147+ ...order ,
148+ } ,
149+ } ) ;
150+ } ;
151+
138152describe ( 'PO' , ( ) => {
139153 beforeEach ( ( ) => {
140- usePurchaseOrderResources . mockReturnValue ( orderRelatedData ) ;
154+ setOrderResources ( ) ;
141155 } ) ;
142156
143157 afterEach ( ( ) => {
@@ -294,6 +308,27 @@ describe('PO actions', () => {
294308 expect ( defaultProps . mutator . generatedOrderNumber . GET ) . toHaveBeenCalled ( ) ;
295309 } ) ;
296310
311+ it ( 'should pass clone error options to order update error handler' , async ( ) => {
312+ defaultProps . mutator . generatedOrderNumber . GET . mockRejectedValueOnce ( { } ) ;
313+
314+ renderComponent ( ) ;
315+
316+ const cloneBtn = await screen . findByTestId ( 'clone-order-button' ) ;
317+
318+ await act ( async ( ) => user . click ( cloneBtn ) ) ;
319+
320+ const confirmBtn = await screen . findByText ( 'ui-orders.order.clone.confirmLabel' ) ;
321+
322+ await act ( async ( ) => user . click ( confirmBtn ) ) ;
323+
324+ await waitFor ( ( ) => (
325+ expect ( mockHandleOrderUpdateError ) . toHaveBeenCalledWith ( expect . anything ( ) , expect . objectContaining ( {
326+ genericCode : 'clone.error' ,
327+ openModal : expect . any ( Function ) ,
328+ } ) )
329+ ) ) ;
330+ } ) ;
331+
297332 it ( 'should delete order after confirmation' , async ( ) => {
298333 renderComponent ( ) ;
299334
@@ -308,6 +343,24 @@ describe('PO actions', () => {
308343 expect ( defaultProps . mutator . orderDetails . DELETE ) . toHaveBeenCalled ( ) ;
309344 } ) ;
310345
346+ it ( 'should process delete order error without navigation' , async ( ) => {
347+ defaultProps . mutator . orderDetails . DELETE . mockRejectedValueOnce ( { } ) ;
348+ history . replace . mockClear ( ) ;
349+
350+ renderComponent ( ) ;
351+
352+ const deleteBtn = await screen . findByTestId ( 'button-delete-order' ) ;
353+
354+ await act ( async ( ) => user . click ( deleteBtn ) ) ;
355+
356+ const confirmBtn = await screen . findByText ( 'ui-orders.order.delete.confirmLabel' ) ;
357+
358+ await act ( async ( ) => user . click ( confirmBtn ) ) ;
359+
360+ await waitFor ( ( ) => expect ( defaultProps . mutator . orderDetails . DELETE ) . toHaveBeenCalled ( ) ) ;
361+ expect ( history . replace ) . not . toHaveBeenCalled ( ) ;
362+ } ) ;
363+
311364 it ( 'should approve order' , async ( ) => {
312365 renderComponent ( {
313366 resources : {
@@ -327,6 +380,32 @@ describe('PO actions', () => {
327380 expect ( defaultProps . mutator . orderDetails . PUT ) . toHaveBeenCalled ( ) ;
328381 } ) ;
329382
383+ it ( 'should pass approve action options to order update error handler' , async ( ) => {
384+ defaultProps . mutator . orderDetails . PUT . mockRejectedValueOnce ( { } ) ;
385+
386+ renderComponent ( {
387+ resources : {
388+ ...defaultProps . resources ,
389+ approvalsSetting : {
390+ records : [ {
391+ value : JSON . stringify ( { isApprovalRequired : true } ) ,
392+ } ] ,
393+ } ,
394+ } ,
395+ } ) ;
396+
397+ const approveBtn = await screen . findByTestId ( 'approve-order-button' ) ;
398+
399+ await act ( async ( ) => user . click ( approveBtn ) ) ;
400+
401+ await waitFor ( ( ) => (
402+ expect ( mockHandleOrderUpdateError ) . toHaveBeenCalledWith ( expect . anything ( ) , expect . objectContaining ( {
403+ actionType : PO_UPDATE_ACTION_TYPES . APPROVE ,
404+ openModal : expect . any ( Function ) ,
405+ } ) )
406+ ) ) ;
407+ } ) ;
408+
330409 it ( 'should update order details after reexport' , async ( ) => {
331410 renderComponent ( ) ;
332411
@@ -344,10 +423,8 @@ describe('PO actions', () => {
344423
345424 describe ( 'a pending order' , ( ) => {
346425 it ( 'should open order after confirmation' , async ( ) => {
347- usePurchaseOrderResources . mockReturnValue ( {
348- ...orderRelatedData ,
426+ setOrderResources ( {
349427 order : {
350- ...ORDER ,
351428 workflowStatus : ORDER_STATUSES . pending ,
352429 } ,
353430 } ) ;
@@ -366,10 +443,8 @@ describe('PO actions', () => {
366443 } ) ;
367444
368445 it ( 'should pass open action options to order update error handler' , async ( ) => {
369- usePurchaseOrderResources . mockReturnValue ( {
370- ...orderRelatedData ,
446+ setOrderResources ( {
371447 order : {
372- ...ORDER ,
373448 workflowStatus : ORDER_STATUSES . pending ,
374449 } ,
375450 } ) ;
@@ -394,14 +469,43 @@ describe('PO actions', () => {
394469 } ) )
395470 ) ) ;
396471 } ) ;
472+
473+ it ( 'should show different account modal when opening with multiple export accounts' , async ( ) => {
474+ setOrderResources ( {
475+ order : {
476+ workflowStatus : ORDER_STATUSES . pending ,
477+ manualPo : false ,
478+ } ,
479+ orderLines : [ {
480+ cost : { currency : 'USD' } ,
481+ automaticExport : true ,
482+ vendorDetail : { vendorAccount : 'ACC-1' } ,
483+ } , {
484+ cost : { currency : 'USD' } ,
485+ automaticExport : true ,
486+ vendorDetail : { vendorAccount : 'ACC-2' } ,
487+ } ] ,
488+ } ) ;
489+
490+ renderComponent ( ) ;
491+
492+ const openBtn = await screen . findByTestId ( 'open-order-button' ) ;
493+
494+ await act ( async ( ) => user . click ( openBtn ) ) ;
495+
496+ const confirmOpenBtn = await screen . findByText ( 'ui-orders.openOrderModal.submit' ) ;
497+
498+ await act ( async ( ) => user . click ( confirmOpenBtn ) ) ;
499+
500+ expect ( await screen . findByText ( 'ui-orders.differentAccounts.title' ) ) . toBeInTheDocument ( ) ;
501+ expect ( defaultProps . mutator . orderDetails . PUT ) . not . toHaveBeenCalled ( ) ;
502+ } ) ;
397503 } ) ;
398504
399505 describe ( 'a closed order' , ( ) => {
400506 it ( 'should reopen order' , async ( ) => {
401- usePurchaseOrderResources . mockReturnValue ( {
402- ...orderRelatedData ,
507+ setOrderResources ( {
403508 order : {
404- ...ORDER ,
405509 workflowStatus : ORDER_STATUSES . closed ,
406510 } ,
407511 } ) ;
@@ -414,14 +518,34 @@ describe('PO actions', () => {
414518
415519 expect ( defaultProps . mutator . orderDetails . PUT ) . toHaveBeenCalled ( ) ;
416520 } ) ;
521+
522+ it ( 'should pass reopen action options to order update error handler' , async ( ) => {
523+ setOrderResources ( {
524+ order : {
525+ workflowStatus : ORDER_STATUSES . closed ,
526+ } ,
527+ } ) ;
528+ defaultProps . mutator . orderDetails . PUT . mockRejectedValueOnce ( { } ) ;
529+
530+ renderComponent ( ) ;
531+
532+ const reopenBtn = await screen . findByTestId ( 'reopen-order-button' ) ;
533+
534+ await act ( async ( ) => user . click ( reopenBtn ) ) ;
535+
536+ await waitFor ( ( ) => (
537+ expect ( mockHandleOrderUpdateError ) . toHaveBeenCalledWith ( expect . anything ( ) , expect . objectContaining ( {
538+ actionType : PO_UPDATE_ACTION_TYPES . REOPEN ,
539+ openModal : expect . any ( Function ) ,
540+ } ) )
541+ ) ) ;
542+ } ) ;
417543 } ) ;
418544
419545 describe ( 'adding PO Line' , ( ) => {
420546 it ( 'should create new POLine if the linelimit is not exceeded' , async ( ) => {
421- usePurchaseOrderResources . mockReturnValue ( {
422- ...orderRelatedData ,
547+ setOrderResources ( {
423548 order : {
424- ...ORDER ,
425549 workflowStatus : ORDER_STATUSES . pending ,
426550 } ,
427551 } ) ;
@@ -438,10 +562,8 @@ describe('PO actions', () => {
438562 } ) ;
439563
440564 it ( 'should create new PO if the line limit is exceeded' , async ( ) => {
441- usePurchaseOrderResources . mockReturnValue ( {
442- ...orderRelatedData ,
565+ setOrderResources ( {
443566 order : {
444- ...ORDER ,
445567 workflowStatus : ORDER_STATUSES . pending ,
446568 } ,
447569 orderLines : [ {
@@ -499,10 +621,8 @@ describe('PO errors', () => {
499621 it ( 'should handle errors on update order' , async ( ) => {
500622 defaultProps . mutator . orderDetails . PUT . mockRejectedValue ( { } ) ;
501623
502- usePurchaseOrderResources . mockReturnValue ( {
503- ...orderRelatedData ,
624+ setOrderResources ( {
504625 order : {
505- ...ORDER ,
506626 workflowStatus : ORDER_STATUSES . pending ,
507627 approved : true ,
508628 } ,
@@ -524,6 +644,56 @@ describe('PO errors', () => {
524644 expect ( defaultProps . mutator . orderDetails . PUT ) . toHaveBeenCalled ( ) ;
525645 } ) ;
526646 } ) ;
647+
648+ it ( 'should open and close update error modal from open order flow' , async ( ) => {
649+ defaultProps . mutator . orderDetails . PUT . mockRejectedValueOnce ( { } ) ;
650+
651+ mockHandleOrderUpdateError . mockImplementationOnce ( async ( _error , { openModal } ) => {
652+ openModal ( [ { code : ERROR_CODES . vendorNotFound } ] ) ;
653+ } ) ;
654+
655+ setOrderResources ( {
656+ order : {
657+ workflowStatus : ORDER_STATUSES . pending ,
658+ } ,
659+ } ) ;
660+
661+ renderComponent ( ) ;
662+
663+ const openOrderBtn = await screen . findByTestId ( 'open-order-button' ) ;
664+
665+ await act ( async ( ) => user . click ( openOrderBtn ) ) ;
666+
667+ await act ( async ( ) => user . click ( screen . getByText ( 'ui-orders.openOrderModal.submit' ) ) ) ;
668+
669+ expect ( await screen . findByText ( 'ui-orders.errors.vendorNotFound' ) ) . toBeInTheDocument ( ) ;
670+
671+ await act ( async ( ) => user . click ( screen . getByText ( 'ui-orders.openOrderModal.cancel' ) ) ) ;
672+
673+ await waitFor ( ( ) => expect ( screen . queryByText ( 'ui-orders.errors.vendorNotFound' ) ) . not . toBeInTheDocument ( ) ) ;
674+ } ) ;
675+
676+ it ( 'should close close-order modal without submitting' , async ( ) => {
677+ setOrderResources ( {
678+ order : {
679+ workflowStatus : ORDER_STATUSES . open ,
680+ } ,
681+ } ) ;
682+
683+ renderComponent ( ) ;
684+ defaultProps . mutator . orderDetails . PUT . mockClear ( ) ;
685+
686+ const closeBtn = await screen . findByTestId ( 'close-order-button' ) ;
687+
688+ await act ( async ( ) => user . click ( closeBtn ) ) ;
689+
690+ expect ( await screen . findByText ( 'ui-orders.closeOrderModal.submit' ) ) . toBeInTheDocument ( ) ;
691+
692+ await act ( async ( ) => user . click ( screen . getByText ( 'ui-orders.closeOrderModal.cancel' ) ) ) ;
693+
694+ await waitFor ( ( ) => expect ( screen . queryByText ( 'ui-orders.closeOrderModal.submit' ) ) . not . toBeInTheDocument ( ) ) ;
695+ expect ( defaultProps . mutator . orderDetails . PUT ) . not . toHaveBeenCalled ( ) ;
696+ } ) ;
527697} ) ;
528698
529699describe ( 'PO shortcuts' , ( ) => {
@@ -572,10 +742,8 @@ describe('PO shortcuts', () => {
572742 } ) ;
573743
574744 it ( 'should translate to POL creation form' , async ( ) => {
575- usePurchaseOrderResources . mockReturnValue ( {
576- ...orderRelatedData ,
745+ setOrderResources ( {
577746 order : {
578- ...ORDER ,
579747 workflowStatus : ORDER_STATUSES . pending ,
580748 } ,
581749 } ) ;
0 commit comments