Let's suppose I have the following component:
@Component({
selector: 'app-dumb',
template: '<button (click)="increment()">Increment</button>'
})
export class DumbComponent {
@Output() onIncrement = new EventEmitter<void>();
increment() {
this.onIncrement.emit();
}
}
I am using Angular Testing Library, and my objective is to click on the button and assert the given output function has been invoked.
The project is using karma/jasmine, and it seems that is not straightforward to add jest.
The following describes the only way I was able to check what I needed, but I'd like avoid to spy on the componentInstance
and instead inject the thing
I want to spy on.
it("emits an event when the increment button is clicked", async () => {
const { fixture } = await render(DumbComponent);
spyOn(fixture.componentInstance.onIncrement, 'emit');
await clickIncrementButton();
expect(fixture.componentInstance.onIncrement.emit).toHaveBeenCalledTimes(1);
})
I tried using jasmine.createSpy
but It doesn't seem to be a valid type to be injected.
const onIncrement = createSpy();
await render(DumbComponent, {
componentProperties: {
onIncrement: onIncrement
}
})
Any idea?