From 8f78a1f76c46c9df472e70284c0de7951640e846 Mon Sep 17 00:00:00 2001 From: Matejk00 Date: Wed, 13 Sep 2023 10:08:23 +0200 Subject: [PATCH] Update unit tests coverage --- ...f-checkout-payment-wrapper.service.spec.ts | 742 +++++++++--------- .../connectors/opf-checkout.connector.spec.ts | 95 ++- .../core/facade/opf-checkout.service.spec.ts | 143 ++-- .../services/opf-endpoints.service.spec.ts | 102 ++- 4 files changed, 557 insertions(+), 525 deletions(-) diff --git a/integration-libs/opf/checkout/components/opf-checkout-payment-wrapper/opf-checkout-payment-wrapper.service.spec.ts b/integration-libs/opf/checkout/components/opf-checkout-payment-wrapper/opf-checkout-payment-wrapper.service.spec.ts index e874dd2860e..aa3d50747c1 100644 --- a/integration-libs/opf/checkout/components/opf-checkout-payment-wrapper/opf-checkout-payment-wrapper.service.spec.ts +++ b/integration-libs/opf/checkout/components/opf-checkout-payment-wrapper/opf-checkout-payment-wrapper.service.spec.ts @@ -1,367 +1,375 @@ -// import { fakeAsync, TestBed, tick } from '@angular/core/testing'; -// import { ActiveCartService } from '@spartacus/cart/base/core'; -// import { -// GlobalMessageService, -// RouterState, -// RoutingService, -// UserIdService, -// } from '@spartacus/core'; -// import { -// OpfOrderFacade, -// OpfOtpFacade, -// OpfService, -// } from '@spartacus/opf/base/root'; -// import { of, throwError } from 'rxjs'; -// import { OpfResourceLoaderService } from '../../core/services'; -// import { OpfCheckoutFacade } from '../../root/facade'; -// import { -// OpfPaymentMethodType, -// PaymentDynamicScriptResourceType, -// PaymentSessionData, -// } from '../../root/model'; -// import { OpfCheckoutPaymentWrapperService } from './opf-checkout-payment-wrapper.service'; - -// const mockUrl = 'https://sap.com'; - -// describe('OpfCheckoutPaymentWrapperService', () => { -// let service: OpfCheckoutPaymentWrapperService; -// let opfCheckoutFacadeMock: jasmine.SpyObj; -// let opfOtpFacadeMock: jasmine.SpyObj; -// let opfResourceLoaderServiceMock: jasmine.SpyObj; -// let userIdServiceMock: jasmine.SpyObj; -// let activeCartServiceMock: jasmine.SpyObj; -// let routingServiceMock: jasmine.SpyObj; -// let globalMessageServiceMock: jasmine.SpyObj; -// let opfOrderFacadeMock: jasmine.SpyObj; -// let opfServiceMock: jasmine.SpyObj; - -// beforeEach(() => { -// opfCheckoutFacadeMock = jasmine.createSpyObj('OpfCheckoutFacade', [ -// 'initiatePayment', -// ]); -// opfOtpFacadeMock = jasmine.createSpyObj('OpfOtpFacade', ['generateOtpKey']); -// opfResourceLoaderServiceMock = jasmine.createSpyObj( -// 'OpfResourceLoaderService', -// [ -// 'executeScriptFromHtml', -// 'clearAllProviderResources', -// 'loadProviderResources', -// ] -// ); -// userIdServiceMock = jasmine.createSpyObj('UserIdService', ['getUserId']); -// activeCartServiceMock = jasmine.createSpyObj('ActiveCartService', [ -// 'getActiveCartId', -// ]); -// routingServiceMock = jasmine.createSpyObj('RoutingService', [ -// 'getRouterState', -// 'go', -// 'getFullUrl', -// ]); -// globalMessageServiceMock = jasmine.createSpyObj('GlobalMessageService', [ -// 'add', -// ]); -// opfOrderFacadeMock = jasmine.createSpyObj('OpfOrderFacade', [ -// 'placeOpfOrder', -// ]); -// opfServiceMock = jasmine.createSpyObj('OpfService', [ -// 'updateOpfMetadataState', -// ]); - -// routingServiceMock.getRouterState.and.returnValue( -// of({ -// state: { -// semanticRoute: 'checkoutReviewOrder', -// }, -// } as RouterState) -// ); - -// TestBed.configureTestingModule({ -// providers: [ -// OpfCheckoutPaymentWrapperService, -// { provide: OpfCheckoutFacade, useValue: opfCheckoutFacadeMock }, -// { provide: OpfOtpFacade, useValue: opfOtpFacadeMock }, -// { -// provide: OpfResourceLoaderService, -// useValue: opfResourceLoaderServiceMock, -// }, -// { provide: UserIdService, useValue: userIdServiceMock }, -// { provide: ActiveCartService, useValue: activeCartServiceMock }, -// { provide: RoutingService, useValue: routingServiceMock }, -// { provide: GlobalMessageService, useValue: globalMessageServiceMock }, -// { provide: OpfOrderFacade, useValue: opfOrderFacadeMock }, -// { provide: OpfService, useValue: opfServiceMock }, -// ], -// }); - -// service = TestBed.inject(OpfCheckoutPaymentWrapperService); -// }); - -// it('should retrieve renderPaymentMethodEvent$', (done) => { -// const mockRenderPaymentMethodEvent = { isLoading: false, isError: false }; -// service['renderPaymentMethodEvent$'].next(mockRenderPaymentMethodEvent); - -// service.getRenderPaymentMethodEvent().subscribe((event) => { -// expect(event).toEqual(mockRenderPaymentMethodEvent); -// done(); -// }); -// }); - -// it('should initiate payment successfully and render payment gateway', (done) => { -// const mockPaymentOptionId = 123; -// const mockOtpKey = 'otpKey'; -// const mockUserId = 'userId'; -// const mockCartId = 'cartId'; -// const mockPaymentSessionData: PaymentSessionData = { -// dynamicScript: { -// html: '', -// jsUrls: [ -// { url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }, -// ], -// cssUrls: [ -// { url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }, -// ], -// }, -// }; - -// opfCheckoutFacadeMock.initiatePayment.and.returnValue( -// of(mockPaymentSessionData) -// ); -// opfOtpFacadeMock.generateOtpKey.and.returnValue( -// of({ accessCode: mockOtpKey }) -// ); -// userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); -// activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); -// routingServiceMock.getRouterState.and.returnValue( -// of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) -// ); -// routingServiceMock.getFullUrl.and.returnValue(mockUrl); -// opfServiceMock.updateOpfMetadataState.and.stub(); -// opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( -// Promise.resolve() -// ); -// spyOn(service, 'renderPaymentGateway').and.callThrough(); - -// service.initiatePayment(mockPaymentOptionId).subscribe(() => { -// expect(opfCheckoutFacadeMock.initiatePayment).toHaveBeenCalledWith({ -// otpKey: mockOtpKey, -// config: { -// configurationId: mockPaymentOptionId.toString(), -// cartId: mockCartId, -// resultURL: mockUrl, -// cancelURL: mockUrl, -// }, -// }); - -// expect( -// opfResourceLoaderServiceMock.loadProviderResources -// ).toHaveBeenCalledWith( -// [{ url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }], -// [{ url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }] -// ); - -// expect(service.renderPaymentGateway).toHaveBeenCalledWith({ -// dynamicScript: { -// html: '', -// jsUrls: [ -// { -// url: 'script.js', -// type: PaymentDynamicScriptResourceType.SCRIPT, -// }, -// ], -// cssUrls: [ -// { -// url: 'styles.css', -// type: PaymentDynamicScriptResourceType.STYLES, -// }, -// ], -// }, -// }); - -// done(); -// }); -// }); - -// it('should handle when payment initiation fails with 409 error', (done) => { -// const mockPaymentOptionId = 123; -// const mockOtpKey = 'otpKey'; -// const mockUserId = 'userId'; -// const mockCartId = 'cartId'; - -// opfCheckoutFacadeMock.initiatePayment.and.returnValue( -// throwError({ status: 409 }) -// ); - -// opfOrderFacadeMock.placeOpfOrder.and.returnValue(of({})); -// opfOtpFacadeMock.generateOtpKey.and.returnValue( -// of({ accessCode: mockOtpKey }) -// ); -// userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); -// activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); -// routingServiceMock.getRouterState.and.returnValue( -// of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) -// ); -// routingServiceMock.getFullUrl.and.returnValue(mockUrl); -// opfServiceMock.updateOpfMetadataState.and.stub(); -// opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( -// Promise.resolve() -// ); -// spyOn(service, 'renderPaymentGateway').and.callThrough(); - -// service.initiatePayment(mockPaymentOptionId).subscribe( -// () => {}, -// (error) => { -// expect(error).toBe('Payment already done'); -// done(); -// } -// ); -// }); - -// it('should handle when payment initiation fails with 500 error', (done) => { -// const mockPaymentOptionId = 123; -// const mockOtpKey = 'otpKey'; -// const mockUserId = 'userId'; -// const mockCartId = 'cartId'; - -// opfCheckoutFacadeMock.initiatePayment.and.returnValue( -// throwError({ status: 500 }) -// ); - -// opfOtpFacadeMock.generateOtpKey.and.returnValue( -// of({ accessCode: mockOtpKey }) -// ); -// userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); -// activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); -// routingServiceMock.getRouterState.and.returnValue( -// of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) -// ); -// routingServiceMock.getFullUrl.and.returnValue(mockUrl); -// opfServiceMock.updateOpfMetadataState.and.stub(); -// opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( -// Promise.resolve() -// ); -// spyOn(service, 'renderPaymentGateway').and.callThrough(); - -// service.initiatePayment(mockPaymentOptionId).subscribe( -// () => {}, -// (error) => { -// expect(error).toBe('Payment failed'); -// expect(globalMessageServiceMock.add).toHaveBeenCalled(); -// done(); -// } -// ); -// }); - -// it('should reload payment mode', () => { -// const mockPaymentOptionId = 123; -// spyOn(service, 'initiatePayment').and.callThrough(); -// userIdServiceMock.getUserId.and.returnValue(of()); -// activeCartServiceMock.getActiveCartId.and.returnValue(of()); -// service['lastPaymentOptionId'] = mockPaymentOptionId; - -// service.reloadPaymentMode(); - -// expect(service.initiatePayment).toHaveBeenCalledWith(mockPaymentOptionId); -// }); - -// it('should render payment gateway with destination URL', () => { -// const mockPaymentSessionData: PaymentSessionData = { -// destination: { url: mockUrl }, -// }; - -// service['renderPaymentGateway'](mockPaymentSessionData); - -// expect(service['renderPaymentMethodEvent$'].value).toEqual({ -// isLoading: false, -// isError: false, -// renderType: OpfPaymentMethodType.DESTINATION, -// data: mockUrl, -// }); -// }); - -// it('should render payment gateway with dynamic script', (done) => { -// const mockPaymentSessionData: PaymentSessionData = { -// dynamicScript: { -// html: '', -// jsUrls: [ -// { url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }, -// ], -// cssUrls: [ -// { url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }, -// ], -// }, -// }; - -// opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( -// Promise.resolve() -// ); - -// service['renderPaymentGateway'](mockPaymentSessionData); - -// expect( -// opfResourceLoaderServiceMock.loadProviderResources -// ).toHaveBeenCalledWith( -// [{ url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }], -// [{ url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }] -// ); - -// setTimeout(() => { -// expect(service['renderPaymentMethodEvent$'].value).toEqual({ -// isLoading: false, -// isError: false, -// renderType: OpfPaymentMethodType.DYNAMIC_SCRIPT, -// data: '', -// }); -// done(); -// }); -// }); - -// it('should handle place order success', () => { -// service['onPlaceOrderSuccess'](); -// expect(service['routingService'].go).toHaveBeenCalledWith({ -// cxRoute: 'orderConfirmation', -// }); -// }); - -// it('should set payment initiation config', () => { -// const mockOtpKey = 'otpKey'; -// const mockPaymentOptionId = 123; -// const mockActiveCartId = 'cartId'; -// service['activeCartId'] = mockActiveCartId; -// routingServiceMock.getFullUrl.and.returnValue(mockUrl); - -// activeCartServiceMock.getActiveCartId.and.returnValue(of(mockActiveCartId)); - -// const config = service['setPaymentInitiationConfig']( -// mockOtpKey, -// mockPaymentOptionId -// ); - -// expect(config).toEqual({ -// otpKey: mockOtpKey, -// config: { -// configurationId: mockPaymentOptionId.toString(), -// cartId: mockActiveCartId, -// resultURL: mockUrl, -// cancelURL: mockUrl, -// }, -// }); -// }); - -// it('should execute script from HTML', fakeAsync(() => { -// const mockHtml = ''; - -// routingServiceMock.getRouterState.and.returnValue( -// of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) -// ); -// opfResourceLoaderServiceMock.executeScriptFromHtml.and.stub(); - -// service['executeScriptFromHtml'](mockHtml); - -// expect(routingServiceMock.getRouterState).toHaveBeenCalled(); - -// tick(500); -// expect( -// opfResourceLoaderServiceMock.executeScriptFromHtml -// ).toHaveBeenCalledWith(mockHtml); -// })); -// }); +import { fakeAsync, TestBed, tick } from '@angular/core/testing'; +import { ActiveCartService } from '@spartacus/cart/base/core'; +import { + GlobalMessageService, + RouterState, + RoutingService, + UserIdService, +} from '@spartacus/core'; +import { + OpfOrderFacade, + OpfOtpFacade, + OpfService, +} from '@spartacus/opf/base/root'; +import { of, throwError } from 'rxjs'; +import { OpfResourceLoaderService } from '../../core/services'; +import { OpfCheckoutFacade } from '../../root/facade'; +import { + OpfPaymentMethodType, + PaymentDynamicScriptResourceType, + PaymentSessionData, +} from '../../root/model'; +import { OpfCheckoutPaymentWrapperService } from './opf-checkout-payment-wrapper.service'; + +const mockUrl = 'https://sap.com'; + +describe('OpfCheckoutPaymentWrapperService', () => { + let service: OpfCheckoutPaymentWrapperService; + let opfCheckoutFacadeMock: jasmine.SpyObj; + let opfOtpFacadeMock: jasmine.SpyObj; + let opfResourceLoaderServiceMock: jasmine.SpyObj; + let userIdServiceMock: jasmine.SpyObj; + let activeCartServiceMock: jasmine.SpyObj; + let routingServiceMock: jasmine.SpyObj; + let globalMessageServiceMock: jasmine.SpyObj; + let opfOrderFacadeMock: jasmine.SpyObj; + let opfServiceMock: jasmine.SpyObj; + + beforeEach(() => { + opfCheckoutFacadeMock = jasmine.createSpyObj('OpfCheckoutFacade', [ + 'initiatePayment', + ]); + opfOtpFacadeMock = jasmine.createSpyObj('OpfOtpFacade', ['generateOtpKey']); + opfResourceLoaderServiceMock = jasmine.createSpyObj( + 'OpfResourceLoaderService', + [ + 'executeScriptFromHtml', + 'clearAllProviderResources', + 'loadProviderResources', + ] + ); + userIdServiceMock = jasmine.createSpyObj('UserIdService', ['getUserId']); + activeCartServiceMock = jasmine.createSpyObj('ActiveCartService', [ + 'getActiveCartId', + ]); + routingServiceMock = jasmine.createSpyObj('RoutingService', [ + 'getRouterState', + 'go', + 'getFullUrl', + ]); + globalMessageServiceMock = jasmine.createSpyObj('GlobalMessageService', [ + 'add', + ]); + opfOrderFacadeMock = jasmine.createSpyObj('OpfOrderFacade', [ + 'placeOpfOrder', + ]); + opfServiceMock = jasmine.createSpyObj('OpfService', [ + 'updateOpfMetadataState', + ]); + + routingServiceMock.getRouterState.and.returnValue( + of({ + state: { + semanticRoute: 'checkoutReviewOrder', + }, + } as RouterState) + ); + + TestBed.configureTestingModule({ + providers: [ + OpfCheckoutPaymentWrapperService, + { provide: OpfCheckoutFacade, useValue: opfCheckoutFacadeMock }, + { provide: OpfOtpFacade, useValue: opfOtpFacadeMock }, + { + provide: OpfResourceLoaderService, + useValue: opfResourceLoaderServiceMock, + }, + { provide: UserIdService, useValue: userIdServiceMock }, + { provide: ActiveCartService, useValue: activeCartServiceMock }, + { provide: RoutingService, useValue: routingServiceMock }, + { provide: GlobalMessageService, useValue: globalMessageServiceMock }, + { provide: OpfOrderFacade, useValue: opfOrderFacadeMock }, + { provide: OpfService, useValue: opfServiceMock }, + ], + }); + + service = TestBed.inject(OpfCheckoutPaymentWrapperService); + }); + + it('should retrieve renderPaymentMethodEvent$', (done) => { + const mockRenderPaymentMethodEvent = { isLoading: false, isError: false }; + service['renderPaymentMethodEvent$'].next(mockRenderPaymentMethodEvent); + + service.getRenderPaymentMethodEvent().subscribe((event) => { + expect(event).toEqual(mockRenderPaymentMethodEvent); + done(); + }); + }); + + it('should initiate payment successfully and render payment gateway', (done) => { + const mockPaymentOptionId = 123; + const mockOtpKey = 'otpKey'; + const mockUserId = 'userId'; + const mockCartId = 'cartId'; + const mockPaymentSessionData: PaymentSessionData = { + dynamicScript: { + html: '', + jsUrls: [ + { url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }, + ], + cssUrls: [ + { url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }, + ], + }, + }; + + opfCheckoutFacadeMock.initiatePayment.and.returnValue( + of(mockPaymentSessionData) + ); + opfOtpFacadeMock.generateOtpKey.and.returnValue( + of({ accessCode: mockOtpKey }) + ); + userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); + activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); + routingServiceMock.getRouterState.and.returnValue( + of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) + ); + routingServiceMock.getFullUrl.and.returnValue(mockUrl); + opfServiceMock.updateOpfMetadataState.and.stub(); + opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( + Promise.resolve() + ); + spyOn(service, 'renderPaymentGateway').and.callThrough(); + + service.initiatePayment(mockPaymentOptionId).subscribe(() => { + expect(opfCheckoutFacadeMock.initiatePayment).toHaveBeenCalledWith({ + otpKey: mockOtpKey, + config: { + configurationId: mockPaymentOptionId.toString(), + cartId: mockCartId, + resultURL: mockUrl, + cancelURL: mockUrl, + }, + }); + + expect( + opfResourceLoaderServiceMock.loadProviderResources + ).toHaveBeenCalledWith( + [{ url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }], + [{ url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }] + ); + + expect(service.renderPaymentGateway).toHaveBeenCalledWith({ + dynamicScript: { + html: '', + jsUrls: [ + { + url: 'script.js', + type: PaymentDynamicScriptResourceType.SCRIPT, + }, + ], + cssUrls: [ + { + url: 'styles.css', + type: PaymentDynamicScriptResourceType.STYLES, + }, + ], + }, + }); + + done(); + }); + }); + + it('should handle when payment initiation fails with 409 error', (done) => { + const mockPaymentOptionId = 123; + const mockOtpKey = 'otpKey'; + const mockUserId = 'userId'; + const mockCartId = 'cartId'; + + opfCheckoutFacadeMock.initiatePayment.and.returnValue( + throwError({ status: 409 }) + ); + + opfOrderFacadeMock.placeOpfOrder.and.returnValue(of({})); + opfOtpFacadeMock.generateOtpKey.and.returnValue( + of({ accessCode: mockOtpKey }) + ); + userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); + activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); + routingServiceMock.getRouterState.and.returnValue( + of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) + ); + routingServiceMock.getFullUrl.and.returnValue(mockUrl); + opfServiceMock.updateOpfMetadataState.and.stub(); + opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( + Promise.resolve() + ); + spyOn(service, 'renderPaymentGateway').and.callThrough(); + + service.initiatePayment(mockPaymentOptionId).subscribe( + () => {}, + (error) => { + expect(error).toBe('Payment already done'); + done(); + } + ); + }); + + it('should handle when payment initiation fails with 500 error', (done) => { + const mockPaymentOptionId = 123; + const mockOtpKey = 'otpKey'; + const mockUserId = 'userId'; + const mockCartId = 'cartId'; + + opfCheckoutFacadeMock.initiatePayment.and.returnValue( + throwError({ status: 500 }) + ); + + opfOtpFacadeMock.generateOtpKey.and.returnValue( + of({ accessCode: mockOtpKey }) + ); + userIdServiceMock.getUserId.and.returnValue(of(mockUserId)); + activeCartServiceMock.getActiveCartId.and.returnValue(of(mockCartId)); + routingServiceMock.getRouterState.and.returnValue( + of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) + ); + routingServiceMock.getFullUrl.and.returnValue(mockUrl); + opfServiceMock.updateOpfMetadataState.and.stub(); + opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( + Promise.resolve() + ); + spyOn(service, 'renderPaymentGateway').and.callThrough(); + + service.initiatePayment(mockPaymentOptionId).subscribe( + () => {}, + (error) => { + expect(error).toBe('Payment failed'); + expect(globalMessageServiceMock.add).toHaveBeenCalled(); + done(); + } + ); + }); + + it('should reload payment mode', () => { + const mockPaymentOptionId = 123; + spyOn(service, 'initiatePayment').and.callThrough(); + userIdServiceMock.getUserId.and.returnValue(of()); + activeCartServiceMock.getActiveCartId.and.returnValue(of()); + service['lastPaymentOptionId'] = mockPaymentOptionId; + + service.reloadPaymentMode(); + + expect(service.initiatePayment).toHaveBeenCalledWith(mockPaymentOptionId); + }); + + it('should render payment gateway with destination URL', () => { + const mockPaymentSessionData: PaymentSessionData = { + destination: { url: mockUrl }, + }; + + service['renderPaymentGateway'](mockPaymentSessionData); + + expect(service['renderPaymentMethodEvent$'].value).toEqual({ + isLoading: false, + isError: false, + renderType: OpfPaymentMethodType.DESTINATION, + data: mockUrl, + }); + }); + + it('should render payment gateway with dynamic script', (done) => { + const mockPaymentSessionData: PaymentSessionData = { + dynamicScript: { + html: '', + jsUrls: [ + { url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }, + ], + cssUrls: [ + { url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }, + ], + }, + }; + + opfResourceLoaderServiceMock.loadProviderResources.and.returnValue( + Promise.resolve() + ); + + service['renderPaymentGateway'](mockPaymentSessionData); + + expect( + opfResourceLoaderServiceMock.loadProviderResources + ).toHaveBeenCalledWith( + [{ url: 'script.js', type: PaymentDynamicScriptResourceType.SCRIPT }], + [{ url: 'styles.css', type: PaymentDynamicScriptResourceType.STYLES }] + ); + + setTimeout(() => { + expect(service['renderPaymentMethodEvent$'].value).toEqual({ + isLoading: false, + isError: false, + renderType: OpfPaymentMethodType.DYNAMIC_SCRIPT, + data: '', + }); + done(); + }); + }); + + it('should handle place order success', () => { + service['onPlaceOrderSuccess'](); + expect(service['routingService'].go).toHaveBeenCalledWith({ + cxRoute: 'orderConfirmation', + }); + }); + + it('should set payment initiation config', () => { + const mockOtpKey = 'otpKey'; + const mockPaymentOptionId = 123; + const mockActiveCartId = 'cartId'; + service['activeCartId'] = mockActiveCartId; + routingServiceMock.getFullUrl.and.returnValue(mockUrl); + + activeCartServiceMock.getActiveCartId.and.returnValue(of(mockActiveCartId)); + + const config = service['setPaymentInitiationConfig']( + mockOtpKey, + mockPaymentOptionId + ); + + expect(config).toEqual({ + otpKey: mockOtpKey, + config: { + configurationId: mockPaymentOptionId.toString(), + cartId: mockActiveCartId, + resultURL: mockUrl, + cancelURL: mockUrl, + }, + }); + }); + + it('should execute script from HTML', fakeAsync(() => { + const mockHtml = ''; + + routingServiceMock.getRouterState.and.returnValue( + of({ state: { semanticRoute: 'checkoutReviewOrder' } } as RouterState) + ); + opfResourceLoaderServiceMock.executeScriptFromHtml.and.stub(); + + service['executeScriptFromHtml'](mockHtml); + + expect(routingServiceMock.getRouterState).toHaveBeenCalled(); + + tick(500); + expect( + opfResourceLoaderServiceMock.executeScriptFromHtml + ).toHaveBeenCalledWith(mockHtml); + })); + + it('should call the necessary methods on an error', () => { + service['onPlaceOrderError'](); + + expect(service['routingService'].go).toHaveBeenCalledWith({ + cxRoute: 'checkoutReviewOrder', + }); + }); +}); diff --git a/integration-libs/opf/checkout/core/connectors/opf-checkout.connector.spec.ts b/integration-libs/opf/checkout/core/connectors/opf-checkout.connector.spec.ts index 1a7d4cc3933..e5b083674c4 100644 --- a/integration-libs/opf/checkout/core/connectors/opf-checkout.connector.spec.ts +++ b/integration-libs/opf/checkout/core/connectors/opf-checkout.connector.spec.ts @@ -1,40 +1,55 @@ -// TODO: Add unit tests - -// import { TestBed } from '@angular/core/testing'; -// import { ActiveConfiguration } from '@spartacus/opf/root'; -// import { EMPTY, Observable } from 'rxjs'; -// import { OpfCheckoutConnector } from './opf-checkout.connector'; -// import { OpfAdapter } from './opf.adapter'; - -// class MockOpfAdapter implements Partial { -// getActiveConfigurations(): Observable { -// return EMPTY; -// } -// } - -// describe('OpfCheckoutConnector', () => { -// let service: OpfCheckoutConnector; -// let adapter: OpfAdapter; - -// beforeEach(() => { -// TestBed.configureTestingModule({ -// providers: [ -// OpfCheckoutConnector, -// { provide: OpfAdapter, useClass: MockOpfAdapter }, -// ], -// }); - -// service = TestBed.inject(OpfCheckoutConnector); -// adapter = TestBed.inject(OpfAdapter); -// }); - -// it('should be created', () => { -// expect(service).toBeTruthy(); -// }); - -// it('getActiveConfigurations should call adapter', () => { -// spyOn(adapter, 'getActiveConfigurations').and.stub(); -// service.getActiveConfigurations(); -// expect(adapter.getActiveConfigurations).toHaveBeenCalled(); -// }); -// }); +import { TestBed } from '@angular/core/testing'; +import { ActiveConfiguration } from '@spartacus/opf/checkout/root'; +import { EMPTY, Observable } from 'rxjs'; +import { PaymentInitiationConfig, PaymentSessionData } from '../../root/model'; +import { OpfCheckoutConnector } from './opf-checkout.connector'; +import { OpfAdapter } from './opf.adapter'; + +class MockOpfAdapter implements Partial { + getActiveConfigurations(): Observable { + return EMPTY; + } + initiatePayment( + _paymentConfig: PaymentInitiationConfig + ): Observable { + return EMPTY; + } +} + +describe('OpfCheckoutConnector', () => { + let service: OpfCheckoutConnector; + let adapter: OpfAdapter; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfCheckoutConnector, + { provide: OpfAdapter, useClass: MockOpfAdapter }, + ], + }); + + service = TestBed.inject(OpfCheckoutConnector); + adapter = TestBed.inject(OpfAdapter); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('getActiveConfigurations should call adapter', () => { + spyOn(adapter, 'getActiveConfigurations').and.stub(); + service.getActiveConfigurations(); + expect(adapter.getActiveConfigurations).toHaveBeenCalled(); + }); + + it('initiatePayment should call adapter', () => { + const paymentConfig: PaymentInitiationConfig = { + otpKey: 'test', + config: {}, + }; + spyOn(adapter, 'initiatePayment').and.stub(); + service.initiatePayment(paymentConfig); + + expect(adapter.initiatePayment).toHaveBeenCalledWith(paymentConfig); + }); +}); diff --git a/integration-libs/opf/checkout/core/facade/opf-checkout.service.spec.ts b/integration-libs/opf/checkout/core/facade/opf-checkout.service.spec.ts index 7b41fea0241..378ea7dab1a 100644 --- a/integration-libs/opf/checkout/core/facade/opf-checkout.service.spec.ts +++ b/integration-libs/opf/checkout/core/facade/opf-checkout.service.spec.ts @@ -1,74 +1,85 @@ -// TODO: Add unit tests +import { inject, TestBed } from '@angular/core/testing'; +import { QueryService } from '@spartacus/core'; +import { + ActiveConfiguration, + OpfPaymentProviderType, +} from '@spartacus/opf/checkout/root'; +import { of } from 'rxjs'; +import { take } from 'rxjs/operators'; +import { OpfCheckoutConnector } from '../connectors/opf-checkout.connector'; +import { OpfCheckoutService } from './opf-checkout.service'; +import createSpy = jasmine.createSpy; -// import { inject, TestBed } from '@angular/core/testing'; -// import { QueryService } from '@spartacus/core'; -// import { -// ActiveConfiguration, -// OpfPaymentProviderType, -// } from '@spartacus/opf/root'; -// import { of } from 'rxjs'; -// import { take } from 'rxjs/operators'; -// import { OpfCheckoutConnector } from '../connectors/opf-checkout.connector'; -// import { OpfCheckoutService } from './opf-checkout.service'; -// import createSpy = jasmine.createSpy; +const mockActiveConfigurations: ActiveConfiguration[] = [ + { + id: 1, + providerType: OpfPaymentProviderType.PAYMENT_GATEWAY, + displayName: 'Test1', + }, + { + id: 2, + providerType: OpfPaymentProviderType.PAYMENT_METHOD, + displayName: 'Test2', + }, +]; -// const mockActiveConfigurations: ActiveConfiguration[] = [ -// { -// id: 1, -// providerType: OpfPaymentProviderType.PAYMENT_GATEWAY, -// displayName: 'Test1', -// }, -// { -// id: 2, -// providerType: OpfPaymentProviderType.PAYMENT_METHOD, -// displayName: 'Test2', -// }, -// ]; +class MockOpfCheckoutConnector implements Partial { + getActiveConfigurations = createSpy().and.returnValue( + of(mockActiveConfigurations) + ); + initiatePayment = createSpy().and.returnValue(of('initiatePayment')); +} -// class MockOpfCheckoutConnector implements Partial { -// getActiveConfigurations = createSpy().and.returnValue( -// of(mockActiveConfigurations) -// ); -// } +describe(`CheckoutPaymentService`, () => { + let service: OpfCheckoutService; + let connector: OpfCheckoutConnector; -// describe(`CheckoutPaymentService`, () => { -// let service: OpfCheckoutService; -// let connector: OpfCheckoutConnector; + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfCheckoutService, + QueryService, + { provide: OpfCheckoutConnector, useClass: MockOpfCheckoutConnector }, + ], + }); -// beforeEach(() => { -// TestBed.configureTestingModule({ -// providers: [ -// OpfCheckoutService, -// QueryService, -// { provide: OpfCheckoutConnector, useClass: MockOpfCheckoutConnector }, -// ], -// }); + service = TestBed.inject(OpfCheckoutService); + connector = TestBed.inject(OpfCheckoutConnector); + }); -// service = TestBed.inject(OpfCheckoutService); -// connector = TestBed.inject(OpfCheckoutConnector); -// }); + it(`should inject OpfCheckoutService`, inject( + [OpfCheckoutService], + (opfCheckoutService: OpfCheckoutService) => { + expect(opfCheckoutService).toBeTruthy(); + } + )); -// it(`should inject OpfCheckoutService`, inject( -// [OpfCheckoutService], -// (opfCheckoutService: OpfCheckoutService) => { -// expect(opfCheckoutService).toBeTruthy(); -// } -// )); + describe(`getActiveConfigurationsState`, () => { + it(`should call the opfCheckoutConnector.getActiveConfigurations()`, (done) => { + service + .getActiveConfigurationsState() + .pipe(take(1)) + .subscribe((state) => { + expect(connector.getActiveConfigurations).toHaveBeenCalled(); + expect(state).toEqual({ + loading: false, + error: false, + data: mockActiveConfigurations, + }); + done(); + }); + }); + }); -// describe(`getActiveConfigurationsState`, () => { -// it(`should call the opfCheckoutConnector.getActiveConfigurations()`, (done) => { -// service -// .getActiveConfigurationsState() -// .pipe(take(1)) -// .subscribe((state) => { -// expect(connector.getActiveConfigurations).toHaveBeenCalled(); -// expect(state).toEqual({ -// loading: false, -// error: false, -// data: mockActiveConfigurations, -// }); -// done(); -// }); -// }); -// }); -// }); + describe(`initiatePayment`, () => { + it(`should call the opfCheckoutConnector.initiatePayment()`, (done) => { + service + .initiatePayment({}) + .pipe(take(1)) + .subscribe(() => { + expect(connector.initiatePayment).toHaveBeenCalledWith({}); + done(); + }); + }); + }); +}); diff --git a/integration-libs/opf/checkout/core/services/opf-endpoints.service.spec.ts b/integration-libs/opf/checkout/core/services/opf-endpoints.service.spec.ts index 109ad68161e..5b17f168855 100644 --- a/integration-libs/opf/checkout/core/services/opf-endpoints.service.spec.ts +++ b/integration-libs/opf/checkout/core/services/opf-endpoints.service.spec.ts @@ -1,52 +1,50 @@ -// TODO: Add unit tests - -// import { TestBed } from '@angular/core/testing'; -// import { BaseSiteService, Config } from '@spartacus/core'; -// import { of } from 'rxjs'; -// import { OpfEndpointsService } from './opf-endpoints.service'; -// import createSpy = jasmine.createSpy; - -// const mockOccConfig = { -// backend: { -// occ: { -// baseUrl: 'http://test-baseUrl', -// endpoints: { -// test: 'test-endpoint', -// }, -// }, -// }, -// opf: { -// baseUrl: 'http://test-opf.com', -// }, -// }; - -// const baseSite = 'test-baseSite'; -// class MockBaseSiteService { -// getActive = createSpy().and.returnValue(of(baseSite)); -// } - -// describe(`OpfEndpointsService`, () => { -// let service: OpfEndpointsService; -// let baseSiteService: BaseSiteService; - -// beforeEach(() => { -// TestBed.configureTestingModule({ -// providers: [ -// OpfEndpointsService, -// { provide: BaseSiteService, useClass: MockBaseSiteService }, -// { provide: Config, useValue: mockOccConfig }, -// ], -// }); - -// service = TestBed.inject(OpfEndpointsService); -// baseSiteService = TestBed.inject(BaseSiteService); -// }); - -// it(`should build proper url based on passed endpoint for context`, () => { -// const result = service.buildUrl('test'); -// const expectedUrl = `${mockOccConfig.opf.baseUrl}/${baseSite}/${mockOccConfig.backend.occ.endpoints.test}`; - -// expect(result).toEqual(expectedUrl); -// expect(baseSiteService.getActive).toHaveBeenCalled(); -// }); -// }); +import { TestBed } from '@angular/core/testing'; +import { BaseSiteService, Config } from '@spartacus/core'; +import { of } from 'rxjs'; +import { OpfEndpointsService } from './opf-endpoints.service'; +import createSpy = jasmine.createSpy; + +const mockOccConfig = { + backend: { + occ: { + baseUrl: 'http://test-baseUrl', + endpoints: { + test: 'test-endpoint', + }, + }, + }, + opf: { + baseUrl: 'http://test-opf.com', + }, +}; + +const baseSite = 'test-baseSite'; +class MockBaseSiteService { + getActive = createSpy().and.returnValue(of(baseSite)); +} + +describe(`OpfEndpointsService`, () => { + let service: OpfEndpointsService; + let baseSiteService: BaseSiteService; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfEndpointsService, + { provide: BaseSiteService, useClass: MockBaseSiteService }, + { provide: Config, useValue: mockOccConfig }, + ], + }); + + service = TestBed.inject(OpfEndpointsService); + baseSiteService = TestBed.inject(BaseSiteService); + }); + + it(`should build proper url based on passed endpoint for context`, () => { + const result = service.buildUrl('test'); + const expectedUrl = `${mockOccConfig.opf.baseUrl}/${baseSite}/${mockOccConfig.backend.occ.endpoints.test}`; + + expect(result).toEqual(expectedUrl); + expect(baseSiteService.getActive).toHaveBeenCalled(); + }); +});