Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
bistaastha committed Jan 7, 2025
1 parent e905388 commit a34b2a3
Showing 1 changed file with 0 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -1,172 +0,0 @@
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { CorporateCreditCardExpenseService } from 'src/app/core/services/corporate-credit-card-expense.service';
import { SpenderOnboardingConnectCardStepComponent } from './spender-onboarding-connect-card-step.component';
import { RealTimeFeedService } from 'src/app/core/services/real-time-feed.service';
import { FormBuilder, FormControl, ReactiveFormsModule, Validators } from '@angular/forms';
import { IonicModule, PopoverController } from '@ionic/angular';
import { By } from '@angular/platform-browser';
import { CardNetworkType } from 'src/app/core/enums/card-network-type';
import { NgxMaskModule } from 'ngx-mask';
import { HttpErrorResponse } from '@angular/common/http';
import { of, throwError } from 'rxjs';
import { statementUploadedCard } from 'src/app/core/mock-data/platform-corporate-card.data';

fdescribe('SpenderOnboardingConnectCardStepComponent', () => {
let component: SpenderOnboardingConnectCardStepComponent;
let fixture: ComponentFixture<SpenderOnboardingConnectCardStepComponent>;
let corporateCreditCardExpenseService: jasmine.SpyObj<CorporateCreditCardExpenseService>;
let realTimeFeedService: jasmine.SpyObj<RealTimeFeedService>;
let popoverController: jasmine.SpyObj<PopoverController>;
let fb: FormBuilder;

beforeEach(waitForAsync(() => {
const corporateCreditCardExpenseServiceSpy = jasmine.createSpyObj('CorporateCreditCardExpenseService', [
'getCorporateCards',
]);
const popoverControllerSpy = jasmine.createSpyObj('PopoverController', ['dismiss']);
const realTimeFeedServiceSpy = jasmine.createSpyObj('RealTimeFeedService', [
'enroll',
'getCardTypeFromNumber',
'isCardNumberValid',
]);

TestBed.configureTestingModule({
declarations: [SpenderOnboardingConnectCardStepComponent],
imports: [IonicModule.forRoot(), NgxMaskModule.forRoot(), ReactiveFormsModule],
providers: [
FormBuilder,
{ provide: RealTimeFeedService, useValue: realTimeFeedServiceSpy },
{ provide: CorporateCreditCardExpenseService, useValue: corporateCreditCardExpenseServiceSpy },
{ provide: PopoverController, useValue: popoverControllerSpy },
],
}).compileComponents();

fixture = TestBed.createComponent(SpenderOnboardingConnectCardStepComponent);
component = fixture.componentInstance;

realTimeFeedService = TestBed.inject(RealTimeFeedService) as jasmine.SpyObj<RealTimeFeedService>;
corporateCreditCardExpenseService = TestBed.inject(
CorporateCreditCardExpenseService
) as jasmine.SpyObj<CorporateCreditCardExpenseService>;
fb = TestBed.inject(FormBuilder);
component.fg = fb.group({});
popoverController = TestBed.inject(PopoverController) as jasmine.SpyObj<PopoverController>;
corporateCreditCardExpenseService.getCorporateCards.and.returnValue(null);
realTimeFeedService.isCardNumberValid.and.returnValue(true);
realTimeFeedService.getCardTypeFromNumber.and.returnValue(CardNetworkType.VISA);
}));

it('setupErrorMessages(): should add card to failedCards and call handleEnrollmentFailures', () => {
const error = new HttpErrorResponse({ status: 400, statusText: 'Bad Request' });
//@ts-ignore
spyOn(component, 'handleEnrollmentFailures');

component.cardsList = { failedCards: [], successfulCards: [] }; // Initialize card list
component.setupErrorMessages(error, '1234', 'bacc1234');

expect(component.cardsList.failedCards).toContain('**** 1234');
//@ts-ignore
expect(component.handleEnrollmentFailures).toHaveBeenCalledWith(error, 'bacc1234');
});

describe('enrollMultipleCards(): ', () => {
it('should call enroll for each card and handle success and failure cases', fakeAsync(() => {
const cards = [statementUploadedCard, { ...statementUploadedCard, id: 'bacc15bbrRGWzg' }];
corporateCreditCardExpenseService.getCorporateCards.and.returnValue(of(cards));
component.cardsList = { failedCards: [], successfulCards: [] }; // Initialize card list
component.cardValuesMap = {
bacc15bbrRGWzf: { last_four: '1111', card_type: CardNetworkType.OTHERS },
bacc15bbrRGWzg: { last_four: '0004', card_type: CardNetworkType.OTHERS },
}; // Mock card values
component.fg = new FormBuilder().group({
card_number_bacc15bbrRGWzf: [
'411111111111',
Validators.required,
component.cardNumberValidator.bind(component),
component.cardNetworkValidator.bind(component),
],
card_number_bacc15bbrRGWzg: [
'550000000000',
Validators.required,
component.cardNumberValidator.bind(component),
component.cardNetworkValidator.bind(component),
],
});

realTimeFeedService.enroll.and.returnValues(
of(null), // Success for first card
throwError(new HttpErrorResponse({ status: 400, statusText: 'Bad Request' })) // Error for second card
);

spyOn(component, 'setupErrorMessages');
spyOn(component, 'showErrorPopover');

component.enrollMultipleCards(cards);

fixture.detectChanges();
tick();
expect(realTimeFeedService.enroll).toHaveBeenCalledWith('4111111111111111', 'bacc123456');
expect(realTimeFeedService.enroll).toHaveBeenCalledWith('5500000000000004', 'bacc654321');
expect(component.cardsList.successfulCards).toContain('**** 1111');
expect(component.cardsList.failedCards).toContain('**** 550000000000');
expect(component.setupErrorMessages).toHaveBeenCalled();
expect(component.showErrorPopover).toHaveBeenCalled();
}));
});

describe('template', () => {
beforeEach(() => {
realTimeFeedService.isCardNumberValid.and.returnValue(true);
realTimeFeedService.getCardTypeFromNumber.and.returnValue(CardNetworkType.VISA);
});

it('should call onCardNumberUpdate when input changes', () => {
component.fg = fb.group({
card_number: [
'',
Validators.compose([
Validators.required,
Validators.maxLength(16),
// @ts-ignore
component.cardNumberValidator.bind(component),
// @ts-ignore
component.cardNetworkValidator.bind(component),
]),
],
});
fixture.detectChanges();
const cardNumberUpdateSpy = spyOn(component, 'onCardNumberUpdate');
const input = fixture.debugElement.query(By.css('[data-testid="card-number-input"]')).nativeElement;
input.dispatchEvent(new Event('input'));
expect(cardNumberUpdateSpy).toHaveBeenCalled();
});

it('should enable Continue button when form is valid', () => {
component.fg = fb.group({
card_number: [
'',
Validators.compose([
Validators.required,
Validators.maxLength(16),
// @ts-ignore
component.cardNumberValidator.bind(component),
// @ts-ignore
component.cardNetworkValidator.bind(component),
]),
],
});
// eslint-disable-next-line @typescript-eslint/dot-notation
component.fg.controls['card_number_bacc1234'].setValue('4111111111111111');
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('.connect-card__primary-cta')).nativeElement;
expect(button.disabled).toBeFalse();
});

it('should call enrollCards on Continue button click', () => {
spyOn(component, 'enrollCards');
const button = fixture.debugElement.query(By.css('.connect-card__primary-cta')).nativeElement;
button.click();
expect(component.enrollCards).toHaveBeenCalled();
});
});
});

0 comments on commit a34b2a3

Please sign in to comment.