From aa8f67b90e5fd0af5233697e4a62881db0ae7cc1 Mon Sep 17 00:00:00 2001 From: "a.kornienko" Date: Thu, 29 Feb 2024 10:30:06 +0300 Subject: [PATCH] test(PAYMENTS-18172): add tests --- .../apple-pay/apple-pay-error.guard.spec.ts | 14 +++ .../apple-pay/apple-pay-error.handler.spec.ts | 19 ++-- .../apple-pay/apple-pay.component.spec.ts | 88 +++++++++++++++++++ 3 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 src/core/guards/apple-pay/apple-pay-error.guard.spec.ts create mode 100644 src/features/headless-checkout/web-components/apple-pay/apple-pay.component.spec.ts diff --git a/src/core/guards/apple-pay/apple-pay-error.guard.spec.ts b/src/core/guards/apple-pay/apple-pay-error.guard.spec.ts new file mode 100644 index 0000000..e41b3da --- /dev/null +++ b/src/core/guards/apple-pay/apple-pay-error.guard.spec.ts @@ -0,0 +1,14 @@ +import 'reflect-metadata'; +import { isApplePayErrorEventMessage } from './apple-pay-error.guard'; +import { EventName } from '../../event-name.enum'; + +describe('Event message type guard', () => { + it('Should return true', () => { + expect( + isApplePayErrorEventMessage({ name: EventName.applePayError }) + ).toBeTruthy(); + }); + it('Should return false', () => { + expect(isApplePayErrorEventMessage({})).toBeFalsy(); + }); +}); diff --git a/src/features/headless-checkout/post-messages-handlers/apple-pay/apple-pay-error.handler.spec.ts b/src/features/headless-checkout/post-messages-handlers/apple-pay/apple-pay-error.handler.spec.ts index 94cefe4..366d0b4 100644 --- a/src/features/headless-checkout/post-messages-handlers/apple-pay/apple-pay-error.handler.spec.ts +++ b/src/features/headless-checkout/post-messages-handlers/apple-pay/apple-pay-error.handler.spec.ts @@ -1,29 +1,26 @@ import { applePayErrorHandler } from './apple-pay-error.handler'; import { Message } from '../../../../core/message.interface'; import { EventName } from '../../../../core/event-name.enum'; -import { getUserBalanceValueHandler } from '../get-user-balance-value.handler'; -const mockData = { - fieldName: 'fieldName', -}; - -const mockMessage: Message<{ fieldName: string } | null | undefined> = { - name: EventName.finishLoadComponent, +const mockMessage: Message<{ error: string } | null | undefined> = { + name: EventName.applePayError, data: { - fieldName: 'fieldName', + error: 'error', }, }; -describe('finishLoadComponentHandler', () => { +describe('applePayErrorHandler', () => { it('Should handle data', () => { expect(applePayErrorHandler(mockMessage)).toEqual({ isHandled: true, - value: null, + value: { + error: 'error', + }, }); }); it('Should return null', () => { expect( - getUserBalanceValueHandler({ name: EventName.getSavedMethods }) + applePayErrorHandler({ name: EventName.getSavedMethods }) ).toBeNull(); }); }); diff --git a/src/features/headless-checkout/web-components/apple-pay/apple-pay.component.spec.ts b/src/features/headless-checkout/web-components/apple-pay/apple-pay.component.spec.ts new file mode 100644 index 0000000..7cae836 --- /dev/null +++ b/src/features/headless-checkout/web-components/apple-pay/apple-pay.component.spec.ts @@ -0,0 +1,88 @@ +import { container } from 'tsyringe'; +import { WebComponentTagName } from '../../../../core/web-components/web-component-tag-name.enum'; +import { noopStub } from '../../../../tests/stubs/noop.stub'; +import { FormSpy } from '../../../../core/spy/form-spy/form-spy'; +import { HeadlessCheckout } from '../../headless-checkout'; +import { ApplePayComponent } from './apple-pay.component'; +import { EventName } from '../../../../core/event-name.enum'; + +function createComponent(): HTMLElement { + const element = document.createElement(WebComponentTagName.ApplePayComponent); + (document.getElementById('container')! as HTMLElement).appendChild(element); + return element; +} + +describe('ApplePayComponent', () => { + let headlessCheckout: HeadlessCheckout; + let formSpy: FormSpy; + + window.customElements.define( + WebComponentTagName.ApplePayComponent, + ApplePayComponent + ); + + beforeEach(() => { + document.body.innerHTML = '
'; + + headlessCheckout = { + form: { + onFieldsStatusChange: noopStub, + }, + events: { + onCoreEvent: noopStub, + }, + } as unknown as HeadlessCheckout; + + formSpy = { + listenFormInit: noopStub, + get formWasInit() { + return; + }, + } as unknown as FormSpy; + + container.clearInstances(); + + container + .register(FormSpy, { + useValue: formSpy, + }) + .register(HeadlessCheckout, { + useValue: headlessCheckout, + }); + }); + + afterEach(() => { + document.body.innerHTML = ''; + }); + + it('Web component should be rendered', () => { + spyOnProperty(formSpy, 'formWasInit').and.returnValue(true); + + createComponent(); + + expect(document.querySelector('iframe')).toBeDefined(); + }); + + it('Should draw error', (done) => { + spyOnProperty(formSpy, 'formWasInit').and.returnValue(true); + // eslint-disable-next-line prefer-const + let element: HTMLElement; + spyOn(headlessCheckout.events, 'onCoreEvent').and.callFake((...args) => { + const eventName = args[0]; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const callback: (value?: unknown) => void = args[2]; + setTimeout(() => { + if (eventName === EventName.applePayError) { + callback({ error: 'paymentError' }); + expect(element.querySelector('.apple-pay-error')).not.toBeNull(); + done(); + } + }); + return noopStub; + }); + + element = createComponent(); + expect(document.querySelector('iframe')).toBeDefined(); + }); +});