Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions paypal/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1618,8 +1618,17 @@
}

function onCancel() {
// Wallet cancels (subscriptions, Apple Pay) never started an order, so only release one that exists.
if ( running > 0 ) {
running--;
}

thisForm.classList.add( 'frm_loading_form' );
frmFrontForm.removeSubmitLoading( jQuery( thisForm ), 'disable', 0 );

if ( running === 0 && selectedMethod ) {
updateSubmitButtonVisibility( selectedMethod );
}
}

// ---- Submit Button Helpers ----
Expand Down
45 changes: 45 additions & 0 deletions tests/js/paypalBootstrap.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,48 @@ test( 'a page change before the PayPal SDK loads leaves initialization to the bo
runInNewContext( `let thisForm, cardElementContainer, isRecurring;${ source.slice( start, end ) }`, context );
await context.paypalInit();
} );

/**
* Run the real onCancel() with the given in-flight order count and selected method.
*
* @param {number} running The number of orders in flight when the pop-up closes.
* @param {string|null} selected The selected payment method key.
*
* @return {Object} The remaining count and the keys passed to updateSubmitButtonVisibility().
*/
function cancelWith( running, selected ) {
const start = source.indexOf( '\tfunction onCancel()' );
const end = source.indexOf( '\n\t// ---- Submit Button Helpers ----', start );
assert.ok( start >= 0 && end > start, 'onCancel boundaries must exist.' );
const updated = [];
const context = {
thisForm: { classList: { add() {} } },
frmFrontForm: { removeSubmitLoading() {} },
jQuery: value => value,
updateSubmitButtonVisibility: key => updated.push( key ),
};
runInNewContext( `let running = ${ running }; let selectedMethod = ${ JSON.stringify( selected ) };${ source.slice( start, end ) }; function getRunning() { return running; }`, context );
context.onCancel();

return { running: context.getRunning(), updated };
}

test( 'closing the PayPal pop-up releases the order it started so Credit Card can enable Submit', () => {
const result = cancelWith( 1, 'paypal' );

assert.equal( result.running, 0 );
assert.deepEqual( result.updated, [ 'paypal' ] );
} );

test( 'cancelling without an order in flight does not push the order count below zero', () => {
const result = cancelWith( 0, 'paypal' );

assert.equal( result.running, 0 );
} );

test( 'cancelling while another order is still in flight leaves Submit state alone', () => {
const result = cancelWith( 2, 'card' );

assert.equal( result.running, 1 );
assert.deepEqual( result.updated, [] );
} );
Loading