How can I test if a @output emit a value?

1k Views Asked by At

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

1

There are 1 best solutions below

0
uminder On

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.