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 have added a couple of console.log
in order to figure out why this fails, and it turns out that 1
is never printed, so it doesn't wait for the promise to be resolved, despite being using fixture.whenStable()
. This is my 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 am doing wrong here?