Skip to content

Commit 9cb0e71

Browse files
committed
feat(payment): PAYPAL-2615 moved braintree visa checkout to separate package
1 parent 64d49e9 commit 9cb0e71

9 files changed

+632
-69
lines changed

packages/braintree-integration/src/braintree-visa-checkout/braintree-visa-checkout-button-strategy.spec.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {
1111
getBraintree,
1212
getDataCollectorMock,
1313
getDeviceDataMock,
14+
getPaymentSuccessPayload,
1415
getVisaCheckoutMock,
1516
getVisaCheckoutSDKMock,
1617
VisaCheckoutHostWindow,
18+
VisaCheckoutPaymentSuccessPayload,
1719
VisaCheckoutSDK,
1820
} from '@bigcommerce/checkout-sdk/braintree-utils';
1921
import {
@@ -39,6 +41,7 @@ describe('BraintreeVisaCheckoutButtonStrategy', () => {
3941
let braintreeVisaCheckoutButtonElement: HTMLDivElement;
4042
let dataCollector: BraintreeDataCollector;
4143
let braintreeSDKVersionManager: BraintreeSDKVersionManager;
44+
let visaPayload: VisaCheckoutPaymentSuccessPayload;
4245

4346
const defaultContainerId = 'braintree-visa-checkout-button-mock-id';
4447

@@ -48,6 +51,8 @@ describe('BraintreeVisaCheckoutButtonStrategy', () => {
4851
};
4952

5053
beforeEach(() => {
54+
visaPayload = getPaymentSuccessPayload();
55+
5156
mockWindow = {} as VisaCheckoutHostWindow & BraintreeHostWindow;
5257

5358
dataCollector = getDataCollectorMock();
@@ -180,13 +185,8 @@ describe('BraintreeVisaCheckoutButtonStrategy', () => {
180185
});
181186

182187
it('visa Checkout tokenization', async () => {
183-
// TODO: remove rule and update test with related type (PAYPAL-4383)
184-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
185-
visaCheckoutSDKMock.on = jest.fn((type, callback) => {
186-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
187-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
188-
// @ts-ignore
189-
return type === 'payment.success' ? callback('data') : undefined;
188+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
189+
callback(visaPayload, new Error());
190190
});
191191

192192
await strategy.initialize(initializationOptions);
@@ -209,13 +209,8 @@ describe('BraintreeVisaCheckoutButtonStrategy', () => {
209209
postal_code: '2008',
210210
});
211211

212-
// TODO: remove rule and update test with related type (PAYPAL-4383)
213-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
214-
visaCheckoutSDKMock.on = jest.fn((type, callback) => {
215-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
216-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
217-
// @ts-ignore
218-
return type === 'payment.success' ? callback('data') : undefined;
212+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
213+
callback(visaPayload, new Error());
219214
});
220215

221216
await strategy.initialize(initializationOptions);

packages/braintree-integration/src/braintree-visa-checkout/braintree-visa-checkout-customer-strategy.spec.ts

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import {
1111
getBraintree,
1212
getDataCollectorMock,
1313
getDeviceDataMock,
14+
getPaymentSuccessPayload,
1415
getVisaCheckoutMock,
1516
getVisaCheckoutSDKMock,
1617
VisaCheckoutHostWindow,
18+
VisaCheckoutPaymentSuccessPayload,
1719
VisaCheckoutSDK,
1820
} from '@bigcommerce/checkout-sdk/braintree-utils';
1921
import {
@@ -41,6 +43,7 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
4143
let braintreeVisaCheckoutButtonElement: HTMLDivElement;
4244
let dataCollector: BraintreeDataCollector;
4345
let braintreeSDKVersionManager: BraintreeSDKVersionManager;
46+
let visaPayload: VisaCheckoutPaymentSuccessPayload;
4447

4548
const defaultContainerId = 'braintree-visa-checkout-button-mock-id';
4649

@@ -54,6 +57,8 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
5457
};
5558

5659
beforeEach(() => {
60+
visaPayload = getPaymentSuccessPayload();
61+
5762
mockWindow = {} as VisaCheckoutHostWindow & BraintreeHostWindow;
5863

5964
dataCollector = getDataCollectorMock();
@@ -91,29 +96,19 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
9196
jest.spyOn(paymentIntegrationService.getState(), 'getPaymentMethodOrThrow').mockReturnValue(
9297
paymentMethodMock,
9398
);
94-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
95-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
96-
// @ts-ignore
97-
jest.spyOn(paymentIntegrationService, 'loadCheckout').mockReturnValue(true);
99+
jest.spyOn(paymentIntegrationService, 'loadCheckout');
98100

99101
jest.spyOn(braintreeSdk, 'initialize');
100102
jest.spyOn(braintreeSdk, 'deinitialize');
101-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
102-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
103-
// @ts-ignore
104-
jest.spyOn(braintreeSdk, 'getBraintreeVisaCheckout').mockReturnValue(braintreeVisaCheckout);
105-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
106-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
107-
// @ts-ignore
108-
jest.spyOn(braintreeSdk, 'getDataCollectorOrThrow').mockReturnValue(dataCollector);
109-
110-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
111-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
112-
// @ts-ignore
103+
jest.spyOn(braintreeSdk, 'getBraintreeVisaCheckout').mockResolvedValue(
104+
braintreeVisaCheckout,
105+
);
106+
jest.spyOn(braintreeSdk, 'getDataCollectorOrThrow').mockResolvedValue(dataCollector);
107+
113108
jest.spyOn(braintreeSdk, 'getVisaCheckoutSdk').mockImplementation(() => {
114109
mockWindow.V = visaCheckoutSDKMock;
115110

116-
return mockWindow.V;
111+
return Promise.resolve(mockWindow.V);
117112
});
118113

119114
jest.spyOn(formPoster, 'postForm').mockImplementation(() => {});
@@ -135,7 +130,6 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
135130
try {
136131
await strategy.initialize({
137132
...initializationOptions,
138-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
139133
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
140134
// @ts-ignore
141135
braintreevisacheckout: {},
@@ -163,14 +157,7 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
163157

164158
it('throws error if methodId is missing', async () => {
165159
try {
166-
await strategy.initialize({
167-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
168-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
169-
// @ts-ignore
170-
braintreevisacheckout: {
171-
...initializationOptions.braintreevisacheckout,
172-
},
173-
});
160+
await strategy.initialize({});
174161
} catch (error) {
175162
expect(error).toBeInstanceOf(InvalidArgumentError);
176163
}
@@ -209,13 +196,8 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
209196
});
210197

211198
it('visa Checkout tokenization', async () => {
212-
// TODO: remove rule and update test with related type (PAYPAL-4383)
213-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
214-
visaCheckoutSDKMock.on = jest.fn((type, callback) => {
215-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
216-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
217-
// @ts-ignore
218-
return type === 'payment.success' ? callback('data') : undefined;
199+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
200+
callback(visaPayload, new Error());
219201
});
220202

221203
await strategy.initialize(initializationOptions);
@@ -225,13 +207,8 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
225207
});
226208

227209
it('registers the error and success callbacks', async () => {
228-
// TODO: remove rule and update test with related type (PAYPAL-4383)
229-
230-
visaCheckoutSDKMock.on = jest.fn((_, callback) => {
231-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
232-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
233-
// @ts-ignore
234-
callback();
210+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
211+
callback(visaPayload, new Error());
235212
});
236213
await strategy.initialize(initializationOptions);
237214

@@ -247,13 +224,8 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
247224

248225
describe('when payment.success', () => {
249226
beforeEach(() => {
250-
// TODO: remove rule and update test with related type (PAYPAL-4383)
251-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
252-
visaCheckoutSDKMock.on = jest.fn((type, callback) => {
253-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
254-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
255-
// @ts-ignore
256-
return type === 'payment.success' ? callback('data') : undefined;
227+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
228+
callback(visaPayload, new Error());
257229
});
258230
});
259231

@@ -301,14 +273,9 @@ describe('BraintreeVisaCheckoutCustomerStrategy', () => {
301273
it('payment error triggers onError from the options', async () => {
302274
const errorMock = new Error();
303275

304-
// TODO: remove rule and update test with related type (PAYPAL-4383)
305-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
306-
visaCheckoutSDKMock.on = jest.fn((type, callback) =>
307-
// TODO: remove ts-ignore and update test with related type (PAYPAL-4383)
308-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
309-
// @ts-ignore
310-
type === 'payment.error' ? callback('data', errorMock) : undefined,
311-
);
276+
jest.spyOn(visaCheckoutSDKMock, 'on').mockImplementation((_, callback) => {
277+
callback(visaPayload, errorMock);
278+
});
312279

313280
await strategy.initialize(initializationOptions);
314281

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default interface BraintreeVisaCheckoutPaymentInitializeOptions {
2+
/**
3+
* A callback that gets called when Visa Checkout fails to initialize or
4+
* selects a payment option.
5+
*
6+
* @param error - The error object describing the failure.
7+
*/
8+
onError?(error: Error): void;
9+
10+
/**
11+
* A callback that gets called when the customer selects a payment option.
12+
*/
13+
onPaymentSelect?(): void;
14+
}
15+
16+
export interface WithBraintreeVisaCheckoutPaymentInitializeOptions {
17+
braintreevisacheckout?: BraintreeVisaCheckoutPaymentInitializeOptions;
18+
}

0 commit comments

Comments
 (0)