Skip to content

Commit

Permalink
Refactored unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Nov 29, 2024
1 parent 39e5985 commit baa4961
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
9 changes: 3 additions & 6 deletions packages/lib/src/components/Klarna/KlarnaPayments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ describe('KlarnaPayments', () => {
expect(screen.queryByRole('button', { name: 'Continue to Pay By Bank' })).toBeFalsy();
});

test('should call setStatus if elementRef is a drop-in', async () => {
test('should call setStatus if elementRef is a drop-in', () => {
const KlarnaPaymentsEle = new KlarnaPayments(global.core, {
...coreProps,
...{ paymentData: '', paymentMethodType: '', sdkData: undefined, useKlarnaWidget: false, showPayButton: false }
});
KlarnaPaymentsEle.elementRef = new Dropin(global.core);
render(KlarnaPaymentsEle.render());
const spy = jest.spyOn(KlarnaPaymentsEle.elementRef, 'setStatus');
// @ts-ignore to test
await waitFor(() => KlarnaPaymentsEle.componentRef);
// @ts-ignore to test
KlarnaPaymentsEle.componentRef.props.onLoaded();
KlarnaPaymentsEle.onLoaded();
expect(spy).toHaveBeenCalled();
});

Expand All @@ -55,7 +52,7 @@ describe('KlarnaPayments', () => {
// @ts-ignore to test
await waitFor(() => KlarnaPaymentsEle.componentRef);
// @ts-ignore to test
KlarnaPaymentsEle.componentRef.props.onComplete();
KlarnaPaymentsEle.componentRef.onComplete();
expect(onAdditionalDetailsMock).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function KlarnaContainer(props) {

klarnaContainerRef.current.setAction = setAction;
klarnaContainerRef.current.setStatus = setStatus;
klarnaContainerRef.current.props = props; // Needed for unit tests
klarnaContainerRef.current.onComplete = props.onComplete; // Needed for unit tests

if (action.sdkData) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/components/Klarna/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ export interface KlarnaAction {
export interface KlarnaContainerRef extends ComponentMethodsRef {
initWidget?: () => void;
setAction?(action: KlarnaAction): void;
props?: any;
onComplete?: (state: any) => void;
}

0 comments on commit baa4961

Please sign in to comment.