From ecb7da2d11d6fc364d27e210d21b946d3cb77114 Mon Sep 17 00:00:00 2001 From: Radhep Sabapathipillai Date: Thu, 27 Jul 2023 09:13:55 -0400 Subject: [PATCH] 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