I have troubles with mocking popup data for unit testing. Getting an error like: TypeError: Cannot read property 'showFilterModal' of undefined
What am I doing wrong?
FilterComponent
public openFilterModal() {
this.filterModal = this.modalService.showFilterModal(this.filtersOptions, this.userRole, 'bills');
this.filterModal.afterClosed
.subscribe(
(result) => {
this.selectedFiltersAmount = result.total;
this.filtersOptions = result.filters;
},
(err) => {
});
}
The idea was to create fake service with necessary observables and functions. Much thanks @Naren
Correct test mocking:
describe('Component', () => {
let component: Component;
let fixture: ComponentFixture<Component>;
const mockModelService = {
showFilterModal:
jasmine.createSpy('showFilterModal').and.returnValue({
afterClosed: of({
total: 1,
filters: null,
reqObj: {
status: 'active',
}
})})
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NecessaryModules],
providers: [
{ provide: LocalModalService, useValue: mockModelService }
],
declarations: [ Component]
}).compileComponents();
fixture = TestBed.createComponent(Component);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create subscription after close modal', () => {
component.openFilterModal();
expect(mockModelService.showFilterModal).toHaveBeenCalled();
expect(component.selectedFiltersAmount).toBe(1);
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…