I'm trying to test this method:
myMethod() {
result$ = combineLatest([observableOne$,observableTwo$]);
result$.subscribe(([one, two]) => {
this.result = one || two;
});
}
ngAfterViewInit() {
this.myMethod();
}
But every time the test fails:
it('should test my method', (): void => {
cmp.result$ = of([true, false]);
cmp.ngAfterViewInit();
expect(cmp.result).toBe(true);
});
The moment i add a setTimeOut everyhing works as expected
it('should test my method', (): void => {
cmp.result$ = of([true, false]);
cmp.ngAfterViewInit();
setTimeout(() => {
expect(cmp.result).toBe(true);
},1000)
});
Are there a solution to test without using the SetTimeOut ?
The problem was directly related the variable that contains the result of the combineLatest "result$" So instead of assiging the result to a variable, i just returned it directly to the method it self this solution works for me:
myMethod(observableOne$: Observable<boolean>, observableTwo$: Observable<boolean>): Observable<boolean> {
return combineLatest([observableOne$, observableTwo$]).pipe(
map(([one, two]) => this.result = one || two
));
};
ngAfterViewInit(): void {
const observableOne$: Observable<boolean> = of(true)
const observableTwo$: Observable<boolean> = of(false)
this.myMethod(observableOne$,observableTwo$).subscribe();
}
it('should test my method', (): void => {
cmp.myMethod(of(true), of(false)).subscribe(result => expect(cmp.result).toEqual(true));
});