I have implement component communication using emit emitter service.
service.ts
import { EventEmitter, Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class BroadcastService {
updateDetails = new EventEmitter();
constructor() { }
}
i am emitting an event from component 1 using
this.broadcastService.updateDetails.emit();
and i'm subscribing to that event in component 2 as follows
this.broadcastService.updateDetails.subscribe(() => {
// code to executed after receiving the event
});
Now my question is
when i am writing unit test cases for component 2, how should i mock the 'broadcastService.updateDetails' so that subscribe method is called and the code inside the subscribe() should be covered in code coverage.
In jasmine karma tests you will need to spy on the updateDetails.emit function and mock the returned value with the .and.returnValue() method.
Let's assume the subscribing to the event in your component 2 looks like this:
So a unittest for the updateDetails function could look like this.
the important part is that you actually create a correct eventMock which is returned in the and.returnValue(eventMock) part of the spy.