Skip to content

Commit

Permalink
fix: Update tests coverage
Browse files Browse the repository at this point in the history
Closes: CXSPA-4611, CXSPA-4612
  • Loading branch information
Matejk00 authored Sep 11, 2023
1 parent a06eac9 commit 4a9cc67
Show file tree
Hide file tree
Showing 9 changed files with 723 additions and 726 deletions.
Original file line number Diff line number Diff line change
@@ -1,153 +1,153 @@
// /*
// * SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]>
// *
// * SPDX-License-Identifier: Apache-2.0
// */

// import { Component } from '@angular/core';
// import { ComponentFixture, TestBed } from '@angular/core/testing';
// import { ActivatedRoute } from '@angular/router';
// import { HttpErrorModel } from '@spartacus/core';
// import { Order } from '@spartacus/order/root';
// import { of, Subscription, throwError } from 'rxjs';
// import { OpfResponseMapElement } from '../../model';
// import { OpfPaymentVerificationComponent } from './opf-payment-verification.component';
// import { OpfPaymentVerificationService } from './opf-payment-verification.service';

// @Component({
// selector: 'cx-spinner',
// template: '',
// })
// class MockSpinnerComponent {}

// describe('OpfPaymentVerificationComponent', () => {
// let component: OpfPaymentVerificationComponent;
// let fixture: ComponentFixture<OpfPaymentVerificationComponent>;
// let routeMock: jasmine.SpyObj<ActivatedRoute>;
// let paymentServiceMock: jasmine.SpyObj<OpfPaymentVerificationService>;

// beforeEach(() => {
// routeMock = jasmine.createSpyObj('ActivatedRoute', [], {
// snapshot: { queryParamMap: new Map() },
// });
// paymentServiceMock = jasmine.createSpyObj('OpfPaymentVerificationService', [
// 'checkIfProcessingCartIdExist',
// 'verifyResultUrl',
// 'verifyPayment',
// 'placeOrder',
// 'goToPage',
// 'displayError',
// ]);

// TestBed.configureTestingModule({
// declarations: [OpfPaymentVerificationComponent, MockSpinnerComponent],
// providers: [
// { provide: ActivatedRoute, useValue: routeMock },
// {
// provide: OpfPaymentVerificationService,
// useValue: paymentServiceMock,
// },
// ],
// });

// fixture = TestBed.createComponent(OpfPaymentVerificationComponent);
// component = fixture.componentInstance;
// });

// it('should create', () => {
// expect(component).toBeTruthy();
// });

// describe('ngOnInit', () => {
// it('should call checkIfProcessingCartIdExist', () => {
// paymentServiceMock.verifyResultUrl.and.returnValue(of());

// component.ngOnInit();
// expect(
// paymentServiceMock.checkIfProcessingCartIdExist
// ).toHaveBeenCalled();
// });

// it('should handle success scenario', () => {
// const mockPaymentSessionId = 'sessionId';
// const mockResponseMap: OpfResponseMapElement[] = [];
// const mockVerifyResult: {
// paymentSessionId: string;
// responseMap: OpfResponseMapElement[];
// } = {
// paymentSessionId: mockPaymentSessionId,
// responseMap: mockResponseMap,
// };
// const mockPlaceOrderResult: Order = { guid: 'placeOrderResult' };

// paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
// paymentServiceMock.verifyPayment.and.returnValue(of(true));
// paymentServiceMock.placeOrder.and.returnValue(of(mockPlaceOrderResult));

// component.ngOnInit();

// expect(paymentServiceMock.verifyResultUrl).toHaveBeenCalledWith(
// routeMock
// );
// expect(paymentServiceMock.verifyPayment).toHaveBeenCalledWith(
// mockPaymentSessionId,
// mockResponseMap
// );
// expect(paymentServiceMock.placeOrder).toHaveBeenCalled();
// });

// it('should handle error scenario', () => {
// const mockError: HttpErrorModel = { status: 500, message: 'Error' };

// const mockVerifyResult = {
// paymentSessionId: '1',
// responseMap: [],
// };

// paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
// paymentServiceMock.verifyPayment.and.returnValue(throwError(mockError));

// spyOn(component, 'onError');

// component.ngOnInit();

// expect(component.onError).toHaveBeenCalledWith(mockError);
// });
// });

// describe('onSuccess', () => {
// it('should call paymentService.goToPage with "orderConfirmation"', () => {
// component.onSuccess();
// expect(paymentServiceMock.goToPage).toHaveBeenCalledWith(
// 'orderConfirmation'
// );
// });
// });

// describe('onError', () => {
// it('should call paymentService.displayError with the provided error and paymentService.goToPage with "checkoutReviewOrder"', () => {
// const mockError: HttpErrorModel = { status: 404, message: 'Not Found' };

// component.onError(mockError);

// expect(paymentServiceMock.displayError).toHaveBeenCalledWith(mockError);
// expect(paymentServiceMock.goToPage).toHaveBeenCalledWith(
// 'checkoutReviewOrder'
// );
// });
// });

