mercredi 6 juin 2018

Angular 5 Unit test Router Navigation

I want to test this function. When return true I want to navigate in this router.

My component code:

  onLogin() {
    this.loading = true;
    this.auth.login(
      this.loginForm.controls['username'].value,
      this.loginForm.controls['password'].value)
      .subscribe(
      result => {
        if (result === true) {
          this.router.navigate(['/home']);
        } else {
          this.loading = false;
        }
      }
      );
  }

I tried this test, that result is true

  it('should call service.login when onLogin', done => {
    const mock = [];
    spyOn(component['auth'], 'login').and.callThrough()
    component['auth'].login('abcs', '1').subscribe(
      result => {
        if (result === true) {
        }
        console.log(result)
          done();
      });
    component.onLogin();
  })

Aucun commentaire:

Enregistrer un commentaire