From edf95d7cc7bf466b827354710903450306fbd2d4 Mon Sep 17 00:00:00 2001 From: Konstantin Kamenskiy Date: Mon, 14 Aug 2023 15:40:43 +0300 Subject: [PATCH] feat(PAYMENTS-15270): form - activate & getStatus + tests --- .../headless-checkout.spec.ts | 21 +++++++++++++++++++ .../headless-checkout/headless-checkout.ts | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/features/headless-checkout/headless-checkout.spec.ts b/src/features/headless-checkout/headless-checkout.spec.ts index 69d8bb4..6ef246e 100644 --- a/src/features/headless-checkout/headless-checkout.spec.ts +++ b/src/features/headless-checkout/headless-checkout.spec.ts @@ -6,6 +6,7 @@ import { Message } from '../../core/message.interface'; import { Handler } from '../../core/post-messages-client/handler.type'; import { LocalizeService } from '../../core/i18n/localize.service'; import { getFinanceDetailsHandler } from './post-messages-handlers/get-finance-details.handler'; +import { FormStatus } from '../../core/status/form-status.enum'; const mockMessage: Message = { name: EventName.initPayment, @@ -189,4 +190,24 @@ describe('HeadlessCheckout', () => { await headlessCheckout.getUserBalance(); expect(spy).toHaveBeenCalled(); }); + + it('Should have correct form status', async () => { + expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.undefined); + + spyOn(postMessagesClient, 'send').and.callFake(async (msg, callback) => { + await Promise.resolve().then(() => callback(msg)); + return Promise.resolve(undefined); + }); + + const formInitPromise = headlessCheckout.form.init({ + paymentMethodId: 1380, + returnUrl: '', + }); + + expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.pending); + + await formInitPromise.then(); + + expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.active); + }); }); diff --git a/src/features/headless-checkout/headless-checkout.ts b/src/features/headless-checkout/headless-checkout.ts index 92a1f9f..c22ba90 100644 --- a/src/features/headless-checkout/headless-checkout.ts +++ b/src/features/headless-checkout/headless-checkout.ts @@ -98,7 +98,9 @@ export class HeadlessCheckout { }, getStatus: (): FormStatus => { - if (this.formSpy.formWasInit) return FormStatus.active; + if (this.formSpy.formWasInit) { + return FormStatus.active; + } return this.formStatus === FormStatus.pending ? FormStatus.pending