diff --git a/src/core/status/form-status.enum.ts b/src/core/status/form-status.enum.ts new file mode 100644 index 0000000..5d3c048 --- /dev/null +++ b/src/core/status/form-status.enum.ts @@ -0,0 +1,5 @@ +export enum FormStatus { + undefined = 'undefined', + pending = 'pending', + active = 'active', +} 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 3787733..c22ba90 100644 --- a/src/features/headless-checkout/headless-checkout.ts +++ b/src/features/headless-checkout/headless-checkout.ts @@ -27,6 +27,7 @@ import { getPaymentStatusHandler } from './post-messages-handlers/get-payment-st import { headlessCheckoutAppUrl } from './environment'; import { FinanceDetails } from '../../core/finance-details/finance-details.interface'; import { getFinanceDetailsHandler } from './post-messages-handlers/get-finance-details.handler'; +import { FormStatus } from '../../core/status/form-status.enum'; @singleton() export class HeadlessCheckout { @@ -64,6 +65,8 @@ export class HeadlessCheckout { * @returns {Form} form details */ init: async (configuration: FormConfiguration): Promise
=> { + this.formStatus = FormStatus.pending; + const msg: Message = { name: EventName.initForm, data: { @@ -77,6 +80,7 @@ export class HeadlessCheckout { this.formSpy.formFields = (args as { fields: Field[] }).fields; } this.formSpy.formWasInit = true; + this.formStatus = FormStatus.active; }) ) as Promise; }, @@ -92,8 +96,23 @@ export class HeadlessCheckout { } ); }, + + getStatus: (): FormStatus => { + if (this.formSpy.formWasInit) { + return FormStatus.active; + } + + return this.formStatus === FormStatus.pending + ? FormStatus.pending + : FormStatus.undefined; + }, + + activate: (): void => { + this.formStatus = FormStatus.active; + }, }; + private formStatus: FormStatus = FormStatus.undefined; private isWebView?: boolean; private isSandbox?: boolean; private coreIframe!: HTMLIFrameElement;