// describe('ngOnDestroy', () => {
// it('should unsubscribe from the subscription', () => {
// const subscriptionMock: Subscription = jasmine.createSpyObj(
// 'Subscription',
// ['unsubscribe']
// );
// component.subscription = subscriptionMock;

// component.ngOnDestroy();

// expect(subscriptionMock.unsubscribe).toHaveBeenCalled();
// });
// });
// });
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { HttpErrorModel } from '@spartacus/core';
import { Order } from '@spartacus/order/root';
import { Subscription, of, throwError } from 'rxjs';
import { OpfResponseMapElement } from '../../model';
import { OpfPaymentVerificationComponent } from './opf-payment-verification.component';
import { OpfPaymentVerificationService } from './opf-payment-verification.service';

@Component({
selector: 'cx-spinner',
template: '',
})
class MockSpinnerComponent {}

describe('OpfPaymentVerificationComponent', () => {
let component: OpfPaymentVerificationComponent;
let fixture: ComponentFixture<OpfPaymentVerificationComponent>;
let routeMock: jasmine.SpyObj<ActivatedRoute>;
let paymentServiceMock: jasmine.SpyObj<OpfPaymentVerificationService>;

beforeEach(() => {
routeMock = jasmine.createSpyObj('ActivatedRoute', [], {
snapshot: { queryParamMap: new Map() },
});
paymentServiceMock = jasmine.createSpyObj('OpfPaymentVerificationService', [
'checkIfProcessingCartIdExist',
'verifyResultUrl',
'verifyPayment',
'placeOrder',
'goToPage',
'displayError',
]);

TestBed.configureTestingModule({
declarations: [OpfPaymentVerificationComponent, MockSpinnerComponent],
providers: [
{ provide: ActivatedRoute, useValue: routeMock },
{
provide: OpfPaymentVerificationService,
useValue: paymentServiceMock,
},
],
});

fixture = TestBed.createComponent(OpfPaymentVerificationComponent);
component = fixture.componentInstance;
});

it('should create', () => {
expect(component).toBeTruthy();
});

describe('ngOnInit', () => {
it('should call checkIfProcessingCartIdExist', () => {
paymentServiceMock.verifyResultUrl.and.returnValue(of());

component.ngOnInit();
expect(
paymentServiceMock.checkIfProcessingCartIdExist
).toHaveBeenCalled();
});

it('should handle success scenario', () => {
const mockPaymentSessionId = 'sessionId';
const mockResponseMap: OpfResponseMapElement[] = [];
const mockVerifyResult: {
paymentSessionId: string;
responseMap: OpfResponseMapElement[];
} = {
paymentSessionId: mockPaymentSessionId,
responseMap: mockResponseMap,
};
const mockPlaceOrderResult: Order = { guid: 'placeOrderResult' };

paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
paymentServiceMock.verifyPayment.and.returnValue(of(true));
paymentServiceMock.placeOrder.and.returnValue(of(mockPlaceOrderResult));

component.ngOnInit();

expect(paymentServiceMock.verifyResultUrl).toHaveBeenCalledWith(
routeMock
);
expect(paymentServiceMock.verifyPayment).toHaveBeenCalledWith(
mockPaymentSessionId,
mockResponseMap
);
expect(paymentServiceMock.placeOrder).toHaveBeenCalled();
});

it('should handle error scenario', () => {
const mockError: HttpErrorModel = { status: 500, message: 'Error' };

const mockVerifyResult = {
paymentSessionId: '1',
responseMap: [],
};

paymentServiceMock.verifyResultUrl.and.returnValue(of(mockVerifyResult));
paymentServiceMock.verifyPayment.and.returnValue(throwError(mockError));

spyOn(component, 'onError');

component.ngOnInit();

expect(component.onError).toHaveBeenCalledWith(mockError);
});
});

describe('onSuccess', () => {
it('should call paymentService.goToPage with "orderConfirmation"', () => {
component.onSuccess();
expect(paymentServiceMock.goToPage).toHaveBeenCalledWith(
'orderConfirmation'
);
});
});

describe('onError', () => {
it('should call paymentService.displayError with the provided error and paymentService.goToPage with "checkoutReviewOrder"', () => {
const mockError: HttpErrorModel = { status: 404, message: 'Not Found' };

component.onError(mockError);

expect(paymentServiceMock.displayError).toHaveBeenCalledWith(mockError);
expect(paymentServiceMock.goToPage).toHaveBeenCalledWith(
'checkoutReviewOrder'
);
});
});

describe('ngOnDestroy', () => {
it('should unsubscribe from the subscription', () => {
const subscriptionMock: Subscription = jasmine.createSpyObj(
'Subscription',
['unsubscribe']
);
component.subscription = subscriptionMock;

component.ngOnDestroy();

expect(subscriptionMock.unsubscribe).toHaveBeenCalled();
});
});
});
Loading

0 comments on commit 4a9cc67

Please sign in to comment.