Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Add tests for OpfOrderService #17660

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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);
});
});
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
// 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<OpfAdapter> {
// getActiveConfigurations(): Observable<ActiveConfiguration[]> {
// 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');
});
});
39 changes: 39 additions & 0 deletions integration-libs/opf/base/core/connectors/otp.connector.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
118 changes: 112 additions & 6 deletions integration-libs/opf/base/core/facade/opf-order.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]>
*
* 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<OpfOrderConnector> {
placeOpfOrder = createSpy().and.callFake(() => of(mockOrder));
}

describe('OpfOrderService', () => {
let service: OpfOrderService;

let activeCartFacade: jasmine.SpyObj<ActiveCartFacade>;
let userIdService: jasmine.SpyObj<UserIdService>;

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();
});
});
Loading