vendredi 25 mai 2018

Expected spy on log to equal 'Authentificated failed'

I need to test compare to value. I tried this exapmle:

I have this component.ts

export class AppComponent implements OnInit {
  title= 'Angular'
  constructor() { }
  ngOnInit() {
  }
}

And in component.spec.ts

it(`should have as title 'Angular'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('Angular');
}));

it(`should have as title 'Angular'`, async(() => {
  const fixture = TestBed.createComponent(AppComponent);
  const app = fixture.debugElement.componentInstance;
  expect(app.title).toEqual('Angular1');
}));

I follow this example that is simple. But now I need to compare my response value with some value

Step1. Html Component

<div id="container">
  <div id="login_card_container">
    <div id="login_card" class="card col s12">
      <form [formGroup]="loginForm" (ngSubmit)="onLogin()" class="col s12">
        <h1 id="form_title">Login</h1>
        <div class="row">
          <div class="input-field col s12">
            <input formControlName="username" id="username" type="text" class="validate" [ngClass]="{invalid: invalidCredentials}">
            <label for="username">Username</label>
          </div>
        </div>
        <div class="row">
          <div class="input-field col s12">
            <input formControlName="password" id="password" type="password" class="validate" [ngClass]="{invalid: invalidCredentials}">
            <label for="password" data-error="Your username/password combination is invalid">Password</label>
          </div>
        </div>
        <div id="login_button_container" class="row">
          <button id="login_button" type="submit" class="btn waves-effect waves-light" [disabled]="!loginForm.valid">
            <i class="fa fa-sign-in left"></i>
            Login
          </button>
        </div>
      </form>
    </div>
  </div>
</div>

Step 2: TS Component

  onLogin() {
    this.loading = true;
    this.ws.login(
      this.loginForm.controls['username'].value,
      this.loginForm.controls['password'].value)
      .subscribe(
      result => {
        if (result === true) {
        } else {
          this.loading = false;
          this.invalidCredentials = true;
        }
      });
  }

This is my first attempt,and show this error

Expected spy on log to equal 'Authentificated failed'.

In fact this is successful because response from service is this message Authentificated failed

  it('should notify in console on form submit', () => {
    spyOn(console, 'log');
    component.loginForm.controls['username'].setValue('admin');
    component.loginForm.controls['password'].setValue('11');
    fixture.debugElement.query(By.css('form')).triggerEventHandler('ngSubmit', null);
   fixture.detectChanges()
    expect(console.log).toEqual('Authentificated failed');
  });

In this code, I want toEqual service response with ('Authentificated failed') like in first example

Can you suggest my how to compare result form service, with some text?

Aucun commentaire:

Enregistrer un commentaire