Im using Cypress.io as part of the way that Im testing an Angular 7 app. I've combed through the docs, forums and blogs, but I dont see a way to mock or stub a function in Angular. Im trying to mock the employee Observable. I need to have the Employee array contain an employee object so that this.federalTaxesService.loadFederalTaxes() function is called. In production, this app gets the Employee array from the NgRx store, but the store is not working correctly when using Cypress for some reason.
export class EmployeeTaxDetailsContainerComponent implements OnInit, OnDestroy {
watchEmployee$: Subscription;
watchFederalTaxes$: Subscription;
federalTaxes$: Observable<any> = this.federalTaxesService.federalTaxes$;
employees$: Observable<any> = this.employeesService.employees$;
federalTaxes: FederalTaxes;
employeeUuid: string;
constructor(public employeesService: EmployeesFacade, public federalTaxesService: FederalTaxesFacade) {}
ngOnInit() {
this.watchEmployee$ = this.employees$.subscribe((employees: Employee[]) => {
if (employees && employees.length > 0) {
this.employeeUuid = employees[0].uuid;
this.federalTaxesService.loadFederalTaxes(this.employeeUuid);
}
});
this.watchFederalTaxes$ = this.federalTaxes$.subscribe((federalTaxes: FederalTaxes) => {
if (federalTaxes) {
this.federalTaxes = federalTaxes;
}
});
}
ngOnDestroy() {
if (this.watchFederalTaxes$) {
this.watchFederalTaxes$.unsubscribe();
}
if (this.watchEmployee$) {
this.watchEmployee$.unsubscribe();
}
}
}