Skip to content

Commit

Permalink
feat(payment): PAYPAL-4051 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-nick committed Apr 19, 2024
1 parent 7956856 commit fd7ed91
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -590,28 +590,34 @@ describe('ApplePayCustomerStrategy', () => {
});

describe('braintree gateway', () => {
const getPaymentMethodCallback = (methodId: string) => {
if (methodId === 'applepay') {
const applePayPaymentMethod = getApplePay();

applePayPaymentMethod.initializationData.gateway = 'braintree';

return applePayPaymentMethod;
}

if (methodId === 'braintree') {
return getBraintree();
}

return {};
};

beforeEach(() => {
jest.spyOn(paymentIntegrationService, 'loadPaymentMethod').mockReturnValue(
paymentIntegrationService.getState(),
);
jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethodOrThrow',
).mockImplementation((methodId) => {
if (methodId === 'applepay') {
const applePayPaymentMethod = getApplePay();

applePayPaymentMethod.initializationData.gateway = 'braintree';

return applePayPaymentMethod;
}

if (methodId === 'braintree') {
return getBraintree();
}

return {};
});
).mockImplementation(getPaymentMethodCallback);
jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethod',
).mockImplementation(getPaymentMethodCallback);

jest.spyOn(braintreeIntegrationService, 'getClient').mockImplementation(
() => 'token',
Expand Down Expand Up @@ -666,6 +672,66 @@ describe('ApplePayCustomerStrategy', () => {
}
}
});

it('sends a payment without deviceSessionId in case the request braintree fails', async () => {
jest.spyOn(paymentIntegrationService, 'loadPaymentMethod').mockImplementation(
(methodId) => {
if (methodId === 'braintree') {
return Promise.reject();
}

return paymentIntegrationService.getState();
},
);

jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethod',
).mockReturnValue(undefined);

const authEvent = {
payment: {
billingContact: getContactAddress(),
shippingContact: getContactAddress(),
token: {
paymentData: {},
paymentMethod: {},
transactionIdentifier: {},
},
},
} as ApplePayJS.ApplePayPaymentAuthorizedEvent;

const customerInitializeOptions = getApplePayCustomerInitializationOptions();

await strategy.initialize(customerInitializeOptions);

if (customerInitializeOptions.applepay) {
const buttonContainer = document.getElementById(
customerInitializeOptions.applepay.container,
);
const button = buttonContainer?.firstChild as HTMLElement;

if (button) {
button.click();
await applePaySession.onpaymentauthorized(authEvent);

expect(paymentIntegrationService.loadPaymentMethod).toHaveBeenCalledWith(
'braintree',
);
expect(paymentIntegrationService.submitPayment).toHaveBeenCalledWith(
expect.objectContaining({
paymentData: expect.objectContaining({
deviceSessionId: undefined,
}),
}),
);
expect(applePaySession.completePayment).toHaveBeenCalled();
expect(
customerInitializeOptions.applepay.onPaymentAuthorize,
).toHaveBeenCalled();
}
}
});
});

it('returns an error if autorize payment fails', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ export default class ApplePayCustomerStrategy implements CustomerStrategy {
storeConfig,
);
} catch (_) {
return noop();
// we don't need to do anything in this block
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,32 @@ describe('ApplePayPaymentStrategy', () => {
});

describe('braintree gateway', () => {
const getPaymentMethodCallback = (methodId: string) => {
if (methodId === 'applepay') {
const applePayPaymentMethod = getApplePay();

applePayPaymentMethod.initializationData.gateway = 'braintree';

return applePayPaymentMethod;
}

if (methodId === 'braintree') {
return getBraintree();
}

return {};
};

beforeEach(() => {
jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethodOrThrow',
).mockImplementation((methodId) => {
if (methodId === 'applepay') {
const applePayPaymentMethod = getApplePay();

applePayPaymentMethod.initializationData.gateway = 'braintree';

return applePayPaymentMethod;
}
).mockImplementation(getPaymentMethodCallback);

if (methodId === 'braintree') {
return getBraintree();
}

return {};
});
jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethod',
).mockImplementation(getPaymentMethodCallback);

jest.spyOn(braintreeIntegrationService, 'getClient').mockImplementation(
() => 'token',
Expand Down Expand Up @@ -246,6 +253,54 @@ describe('ApplePayPaymentStrategy', () => {
}),
);
});

it('sends a payment without deviceSessionId in case the request braintree fails', async () => {
jest.spyOn(paymentIntegrationService, 'loadPaymentMethod').mockImplementation(
(methodId) => {
if (methodId === 'braintree') {
return Promise.reject();
}

return paymentIntegrationService.getState();
},
);

jest.spyOn(
paymentIntegrationService.getState(),
'getPaymentMethod',
).mockReturnValue(undefined);

await strategy.initialize({ methodId: 'applepay' });

const payload = merge({}, getOrderRequestBody(), {
payment: { methodId: paymentMethod.id },
});
const authEvent = {
payment: {
token: {
paymentData: {},
paymentMethod: {},
transactionIdentifier: {},
},
},
} as ApplePayJS.ApplePayPaymentAuthorizedEvent;

strategy.execute(payload);
await new Promise((resolve) => process.nextTick(resolve));
await applePaySession.onpaymentauthorized(authEvent);

expect(paymentIntegrationService.loadPaymentMethod).toHaveBeenCalledWith(
ApplePayGatewayType.BRAINTREE,
);

expect(paymentIntegrationService.submitPayment).toHaveBeenCalledWith(
expect.objectContaining({
paymentData: expect.objectContaining({
deviceSessionId: undefined,
}),
}),
);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RequestSender } from '@bigcommerce/request-sender';
import { noop } from 'lodash';

import { BraintreeIntegrationService } from '@bigcommerce/checkout-sdk/braintree-utils';
import {
Expand Down Expand Up @@ -300,7 +299,7 @@ export default class ApplePayPaymentStrategy implements PaymentStrategy {
storeConfig,
);
} catch (_) {
return noop();
// we don't need to do anything in this block
}
}
}

0 comments on commit fd7ed91

Please sign in to comment.