0

trying to test the following angular component code with jest. It takes an input of a label within the html for a label to a mat-card, and outputs an event emitter. Here is what it looks like. code coverage is already 80+ as is but I am curious how to actually test this portion.

export class KpiContainerComponent {
  @Input() label: string;
  @Output() handleClick: EventEmitter<any> = new EventEmitter();

  public emitClick() {
    this.handleClick.emit();
  }
}

1 Answers1

-1

I think I got it, I was doing my spy incorrectly I believe. This is the test I created to get to 100% code coverage and passing tests.

it('should emit', () =>{
  let spy = jest.spyOn(component, 'emitClick');
  component.emitClick();
  expect(spy).toHaveBeenCalled();
})
  • That test may _pass_ but it's useless; there's nothing expressed there about the behaviour of the implementation, you're just asserting that the spy you _literally just called_ got called. See e.g. https://stackoverflow.com/a/62871934/3001761 for how to actually test @Outputs. – jonrsharpe Oct 11 '22 at 13:00
  • Okay I see now my test works but has no substance or purpose, I guess im still confused as to how to test because the emit() documentation and examples all have a value being passed to emit, such as emit('clicked!'), however in this code there is no value being passed so it defaults to undefined. In which case would it be appropriate to test it by expecting my spy to be undefined? @jonrsharpe – Nikosuave09 Oct 11 '22 at 16:26