I have the following component method:
onSignup() {
if (this.form.valid) {
let email = this.form.value.email;
let password = this.form.value.password;
this.usersService.signup(email, password)
.then(() => {
console.log('1')
this.router.navigate(['/app/home']);
})
.catch(err => {
this.mdlSnackbarService.showToast(err);
});
}
}
And the following unit test:
it('should navigate on success', async(() => {
spyOn(usersService, 'signup').and.returnValue(Promise.resolve());
component.form.setValue({'email': 'mail@mail.com', 'password': '123456'});
component.onSignup();
fixture.whenStable().then(() => {
console.log('2')
expect(router.navigate).toHaveBeenCalledWith(['/app/home']);
})
}))
I added a couple console.logto understand why this fails, and it turns out that it is 1never printed, so it does not wait for the promise to be resolved, despite the fact that it uses fixture.whenStable(). This is mine beforeEach:
beforeEach(inject([Router],(_router: Router) => {
router = _router;
router.navigate = jasmine.createSpy('navigate');
fixture = TestBed.createComponent(SignupComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
Does anyone know what I'm doing wrong here?
source
share