Skip to content

Commit

Permalink
CXSPA-4049 Add tests for payment verification process (#17662)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmch91 authored Jul 20, 2023
1 parent 21c1fb3 commit d534cda
Show file tree
Hide file tree
Showing 5 changed files with 830 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,153 @@
// TODO: Add unit tests
/*
* 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();
});
});
});
Loading

0 comments on commit d534cda

Please sign in to comment.