@@ -33,6 +33,7 @@ define([
3333 '</fieldset>' +
3434 '<input id="p_method_free" type="radio" name="payment[method]" value="free"/>' +
3535 '</div>' +
36+ '<button id="submit_order_top_button" type="button">Submit Order</button>' +
3637 '</form>' ;
3738
3839 $ . widget ( 'magetest.testPaymentMethodA' , {
@@ -369,5 +370,112 @@ define([
369370 ) ;
370371 } ) ;
371372 } ) ;
373+
374+ describe ( 'Check that payment custom handler is executed and button states' , function ( ) {
375+ let $submitButton ;
376+
377+ function testSubmit ( currentPaymentMethod , paymentMethod , ajaxParams ) {
378+ $ . ajax = jasmine . createSpy ( '$.ajax' ) ;
379+ init ( {
380+ method : currentPaymentMethod
381+ } ) ;
382+ $ ( formEl ) . find ( ':radio[value="' + paymentMethod + '"]' ) . prop ( 'checked' , true ) ;
383+ order . switchPaymentMethod ( paymentMethod ) ;
384+
385+ spyOn ( $ . prototype , 'trigger' ) . and . callThrough ( ) ;
386+ order . submit ( ) ;
387+
388+ $submitButton = $ ( '#submit_order_top_button' ) ;
389+ expect ( $ . ajax ) . toHaveBeenCalledTimes ( 1 ) ;
390+ expect ( $ . ajax ) . toHaveBeenCalledWith ( jasmine . objectContaining ( ajaxParams ) ) ;
391+
392+ expect ( $ . prototype . trigger ) . toHaveBeenCalledWith (
393+ jasmine . objectContaining ( { type : 'beforeSubmitOrder' } ) ) ;
394+
395+ if ( paymentMethod !== 'payment1' ) {
396+ $ . prototype . trigger . and . callFake ( function ( event ) {
397+ if ( event . type === 'beforeSubmitOrder' ) {
398+ event . result = false ;
399+ }
400+ } ) ;
401+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( true ) ;
402+ } else {
403+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( false ) ;
404+
405+ }
406+ }
407+
408+ it ( 'Check that payment custom handler is executed and button states #1' , function ( ) {
409+ testSubmit (
410+ null ,
411+ 'payment1' ,
412+ {
413+ url : '/admin/sales/order/create/payment_method/payment1' ,
414+ data : {
415+ code : 'payment1'
416+ }
417+ }
418+ ) ;
419+ } ) ;
420+
421+ it ( 'Check that payment custom handler is executed and button states #2' , function ( ) {
422+ testSubmit (
423+ 'payment1' ,
424+ 'payment1' ,
425+ {
426+ url : '/admin/sales/order/create/payment_method/payment1' ,
427+ data : {
428+ code : 'payment1'
429+ }
430+ }
431+ ) ;
432+ } ) ;
433+
434+ it ( 'Validate re-enabling the button for canceled events' , function ( ) {
435+ order = new window . AdminOrder ( { } ) ;
436+ spyOn ( order , 'submit' ) . and . callFake ( function ( ) {
437+ const $editForm = $ ( '#edit_form' ) ;
438+
439+ if ( $editForm . valid ( ) ) {
440+ $submitButton . prop ( 'disabled' , true ) ;
441+ const beforeSubmitOrderEvent = $ . Event ( 'beforeSubmitOrder' ) ;
442+
443+ $editForm . trigger ( beforeSubmitOrderEvent ) ;
444+
445+ if ( beforeSubmitOrderEvent . result !== false ) {
446+ $editForm . trigger ( 'submitOrder' ) ;
447+ } else {
448+ $submitButton . prop ( 'disabled' , false ) ;
449+ }
450+ }
451+ } ) ;
452+ spyOn ( $ . prototype , 'trigger' ) . and . callFake ( function ( event ) {
453+ if ( event . type === 'beforeSubmitOrder' ) {
454+ event . result = false ;
455+ }
456+ } ) ;
457+ $ . prototype . trigger . and . callFake ( function ( event ) {
458+ if ( event . type === 'beforeSubmitOrder' ) {
459+ event . result = false ;
460+ }
461+ } ) ;
462+ order . submit ( ) ;
463+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( false ) ;
464+ } ) ;
465+
466+ it ( 'Check button state for non-payment1 methods' , function ( ) {
467+ testSubmit (
468+ 'payment2' ,
469+ 'payment2' ,
470+ {
471+ url : '/admin/sales/order/create/payment_method/payment2' ,
472+ data : {
473+ code : 'payment2'
474+ }
475+ }
476+ ) ;
477+ } ) ;
478+ } ) ;
479+
372480 } ) ;
373481} ) ;
0 commit comments