Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Nov 28, 2024
1 parent f7fd597 commit 39e5985
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/lib/src/components/Klarna/KlarnaPayments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class KlarnaPayments extends UIElement<KlarnConfiguration> {

protected onPaymentMethodActive() {
// Reinit the KlarnaSDK widget
this.componentRef.initWidget();
this.componentRef?.initWidget();
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -34,14 +34,15 @@ describe('KlarnaWidget', () => {
Pay with Klarna
</button>
);
const props = {
const props: KlarnaWidgetProps = {
onLoaded,
onComplete,
onError,
paymentData,
paymentMethodType,
sdkData,
payButton
payButton,
containerRef: null
};

beforeAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/components/Klarna/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ export interface KlarnaAction {
export interface KlarnaContainerRef extends ComponentMethodsRef {
initWidget?: () => void;
setAction?(action: KlarnaAction): void;
props?: any;
}

0 comments on commit 39e5985

Please sign in to comment.