Skip to content

Commit

Permalink
test: fy-currency component inside add-edit-adv-request page (#2517)
Browse files Browse the repository at this point in the history
* test: fy-currency component inside add-edit-adv-request page

* pr comments
  • Loading branch information
suyashpatil78 authored Oct 17, 2023
1 parent 2b8a3c2 commit 05b805d
Showing 1 changed file with 154 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,166 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
import { IonicModule, ModalController } from '@ionic/angular';

import { FyCurrencyComponent } from './fy-currency.component';
import { FormBuilder, FormControl, FormGroup, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms';
import { ModalPropertiesService } from 'src/app/core/services/modal-properties.service';
import { Injector, NO_ERRORS_SCHEMA } from '@angular/core';
import { FyCurrencyChooseCurrencyComponent } from './fy-currency-choose-currency/fy-currency-choose-currency.component';
import { properties } from 'src/app/core/mock-data/modal-properties.data';

xdescribe('FyCurrencyComponent', () => {
describe('FyCurrencyComponent', () => {
let component: FyCurrencyComponent;
let fixture: ComponentFixture<FyCurrencyComponent>;
let formBuilder: FormBuilder;
let modalController: jasmine.SpyObj<ModalController>;
let modalProperties: jasmine.SpyObj<ModalPropertiesService>;

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [FyCurrencyComponent],
imports: [IonicModule.forRoot()],
}).compileComponents();
const mockCurrency = {
amount: 100,
currency: 'USD',
};

fixture = TestBed.createComponent(FyCurrencyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
);
beforeEach(waitForAsync(() => {
const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create']);
const modalPropertiesSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']);
const injectorSpy = jasmine.createSpyObj('Injector', ['get']);

TestBed.configureTestingModule({
declarations: [FyCurrencyComponent],
imports: [IonicModule.forRoot()],
providers: [
FormBuilder,
{ provide: ModalController, useValue: modalControllerSpy },
{ provide: ModalPropertiesService, useValue: modalPropertiesSpy },
{ provide: Injector, useValue: injectorSpy },
{
provide: NgControl,
useValue: {
control: new FormControl(),
},
},
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();

fixture = TestBed.createComponent(FyCurrencyComponent);
component = fixture.componentInstance;
formBuilder = TestBed.inject(FormBuilder);
modalController = TestBed.inject(ModalController) as jasmine.SpyObj<ModalController>;
modalProperties = TestBed.inject(ModalPropertiesService) as jasmine.SpyObj<ModalPropertiesService>;
fixture.debugElement.injector.get(NG_VALUE_ACCESSOR);
fixture.detectChanges();
}));

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

it('should set value if form value changes', () => {
component.fg.setValue(mockCurrency);
expect(component.value).toEqual(mockCurrency);
});

describe('get valid():', () => {
it('should return true if control is touched and valid', () => {
//@ts-ignore
component.ngControl.touched = true;
//@ts-ignore
component.ngControl.valid = true;
expect(component.valid).toBeTrue();
});

it('should return false if control is touched and invalid', () => {
//@ts-ignore
component.ngControl.touched = true;
//@ts-ignore
component.ngControl.valid = false;
expect(component.valid).toBeFalse();
});

it('should return true if control is untouched', () => {
//@ts-ignore
component.ngControl.touched = false;
//@ts-ignore
component.ngControl.valid = false;
expect(component.valid).toBeTrue();
});
});

it('get value(): should return innerValue', () => {
//@ts-ignore
component.innerValue = mockCurrency;
const expectedValue = component.value;
expect(expectedValue).toEqual(mockCurrency);
});

it('set value(): should set innerValue and call onChangeCallback', () => {
const formValueSpy = spyOn(component, 'convertInnerValueToFormValue').and.returnValue(mockCurrency);
//@ts-ignore
const onChangeCallbackSpy = spyOn(component, 'onChangeCallback');
component.value = mockCurrency;
expect(component.value).toEqual(mockCurrency);
expect(formValueSpy).toHaveBeenCalledOnceWith(mockCurrency);
expect(onChangeCallbackSpy).toHaveBeenCalledTimes(1);
});

it('convertInnerValueToFormValue(): should return amount as null and currency as homeCurrency if inner value is not defined', () => {
component.homeCurrency = 'INR';

const expectedFormValue = component.convertInnerValueToFormValue(undefined);

expect(expectedFormValue).toEqual({
amount: null,
currency: 'INR',
});
});

it('onBlur(): should call onTouchedCallback', () => {
//@ts-ignore
const onTouchedCallbackSpy = spyOn(component, 'onTouchedCallback');
component.onBlur();
expect(onTouchedCallbackSpy).toHaveBeenCalledTimes(1);
});

it('writeValue(): should set innerValue and form value', () => {
const formValueSpy = spyOn(component, 'convertInnerValueToFormValue').and.returnValue(mockCurrency);
component.writeValue(mockCurrency);
expect(formValueSpy).toHaveBeenCalledOnceWith(mockCurrency);
expect(component.fg.value).toEqual(mockCurrency);
});

it('registerOnChange(): should set onChangeCallback', () => {
const mockFn = () => {};
component.registerOnChange(mockFn);
//@ts-ignore
expect(component.onChangeCallback).toEqual(mockFn);
});

it('registerOnTouched(): should set onTouchedCallback', () => {
const mockFn = () => {};
component.registerOnTouched(mockFn);
//@ts-ignore
expect(component.onTouchedCallback).toEqual(mockFn);
});

it('openCurrencyModal(): should open currency modal', fakeAsync(() => {
const currencyModalSpy = jasmine.createSpyObj('currencyModal', ['present', 'onWillDismiss']);
currencyModalSpy.onWillDismiss.and.resolveTo({
data: { currency: { shortCode: 'USD', longName: 'United States Dollar' } },
});
modalController.create.and.resolveTo(currencyModalSpy);
modalProperties.getModalDefaultProperties.and.returnValue(properties);
component.openCurrencyModal();
tick(100);
expect(modalController.create).toHaveBeenCalledOnceWith({
component: FyCurrencyChooseCurrencyComponent,
componentProps: {
currentSelection: null,
},
mode: 'ios',
...properties,
});
expect(currencyModalSpy.present).toHaveBeenCalledTimes(1);
expect(currencyModalSpy.onWillDismiss).toHaveBeenCalledTimes(1);
}));
});

0 comments on commit 05b805d

Please sign in to comment.