From 1e16475970a13dbfb40045fdf6a3bc85cdd12425 Mon Sep 17 00:00:00 2001 From: bc-nick Date: Wed, 8 Jan 2025 13:21:10 +0100 Subject: [PATCH] feat(payment): PAYPAL-4995 added check for teardown call in deinitialize method --- .../strategies/braintree/braintree-hosted-form.spec.ts | 10 ++++++++++ .../strategies/braintree/braintree-hosted-form.ts | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/core/src/payment/strategies/braintree/braintree-hosted-form.spec.ts b/packages/core/src/payment/strategies/braintree/braintree-hosted-form.spec.ts index b723ba083c..f50bcc2083 100644 --- a/packages/core/src/payment/strategies/braintree/braintree-hosted-form.spec.ts +++ b/packages/core/src/payment/strategies/braintree/braintree-hosted-form.spec.ts @@ -206,6 +206,16 @@ describe('BraintreeHostedForm', () => { expect(cardFields.teardown).toHaveBeenCalled(); }); + + it('do not call teardown if fields are not initialized', async () => { + await subject.initialize({ + ...formOptions, + fields: {}, + }); + await subject.deinitialize(); + + expect(cardFields.teardown).not.toHaveBeenCalled(); + }); }); describe('#tokenize', () => { diff --git a/packages/core/src/payment/strategies/braintree/braintree-hosted-form.ts b/packages/core/src/payment/strategies/braintree/braintree-hosted-form.ts index b89fb3e501..5a58ca8d22 100644 --- a/packages/core/src/payment/strategies/braintree/braintree-hosted-form.ts +++ b/packages/core/src/payment/strategies/braintree/braintree-hosted-form.ts @@ -75,9 +75,11 @@ export default class BraintreeHostedForm { } async deinitialize(): Promise { - this._isInitializedHostedForm = false; + if (this._isInitializedHostedForm) { + this._isInitializedHostedForm = false; - await this._cardFields?.teardown(); + await this._cardFields?.teardown(); + } } validate() {