Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getScriptLoader } from '@bigcommerce/script-loader';
import { merge } from 'lodash';

import {
BraintreeError,
BraintreeErrorType,
BraintreeFastlane,
BraintreeIntegrationService,
BraintreeScriptLoader,
Expand Down Expand Up @@ -527,6 +529,32 @@ describe('BraintreeCreditCardPaymentStrategy', () => {
payload.payment,
);
});

it('throws an error if loadHostedFields finished with an error related to an invalid hosted field when the strategy is not deinitialized', async () => {
const braintreeError: BraintreeError = {
code: 'HOSTED_FIELDS_INVALID_FIELD_SELECTOR',
type: BraintreeErrorType.Merchant,
name: 'BraintreeError',
message: 'Selector does not reference a valid DOM node.',
};

jest.spyOn(braintreeScriptLoader, 'loadHostedFields').mockRejectedValue(
braintreeError,
);
jest.spyOn(
paymentIntegrationService.getState(),
'isPaymentMethodInitialized',
).mockReturnValue(true);

try {
await braintreeCreditCardPaymentStrategy.initialize({
methodId: paymentMethod.id,
braintree: initializeOptions,
});
} catch (error: Error | any) {
expect(error.message).toEqual(braintreeError.message);
}
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class BraintreeCreditCardPaymentStrategy implements PaymentStrate
await this.initializeBraintreeFastlaneOrThrow(methodId);
}
} catch (error) {
return this.handleError(error);
return this.handleError(error, methodId);
}
}

Expand Down Expand Up @@ -135,8 +135,20 @@ export default class BraintreeCreditCardPaymentStrategy implements PaymentStrate
return Promise.resolve();
}

private handleError(error: unknown): never {
private handleError(error: unknown, methodId?: string): void {
if (isBraintreeError(error)) {
const isPaymentMethodInitialized = !!(
methodId &&
this.paymentIntegrationService.getState().isPaymentMethodInitialized({ methodId })
);

if (
!isPaymentMethodInitialized &&
error.code === 'HOSTED_FIELDS_INVALID_FIELD_SELECTOR'
) {
return;
}

throw new PaymentMethodFailedError(error.message);
}

Expand Down