1

I have a component that emit a value when call to a function, how can I test if the @output emit a value ? this is my function

@Output() emitDetail = new EventEmitter();

emitDetailFn() {
    this.emitDetail .emit(false);
}

and this is my test

  it('Detailfn should be called',()=>{

    let emitted: boolean;
    component.emitDetail .subscribe(value => {
      emitted = value
    })
    component.emitDetailFn();
    expect(emitted).toEqual(false)
})

but the coverage is red still in enter image description here

SirRiT003
  • 173
  • 1
  • 2
  • 6

1 Answers1

0

In your unit test, you invoke the function emitDetailFn, the reported code coverage however relates to the function emitDetailFnl.

Nevertheless, your unit test could be written as follows:

it('should emit detail false',() => {
  spyOn(component.emitDetail, 'emit');
  component.emitDetailFn();
  expect(component.emitDetail.emit).toHaveBeenCalledWith(false);
});

For further details, please consult spyOn and matchers from the Jasmine documentation.

uminder
  • 23,831
  • 5
  • 37
  • 72