-
Notifications
You must be signed in to change notification settings - Fork 15
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
test: 100% coverage of virtual-select component #2500
Changes from all commits
56b2cf7
81fc9b1
358ff2c
2573026
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing'; | ||
import { IonicModule, ModalController } from '@ionic/angular'; | ||
|
||
import { Injector, NO_ERRORS_SCHEMA } from '@angular/core'; | ||
import { VirtualSelectComponent } from './virtual-select.component'; | ||
import { ModalPropertiesService } from 'src/app/core/services/modal-properties.service'; | ||
import { FormControl, NG_VALUE_ACCESSOR, NgControl } from '@angular/forms'; | ||
import { properties } from 'src/app/core/mock-data/modal-properties.data'; | ||
import { virtualSelectModalControllerParams } from 'src/app/core/mock-data/modal-controller.data'; | ||
|
||
describe('VirtualSelectModalComponent', () => { | ||
let component: VirtualSelectComponent; | ||
let fixture: ComponentFixture<VirtualSelectComponent>; | ||
let modalController: jasmine.SpyObj<ModalController>; | ||
let modalProperties: jasmine.SpyObj<ModalPropertiesService>; | ||
|
||
beforeEach(waitForAsync(() => { | ||
const modalControllerSpy = jasmine.createSpyObj('ModalController', ['create']); | ||
const modalPropertiesServiceSpy = jasmine.createSpyObj('ModalPropertiesService', ['getModalDefaultProperties']); | ||
const injectorSpy = jasmine.createSpyObj('Injector', ['get']); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [VirtualSelectComponent], | ||
imports: [IonicModule.forRoot()], | ||
providers: [ | ||
{ | ||
provide: ModalController, | ||
useValue: modalControllerSpy, | ||
}, | ||
{ | ||
provide: ModalPropertiesService, | ||
useValue: modalPropertiesServiceSpy, | ||
}, | ||
{ | ||
provide: Injector, | ||
useValue: injectorSpy, | ||
}, | ||
{ | ||
provide: NgControl, | ||
useValue: { | ||
control: new FormControl(), | ||
}, | ||
}, | ||
], | ||
schemas: [NO_ERRORS_SCHEMA], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(VirtualSelectComponent); | ||
component = fixture.componentInstance; | ||
modalController = TestBed.inject(ModalController) as jasmine.SpyObj<ModalController>; | ||
modalProperties = TestBed.inject(ModalPropertiesService) as jasmine.SpyObj<ModalPropertiesService>; | ||
component.enableSearch = true; | ||
fixture.debugElement.injector.get(NG_VALUE_ACCESSOR); | ||
fixture.detectChanges(); | ||
})); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
describe('openModal():', () => { | ||
let selectionModalSpy: jasmine.SpyObj<HTMLIonModalElement>; | ||
beforeEach(() => { | ||
selectionModalSpy = jasmine.createSpyObj('selectionModal', ['present', 'onWillDismiss']); | ||
selectionModalSpy.onWillDismiss.and.resolveTo({ | ||
data: { | ||
value: 'value', | ||
}, | ||
}); | ||
modalProperties.getModalDefaultProperties.and.returnValue(properties); | ||
modalController.create.and.resolveTo(selectionModalSpy); | ||
}); | ||
|
||
it('should open select modal and set value equals to value returned by modal', fakeAsync(() => { | ||
component.openModal(); | ||
tick(100); | ||
|
||
expect(modalController.create).toHaveBeenCalledOnceWith(virtualSelectModalControllerParams); | ||
expect(modalProperties.getModalDefaultProperties).toHaveBeenCalledOnceWith('virtual-modal'); | ||
expect(selectionModalSpy.present).toHaveBeenCalledTimes(1); | ||
expect(selectionModalSpy.onWillDismiss).toHaveBeenCalledTimes(1); | ||
})); | ||
|
||
it('should open select modal and set value equals to value returned by modal if label is Payment Mode', fakeAsync(() => { | ||
component.label = 'Payment Mode'; | ||
component.openModal(); | ||
tick(100); | ||
|
||
expect(modalController.create).toHaveBeenCalledOnceWith({ | ||
...virtualSelectModalControllerParams, | ||
componentProps: { ...virtualSelectModalControllerParams.componentProps, label: 'Payment Mode' }, | ||
}); | ||
expect(modalProperties.getModalDefaultProperties).toHaveBeenCalledOnceWith('payment-mode-modal'); | ||
expect(selectionModalSpy.present).toHaveBeenCalledTimes(1); | ||
expect(selectionModalSpy.onWillDismiss).toHaveBeenCalledTimes(1); | ||
})); | ||
}); | ||
|
||
describe('get valid():', () => { | ||
beforeEach(() => { | ||
//@ts-ignore | ||
component.ngControl.touched = true; | ||
//@ts-ignore | ||
component.ngControl.valid = true; | ||
}); | ||
|
||
it('should return true if control is touched and valid', () => { | ||
expect(component.valid).toBeTrue(); | ||
}); | ||
|
||
it('should return false if control is touched and invalid', () => { | ||
//@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(); | ||
}); | ||
}); | ||
|
||
describe('set value():', () => { | ||
beforeEach(() => { | ||
//@ts-ignore | ||
component.innerValue = 'ECONOMY'; | ||
component.options = [ | ||
{ label: 'Business', value: 'BUSINESS' }, | ||
{ label: 'Economy', value: 'ECONOMY' }, | ||
{ label: 'First Class', value: 'FIRST_CLASS' }, | ||
]; | ||
//@ts-ignore | ||
spyOn(component, 'onChangeCallback'); | ||
}); | ||
|
||
it('should set the value and update the displayValue if any "option.value" is equal to innerValue', () => { | ||
component.value = 'BUSINESS'; | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual('BUSINESS'); | ||
expect(component.displayValue).toEqual('Business'); | ||
//@ts-ignore | ||
expect(component.onChangeCallback).toHaveBeenCalledOnceWith('BUSINESS'); | ||
}); | ||
|
||
it('should set the value and update the displayValue if type of innerValue is string', () => { | ||
component.value = ''; | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual(''); | ||
expect(component.displayValue).toEqual(''); | ||
//@ts-ignore | ||
expect(component.onChangeCallback).toHaveBeenCalledOnceWith(''); | ||
}); | ||
|
||
it('should set the value and update the displayValue if innerValue and defaultLabelProps is defined', () => { | ||
component.defaultLabelProp = 'vendor'; | ||
component.value = { travelClass: 'BUSINESS', vendor: 'vendor1' }; | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
expect(component.displayValue).toEqual('vendor1'); | ||
//@ts-ignore | ||
expect(component.onChangeCallback).toHaveBeenCalledOnceWith({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added one line space, don't know why github not showing as resolved |
||
it('should set the value and update the displayValue if none of the conditions holds true', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, fix it for all |
||
component.defaultLabelProp = ''; | ||
component.value = { travelClass: 'BUSINESS', vendor: 'vendor1' }; | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
expect(component.displayValue).toEqual(''); | ||
//@ts-ignore | ||
expect(component.onChangeCallback).toHaveBeenCalledOnceWith({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
}); | ||
}); | ||
|
||
it('onBlur(): should call onTouchedCallback once', () => { | ||
//@ts-ignore | ||
spyOn(component, 'onTouchedCallback'); | ||
component.onBlur(); | ||
//@ts-ignore | ||
expect(component.onTouchedCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
describe('writeValue(): ', () => { | ||
beforeEach(() => { | ||
//@ts-ignore | ||
component.innerValue = 'ECONOMY'; | ||
component.options = [ | ||
{ label: 'Business', value: 'BUSINESS' }, | ||
{ label: 'Economy', value: 'ECONOMY' }, | ||
{ label: 'First Class', value: 'FIRST_CLASS' }, | ||
]; | ||
}); | ||
|
||
it('should set the value and update the displayValue if any "option.value" is equal to innerValue', () => { | ||
component.writeValue('BUSINESS'); | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual('BUSINESS'); | ||
expect(component.displayValue).toEqual('Business'); | ||
}); | ||
|
||
it('should set the value and update the displayValue if type of innerValue is string', () => { | ||
component.writeValue(''); | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual(''); | ||
expect(component.displayValue).toEqual(''); | ||
}); | ||
|
||
it('should set the value and update the displayValue if innerValue and defaultLabelProps is defined', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one line gap |
||
component.defaultLabelProp = 'vendor'; | ||
component.writeValue({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
expect(component.displayValue).toEqual('vendor1'); | ||
}); | ||
|
||
it('should set the value and update the displayValue if none of the conditions holds true', () => { | ||
component.defaultLabelProp = ''; | ||
component.writeValue({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
//@ts-ignore | ||
expect(component.innerValue).toEqual({ travelClass: 'BUSINESS', vendor: 'vendor1' }); | ||
expect(component.displayValue).toEqual(''); | ||
}); | ||
}); | ||
|
||
it('registerOnChange(): should set onChangeCallback', () => { | ||
const onChange = (): void => {}; | ||
component.registerOnChange(onChange); | ||
//@ts-ignore | ||
expect(component.onChangeCallback).toEqual(onChange); | ||
}); | ||
|
||
it('registerOnTouched(): should set onTouchedCallback', () => { | ||
const onTouched = (): void => {}; | ||
component.registerOnTouched(onTouched); | ||
//@ts-ignore | ||
expect(component.onTouchedCallback).toEqual(onTouched); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one line space between each it()