@Injectable({
providedIn: 'root'
})
export class MyResolver implements Resolve<LoggedUserProfile> {
constructor(private readonly _store: Store<AppState>) {}
resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<LoggedUserProfile> {
return this._store.pipe(
select(selectLoggedUserProfile),
filter(profile => profile && !!profile.id),
take(1)
);
}
}
I want to test that the route is not resolved when the profile is not a valid object. For that, I am checking that the function take is not called.
import * as rxjsOperatores from 'rxjs/operators';
it('should not resolve the route if profile is not valid', () => {
const setPropertyTypeSpy = jasmine.createSpy('take');
const takeSpyOn = spyOnProperty(rxjsOperatores, 'take').and.returnValue(
setPropertyTypeSpy
);
mockStore.overrideSelector(selectLoggedUserProfile, null);
service.resolve(route, null).subscribe(() => {
expect(takeSpyOn).not.toHaveBeenCalled();
done();
});
});
Spying on "take" has been a challenge for hours now.
First, I tried to spy on the take method like this
const takeSpyOn = spyOn(rxjsOperatores, 'take');
I got an error saying:
Error: <spyOn> : take is not declared writable or has no setter
Then I tried the below technique as suggested by certain people online
const setPropertyTypeSpy = jasmine.createSpy('take');
const takeSpyOn = spyOnProperty(rxjsOperatores, 'take').and.returnValue(
setPropertyTypeSpy
);
Then I get the following error:
TypeError: fn is not a function
Is there a way to spy on the rxjs operators?
I've tried this technique and this other too but didn't work either.
Thanks for helping