@@ -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,109 @@ 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 ( jasmine . objectContaining ( { type : 'beforeSubmitOrder' } ) ) ;
393+
394+ if ( paymentMethod != 'payment1' ) {
395+ $ . prototype . trigger . and . callFake ( function ( event ) {
396+ if ( event . type === 'beforeSubmitOrder' ) {
397+ event . result = false ;
398+ }
399+ } ) ;
400+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( true ) ;
401+ } else {
402+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( false ) ;
403+
404+ }
405+ }
406+
407+ it ( 'Check that payment custom handler is executed and button states #1' , function ( ) {
408+ testSubmit (
409+ null ,
410+ 'payment1' ,
411+ {
412+ url : '/admin/sales/order/create/payment_method/payment1' ,
413+ data : {
414+ code : 'payment1'
415+ }
416+ }
417+ ) ;
418+ } ) ;
419+
420+ it ( 'Check that payment custom handler is executed and button states #2' , function ( ) {
421+ testSubmit (
422+ 'payment1' ,
423+ 'payment1' ,
424+ {
425+ url : '/admin/sales/order/create/payment_method/payment1' ,
426+ data : {
427+ code : 'payment1'
428+ }
429+ }
430+ ) ;
431+ } ) ;
432+
433+ it ( 'Validate re-enabling the button for canceled events' , function ( ) {
434+ order = new window . AdminOrder ( { } ) ;
435+ spyOn ( order , 'submit' ) . and . callFake ( function ( ) {
436+ const $editForm = $ ( '#edit_form' ) ;
437+ if ( $editForm . valid ( ) ) {
438+ $submitButton . prop ( 'disabled' , true ) ;
439+ const beforeSubmitOrderEvent = $ . Event ( 'beforeSubmitOrder' ) ;
440+ $editForm . trigger ( beforeSubmitOrderEvent ) ;
441+
442+ if ( beforeSubmitOrderEvent . result !== false ) {
443+ $editForm . trigger ( 'submitOrder' ) ;
444+ } else {
445+ $submitButton . prop ( 'disabled' , false ) ;
446+ }
447+ }
448+ } ) ;
449+ spyOn ( $ . prototype , 'trigger' ) . and . callFake ( function ( event ) {
450+ if ( event . type === 'beforeSubmitOrder' ) {
451+ event . result = false ;
452+ }
453+ } ) ;
454+ $ . prototype . trigger . and . callFake ( function ( event ) {
455+ if ( event . type === 'beforeSubmitOrder' ) {
456+ event . result = false ;
457+ }
458+ } ) ;
459+ order . submit ( ) ;
460+ expect ( $submitButton . prop ( 'disabled' ) ) . toBe ( false ) ;
461+ } ) ;
462+
463+ it ( 'Check button state for non-payment1 methods' , function ( ) {
464+ testSubmit (
465+ 'payment2' ,
466+ 'payment2' ,
467+ {
468+ url : '/admin/sales/order/create/payment_method/payment2' ,
469+ data : {
470+ code : 'payment2'
471+ }
472+ }
473+ ) ;
474+ } ) ;
475+ } ) ;
476+
372477 } ) ;
373478} ) ;
0 commit comments