Skip to content

Commit

Permalink
fix: payment processing tests (#8295)
Browse files Browse the repository at this point in the history
  • Loading branch information
frosso authored Feb 29, 2024
1 parent 05268b4 commit fa9bbdf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/fix-payment-processing-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: fix: payment processing tests.


103 changes: 103 additions & 0 deletions client/checkout/classic/test/payment-processing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,25 @@ describe( 'Payment processing', () => {
}
} );
} );

afterEach( () => {
[
'billing_first_name',
'billing_last_name',
'billing_email',
'billing_phone',
'billing_city',
'billing_country',
'billing_address_1',
'billing_address_2',
'billing_postcode',
'billing_state',
].forEach( ( id ) => {
const element = document.getElementById( id );
if ( ! element ) return;

document.body.removeChild( element );
} );
jest.clearAllMocks();
} );

Expand Down Expand Up @@ -390,6 +408,91 @@ describe( 'Payment processing', () => {
expect( checkoutResult ).toBe( false );
} );

test( 'Payment processing creates the correct `name` attribute when both last/first name fields are removed', async () => {
setupBillingDetailsFields();
// pretending that the customizer removed the billing name field
document.body.removeChild(
document.getElementById( 'billing_first_name' )
);
document.body.removeChild(
document.getElementById( 'billing_last_name' )
);
getFingerprint.mockImplementation( () => {
return 'fingerprint';
} );

const mockDomElement = document.createElement( 'div' );
mockDomElement.dataset.paymentMethodType = 'card';

await mountStripePaymentElement( apiMock, mockDomElement );

const checkoutForm = {
submit: jest.fn(),
addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'checkout' ),
};

await processPayment( apiMock, checkoutForm, 'card' );

expect( mockCreatePaymentMethod ).toHaveBeenCalledWith( {
elements: expect.any( Object ),
params: {
billing_details: expect.objectContaining( {
name: undefined,
email: '[email protected]',
phone: '555-1234',
address: expect.any( Object ),
} ),
},
} );
} );

test( 'Payment processing creates the correct `name` attribute when last name field is removed via customizer', async () => {
setupBillingDetailsFields();
// pretending that the customizer removed the billing name field
document.body.removeChild(
document.getElementById( 'billing_first_name' )
);
getFingerprint.mockImplementation( () => {
return 'fingerprint';
} );

const mockDomElement = document.createElement( 'div' );
mockDomElement.dataset.paymentMethodType = 'card';

await mountStripePaymentElement( apiMock, mockDomElement );

const checkoutForm = {
submit: jest.fn(),
addClass: jest.fn( () => ( {
block: jest.fn(),
} ) ),
removeClass: jest.fn( () => ( {
unblock: jest.fn(),
} ) ),
attr: jest.fn().mockReturnValue( 'checkout' ),
};

await processPayment( apiMock, checkoutForm, 'card' );

expect( mockCreatePaymentMethod ).toHaveBeenCalledWith( {
elements: expect.any( Object ),
params: {
billing_details: expect.objectContaining( {
name: 'Doe',
email: '[email protected]',
phone: '555-1234',
address: expect.any( Object ),
} ),
},
} );
} );

test( 'Payment processing adds billing details for checkout', async () => {
setupBillingDetailsFields();
getFingerprint.mockImplementation( () => {
Expand Down

0 comments on commit fa9bbdf

Please sign in to comment.