From ba9c59399d397828e7ed781aadbe228bc482e01c Mon Sep 17 00:00:00 2001 From: Radhep Sabapathipillai Date: Tue, 18 Jul 2023 12:37:07 -0400 Subject: [PATCH 1/3] add tests for OpfOrderService --- .../core/facade/opf-order.service.spec.ts | 118 +++++++++++++++++- 1 file changed, 112 insertions(+), 6 deletions(-) diff --git a/integration-libs/opf/base/core/facade/opf-order.service.spec.ts b/integration-libs/opf/base/core/facade/opf-order.service.spec.ts index c233f2d9ee2..9287e91582b 100644 --- a/integration-libs/opf/base/core/facade/opf-order.service.spec.ts +++ b/integration-libs/opf/base/core/facade/opf-order.service.spec.ts @@ -1,7 +1,113 @@ -/* - * SPDX-FileCopyrightText: 2023 SAP Spartacus team - * - * SPDX-License-Identifier: Apache-2.0 - */ +import { TestBed } from '@angular/core/testing'; +import { ActiveCartFacade } from '@spartacus/cart/base/root'; +import { UserIdService } from '@spartacus/core'; +import { of } from 'rxjs'; -// TODO: Add unit tests +import { OpfOrderConnector } from '../connectors'; +import { OpfOrderService } from './opf-order.service'; +import { Order } from '@spartacus/order/root'; + +import createSpy = jasmine.createSpy; + +const mockOrder = { code: 'mockOrder' }; +class MockOpfOrderConnector implements Partial { + placeOpfOrder = createSpy().and.callFake(() => of(mockOrder)); +} + +describe('OpfOrderService', () => { + let service: OpfOrderService; + + let activeCartFacade: jasmine.SpyObj; + let userIdService: jasmine.SpyObj; + + activeCartFacade = jasmine.createSpyObj('ActiveCartFacade', [ + 'takeActiveCartId', + 'isGuestCart', + ]); + userIdService = jasmine.createSpyObj('UserIdService', ['takeUserId']); + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfOrderService, + { + provide: OpfOrderConnector, + useClass: MockOpfOrderConnector, + }, + { + provide: UserIdService, + useValue: userIdService, + }, + { + provide: ActiveCartFacade, + useValue: activeCartFacade, + }, + ], + }); + + service = TestBed.inject(OpfOrderService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should place order and return results', () => { + activeCartFacade.takeActiveCartId.and.returnValue(of('current')); + activeCartFacade.isGuestCart.and.returnValue(of(true)); + userIdService.takeUserId.and.returnValue(of('userId')); + + service + .placeOpfOrder(true) + .subscribe((result: Order) => { + expect(result).toEqual(mockOrder); + expect(result.code).toEqual('mockOrder'); + }) + .unsubscribe(); + }); + + it('should throw error when cart is anonymous', () => { + activeCartFacade.takeActiveCartId.and.returnValue(of('')); + activeCartFacade.isGuestCart.and.returnValue(of(true)); + userIdService.takeUserId.and.returnValue(of('userId')); + + service + .placeOpfOrder(true) + .subscribe({ + error: (e) => { + expect(e).toEqual(new Error('Checkout conditions not met')); + }, + }) + .unsubscribe(); + }); + + it('should throw error when user is not logged in', () => { + activeCartFacade.takeActiveCartId.and.returnValue(of('current')); + activeCartFacade.isGuestCart.and.returnValue(of(false)); + userIdService.takeUserId.and.returnValue(of('')); + + service + .placeOpfOrder(true) + .subscribe({ + error: (e) => { + expect(e).toEqual(new Error('Checkout conditions not met')); + }, + }) + .unsubscribe(); + }); + + it('should throw error when user is anonymous and cart is not guest', () => { + activeCartFacade.takeActiveCartId.and.returnValue(of('current')); + activeCartFacade.isGuestCart.and.returnValue(of(false)); + userIdService.takeUserId.and.returnValue(of('anonymous')); + + service + .placeOpfOrder(true) + .subscribe({ + error: (e) => { + expect(e).toEqual(new Error('Checkout conditions not met')); + }, + }) + .unsubscribe(); + }); +}); From ecb7da2d11d6fc364d27e210d21b946d3cb77114 Mon Sep 17 00:00:00 2001 From: Radhep Sabapathipillai Date: Thu, 27 Jul 2023 09:13:55 -0400 Subject: [PATCH 2/3] add additional tests to fix code coverage --- .../connectors/opf-order.connector.spec.ts | 39 ++++++++++ .../connectors/opf-payment.connector.spec.ts | 71 ++++++++++--------- .../core/connectors/otp.connector.spec.ts | 39 ++++++++++ 3 files changed, 116 insertions(+), 33 deletions(-) create mode 100644 integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts create mode 100644 integration-libs/opf/base/core/connectors/otp.connector.spec.ts diff --git a/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts b/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts new file mode 100644 index 00000000000..2c7e1b1b64d --- /dev/null +++ b/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts @@ -0,0 +1,39 @@ +import { TestBed } from '@angular/core/testing'; +import { of } from 'rxjs'; +import createSpy = jasmine.createSpy; +import { OpfOrderAdapter } from './opf-order.adapter'; +import { OpfOrderConnector } from './opf-order.connector'; + +class MockOpfOrderAdapter implements OpfOrderAdapter { + placeOpfOrder = createSpy('placeOpfOrder').and.callFake((userId, cartId, termsChecked) => + of(`load-${userId}-${cartId}-${termsChecked}`) + ); +} + +describe('OpfOrderConnector', () => { + let service: OpfOrderConnector; + let adapter: OpfOrderAdapter; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfOrderConnector, + { provide: OpfOrderAdapter, useClass: MockOpfOrderAdapter }, + ], + }); + + service = TestBed.inject(OpfOrderConnector); + adapter = TestBed.inject(OpfOrderAdapter); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('validate should call adapter', () => { + let result; + service.placeOpfOrder('user1', 'cart1', true).subscribe((res) => (result = res)); + expect(result).toEqual('load-user1-cart1-true'); + expect(adapter.placeOpfOrder).toHaveBeenCalledWith('user1', 'cart1', true); + }); +}); diff --git a/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts b/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts index 1a7d4cc3933..4eaffe4df79 100644 --- a/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts +++ b/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts @@ -1,40 +1,45 @@ -// TODO: Add unit tests +import { TestBed } from '@angular/core/testing'; +import { of } from 'rxjs'; +import { take } from 'rxjs/operators'; +import createSpy = jasmine.createSpy; +import { OpfPaymentAdapter } from './opf-payment.adapter'; +import { OpfPaymentConnector } from './opf-payment.connector'; -// 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 MockOpfPaymentAdapter implements OpfPaymentAdapter { + verifyPayment = createSpy().and.returnValue(of({})); + submitPayment = createSpy().and.returnValue(of({})); +} -// class MockOpfAdapter implements Partial { -// getActiveConfigurations(): Observable { -// return EMPTY; -// } -// } +describe('OpfPaymentConnector', () => { + let service: OpfPaymentConnector; + let adapter: OpfPaymentAdapter; -// describe('OpfCheckoutConnector', () => { -// let service: OpfCheckoutConnector; -// let adapter: OpfAdapter; + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OpfPaymentConnector, + { + provide: OpfPaymentAdapter, + useClass: MockOpfPaymentAdapter, + }, + ], + }); -// beforeEach(() => { -// TestBed.configureTestingModule({ -// providers: [ -// OpfCheckoutConnector, -// { provide: OpfAdapter, useClass: MockOpfAdapter }, -// ], -// }); + service = TestBed.inject(OpfPaymentConnector); + adapter = TestBed.inject(OpfPaymentAdapter); + }); -// service = TestBed.inject(OpfCheckoutConnector); -// adapter = TestBed.inject(OpfAdapter); -// }); + it('should be created', () => { + expect(service).toBeTruthy(); + }); -// it('should be created', () => { -// expect(service).toBeTruthy(); -// }); + it('should call adapter', () => { + service.verifyPayment('1', {responseMap: [{key: 'test', value: 'value'}]}).pipe(take(1)).subscribe(); + expect(adapter.verifyPayment).toHaveBeenCalledWith('1', {responseMap: [{key: 'test', value: 'value'}]}); + }); -// it('getActiveConfigurations should call adapter', () => { -// spyOn(adapter, 'getActiveConfigurations').and.stub(); -// service.getActiveConfigurations(); -// expect(adapter.getActiveConfigurations).toHaveBeenCalled(); -// }); -// }); + it('should call adapter', () => { + service.submitPayment({}, '1', '2').pipe(take(1)).subscribe(); + expect(adapter.submitPayment).toHaveBeenCalledWith({}, '1', '2'); + }); +}); \ No newline at end of file diff --git a/integration-libs/opf/base/core/connectors/otp.connector.spec.ts b/integration-libs/opf/base/core/connectors/otp.connector.spec.ts new file mode 100644 index 00000000000..59eafae64ee --- /dev/null +++ b/integration-libs/opf/base/core/connectors/otp.connector.spec.ts @@ -0,0 +1,39 @@ +import { TestBed } from '@angular/core/testing'; +import { of } from 'rxjs'; +import { take } from 'rxjs/operators'; +import createSpy = jasmine.createSpy; +import { OtpConnector } from './otp.connector'; +import { OtpAdapter } from './otp.adapter'; + +class MockOtpAdapter implements OtpAdapter { + generateOtpKey = createSpy().and.returnValue(of({})); +} + +describe('OtpConnector', () => { + let service: OtpConnector; + let adapter: OtpAdapter; + + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ + OtpConnector, + { + provide: OtpAdapter, + useClass: MockOtpAdapter, + }, + ], + }); + + service = TestBed.inject(OtpConnector); + adapter = TestBed.inject(OtpAdapter); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('should call adapter', () => { + service.generateOtpKey('user1', 'cart1').pipe(take(1)).subscribe(); + expect(adapter.generateOtpKey).toHaveBeenCalledWith('user1', 'cart1'); + }); +}); \ No newline at end of file From 80c11436df50d8e5cbcf3702e105c80ea05122b0 Mon Sep 17 00:00:00 2001 From: Radhep Sabapathipillai Date: Thu, 27 Jul 2023 09:31:10 -0400 Subject: [PATCH 3/3] format --- .../base/core/connectors/opf-order.connector.spec.ts | 9 ++++++--- .../core/connectors/opf-payment.connector.spec.ts | 11 ++++++++--- .../opf/base/core/connectors/otp.connector.spec.ts | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts b/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts index 2c7e1b1b64d..7ef377453e4 100644 --- a/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts +++ b/integration-libs/opf/base/core/connectors/opf-order.connector.spec.ts @@ -5,8 +5,9 @@ import { OpfOrderAdapter } from './opf-order.adapter'; import { OpfOrderConnector } from './opf-order.connector'; class MockOpfOrderAdapter implements OpfOrderAdapter { - placeOpfOrder = createSpy('placeOpfOrder').and.callFake((userId, cartId, termsChecked) => - of(`load-${userId}-${cartId}-${termsChecked}`) + placeOpfOrder = createSpy('placeOpfOrder').and.callFake( + (userId, cartId, termsChecked) => + of(`load-${userId}-${cartId}-${termsChecked}`) ); } @@ -32,7 +33,9 @@ describe('OpfOrderConnector', () => { it('validate should call adapter', () => { let result; - service.placeOpfOrder('user1', 'cart1', true).subscribe((res) => (result = res)); + service + .placeOpfOrder('user1', 'cart1', true) + .subscribe((res) => (result = res)); expect(result).toEqual('load-user1-cart1-true'); expect(adapter.placeOpfOrder).toHaveBeenCalledWith('user1', 'cart1', true); }); diff --git a/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts b/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts index 4eaffe4df79..5adb2a3b963 100644 --- a/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts +++ b/integration-libs/opf/base/core/connectors/opf-payment.connector.spec.ts @@ -34,12 +34,17 @@ describe('OpfPaymentConnector', () => { }); it('should call adapter', () => { - service.verifyPayment('1', {responseMap: [{key: 'test', value: 'value'}]}).pipe(take(1)).subscribe(); - expect(adapter.verifyPayment).toHaveBeenCalledWith('1', {responseMap: [{key: 'test', value: 'value'}]}); + service + .verifyPayment('1', { responseMap: [{ key: 'test', value: 'value' }] }) + .pipe(take(1)) + .subscribe(); + expect(adapter.verifyPayment).toHaveBeenCalledWith('1', { + responseMap: [{ key: 'test', value: 'value' }], + }); }); it('should call adapter', () => { service.submitPayment({}, '1', '2').pipe(take(1)).subscribe(); expect(adapter.submitPayment).toHaveBeenCalledWith({}, '1', '2'); }); -}); \ No newline at end of file +}); diff --git a/integration-libs/opf/base/core/connectors/otp.connector.spec.ts b/integration-libs/opf/base/core/connectors/otp.connector.spec.ts index 59eafae64ee..5136c3f66d5 100644 --- a/integration-libs/opf/base/core/connectors/otp.connector.spec.ts +++ b/integration-libs/opf/base/core/connectors/otp.connector.spec.ts @@ -36,4 +36,4 @@ describe('OtpConnector', () => { service.generateOtpKey('user1', 'cart1').pipe(take(1)).subscribe(); expect(adapter.generateOtpKey).toHaveBeenCalledWith('user1', 'cart1'); }); -}); \ No newline at end of file +});