Skip to content

Commit 03ef141

Browse files
Klarna fix - Handling SDK action as standalone component (#3064)
* handling action for klarna standalone component * testing that updateWithAction is called * improved tests
1 parent 0b5ebb7 commit 03ef141

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

.changeset/fresh-terms-love.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@adyen/adyen-web': patch
3+
---
4+
5+
Klarna - Fix issue where Klarna as standalone component didn't initialize the Klarna Widget SDK accordingly

packages/lib/src/components/Klarna/KlarnaPayments.test.tsx

+67-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { render, screen, waitFor } from '@testing-library/preact';
22
import KlarnaPayments from './KlarnaPayments';
33
import Dropin from '../Dropin';
4+
import { PaymentAction } from '../../types/global-types';
45

56
describe('KlarnaPayments', () => {
6-
const coreProps = { name: 'Klarna', i18n: global.i18n, loadingContext: 'test', modules: { resources: global.resources } };
7+
const coreProps = {
8+
name: 'Klarna',
9+
i18n: global.i18n,
10+
loadingContext: 'test',
11+
modules: { resources: global.resources, analytics: global.analytics }
12+
};
713
const renderKlarna = props => {
814
const KlarnaPaymentsEle = new KlarnaPayments(global.core, {
915
...coreProps,
@@ -53,4 +59,64 @@ describe('KlarnaPayments', () => {
5359

5460
expect(onAdditionalDetailsMock).toHaveBeenCalled();
5561
});
62+
63+
describe('when handling action', () => {
64+
test('should use updateWithAction when action type is "sdk"', async () => {
65+
const widgetAction: PaymentAction = {
66+
paymentData: 'Ab02b4c0!...',
67+
paymentMethodType: 'klarna_paynow',
68+
type: 'sdk',
69+
sdkData: {
70+
client_token: 'eyJhbGciOiJ...',
71+
payment_method_category: 'pay_over_time'
72+
}
73+
};
74+
75+
const klarna = new KlarnaPayments(global.core, {
76+
...coreProps,
77+
type: 'klarna_paynow',
78+
onSubmit(state, component, actions) {
79+
actions.resolve({
80+
resultCode: 'Pending',
81+
action: widgetAction
82+
});
83+
}
84+
});
85+
const spy = jest.spyOn(klarna, 'updateWithAction');
86+
87+
render(klarna.render());
88+
klarna.submit();
89+
90+
await waitFor(() => expect(spy).toHaveBeenCalledWith(widgetAction), { interval: 100 });
91+
});
92+
93+
test('should NOT use updateWithAction when action type is NOT "sdk"', done => {
94+
const action: PaymentAction = {
95+
paymentMethodType: 'klarna_paynow',
96+
type: 'redirect',
97+
url: 'https://checkoutshopper-test.adyen.com/checkoutshopper/checkoutPaymentRedirect?redirectData=X3XtfGC9%21H4s...',
98+
method: 'GET'
99+
};
100+
101+
const klarna = new KlarnaPayments(global.core, {
102+
...coreProps,
103+
type: 'klarna_paynow',
104+
onSubmit(state, component, actions) {
105+
actions.resolve({
106+
resultCode: 'Pending',
107+
action
108+
});
109+
}
110+
});
111+
const spy = jest.spyOn(klarna, 'updateWithAction');
112+
113+
render(klarna.render());
114+
klarna.submit();
115+
116+
setTimeout(() => {
117+
expect(spy).not.toHaveBeenCalled();
118+
done();
119+
}, 500);
120+
});
121+
});
56122
});

packages/lib/src/components/Klarna/KlarnaPayments.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ class KlarnaPayments extends UIElement<KlarnaConfiguration> {
4242
return <PayButton amount={this.props.amount} onClick={this.submit} {...props} />;
4343
};
4444

45+
public override handleAction(action: KlarnaAction, props = {}): UIElement | null {
46+
if (action.type === 'sdk') {
47+
this.updateWithAction(action);
48+
return;
49+
}
50+
return super.handleAction(action, props);
51+
}
52+
4553
updateWithAction(action: KlarnaAction): void {
4654
if (action.paymentMethodType !== this.type) throw new Error('Invalid Action');
4755
this.componentRef.setAction(action);

0 commit comments

Comments
 (0)