samedi 16 septembre 2017

Angular 4: Testing that a promise does not resolve

I have a spec in my Angular 4 app that is testing an EventEmitter(). I would like to make sure it is called using emit(), but don't know how to use expect() or assert() when the emitter is NOT called.

My production code looks like this:

public onSubmitPhrase() {
  this.submitPhrase.emit( this.currentPhrase );
  this.currentPhrase = '';
}

...and my spec for this looks like this:

it( `should call submitPhrase.emit('${testPhrase}')`, ( done ) => {
  component.currentPhrase = testPhrase;
  component.submitPhrase.subscribe( ( phrase: string ) => {
    expect( phrase ).toEqual( testPhrase );
    done();
  } );

  component.onSubmitPhrase();
} );

This will test if the correct event is emitted, but it will not fail is no event is emitted and I need it to. How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire