From 39e5985c0001930381229574b13a78bdd15500a3 Mon Sep 17 00:00:00 2001 From: nicholas Date: Thu, 28 Nov 2024 18:36:27 +0100 Subject: [PATCH] Fixing unit tests --- packages/lib/src/components/Klarna/KlarnaPayments.tsx | 2 +- .../Klarna/components/KlarnaContainer/KlarnaContainer.tsx | 1 + .../Klarna/components/KlarnaWidget/KlarnaWidget.test.tsx | 7 ++++--- .../Klarna/components/KlarnaWidget/KlarnaWidget.tsx | 6 ++++-- packages/lib/src/components/Klarna/types.ts | 1 + 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/lib/src/components/Klarna/KlarnaPayments.tsx b/packages/lib/src/components/Klarna/KlarnaPayments.tsx index 1782df1e0..83fec2d1e 100644 --- a/packages/lib/src/components/Klarna/KlarnaPayments.tsx +++ b/packages/lib/src/components/Klarna/KlarnaPayments.tsx @@ -53,7 +53,7 @@ class KlarnaPayments extends UIElement { protected onPaymentMethodActive() { // Reinit the KlarnaSDK widget - this.componentRef.initWidget(); + this.componentRef?.initWidget(); } render() { diff --git a/packages/lib/src/components/Klarna/components/KlarnaContainer/KlarnaContainer.tsx b/packages/lib/src/components/Klarna/components/KlarnaContainer/KlarnaContainer.tsx index ee3694dba..378e6888d 100644 --- a/packages/lib/src/components/Klarna/components/KlarnaContainer/KlarnaContainer.tsx +++ b/packages/lib/src/components/Klarna/components/KlarnaContainer/KlarnaContainer.tsx @@ -19,6 +19,7 @@ export function KlarnaContainer(props) { klarnaContainerRef.current.setAction = setAction; klarnaContainerRef.current.setStatus = setStatus; + klarnaContainerRef.current.props = props; // Needed for unit tests if (action.sdkData) { return ( diff --git a/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.test.tsx b/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.test.tsx index 52764ec3d..fad22de7e 100644 --- a/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.test.tsx +++ b/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.test.tsx @@ -3,7 +3,7 @@ import { fireEvent, render, screen } from '@testing-library/preact'; import { KlarnaWidget } from './KlarnaWidget'; import Script from '../../../../utils/Script'; import { KLARNA_WIDGET_URL } from '../../constants'; -import { KlarnaWidgetAuthorizeResponse } from '../../types'; +import { KlarnaWidgetAuthorizeResponse, KlarnaWidgetProps } from '../../types'; jest.mock('../../../../utils/Script', () => { return jest.fn().mockImplementation(() => { @@ -34,14 +34,15 @@ describe('KlarnaWidget', () => { Pay with Klarna ); - const props = { + const props: KlarnaWidgetProps = { onLoaded, onComplete, onError, paymentData, paymentMethodType, sdkData, - payButton + payButton, + containerRef: null }; beforeAll(() => { diff --git a/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.tsx b/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.tsx index 77909b0b3..0d2fc60df 100644 --- a/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.tsx +++ b/packages/lib/src/components/Klarna/components/KlarnaWidget/KlarnaWidget.tsx @@ -44,7 +44,9 @@ export function KlarnaWidget({ sdkData, paymentMethodType, payButton, ...props } ); }; - (props.containerRef as KlarnaContainerRef).initWidget = initializeKlarnaWidget; + if (props.containerRef) { + (props.containerRef as KlarnaContainerRef).initWidget = initializeKlarnaWidget; + } const authorizeKlarna = () => { setStatus('loading'); @@ -101,7 +103,7 @@ export function KlarnaWidget({ sdkData, paymentMethodType, payButton, ...props } console.log('\n### KlarnaWidget:::: useEffect sdkData.payment_method_category=', sdkData.payment_method_category); const script = new Script(KLARNA_WIDGET_URL); - void script.load().then(() => { + void script.load()?.then(() => { console.log('### KlarnaWidget:::: useEffect script: LOADED '); if (initOnLoad) { console.log('### KlarnaWidget:::: manually init widget'); diff --git a/packages/lib/src/components/Klarna/types.ts b/packages/lib/src/components/Klarna/types.ts index 1c113cdcd..13e2bbbcf 100644 --- a/packages/lib/src/components/Klarna/types.ts +++ b/packages/lib/src/components/Klarna/types.ts @@ -64,4 +64,5 @@ export interface KlarnaAction { export interface KlarnaContainerRef extends ComponentMethodsRef { initWidget?: () => void; setAction?(action: KlarnaAction): void; + props?: any; }