Imagine a straightforward class that triggers a new event to an RxJS subject whenever the window is resized. Disregard any perceived complexities, as the main point is that this class generates an event.
export class ResizeService {
private resizeSubject = new Subject();
public onResize(): Observable<Window> {
return this.resizeSubject.asObservable();
}
constructor(private eventManager: EventManager) {
this.eventManager.addGlobalEventListener('window', 'resize', (e) => {
this.resizeObject.next(<Window>event.target)
})
}
private onResize(event: UIEvent) {
this.resizeObject.next(<Window>event.target);
}
}
The question at hand revolves around whether we should verify in our unit tests that the newly emitted event sent to the RxJS subject will indeed be received by the client calling the onResize
method. Consider the following example:
it('should emit a value', fakeAsync(() => {
let subscriptionWorks = false;
fireWindowResizeEvent(new Event({width: 600; height: 400});
resizeService.onResize().subscribe(() => subscriptionWorks = true);
expect(subscriptionWorks).toBeTruthy();
})
)
In the scenario where a developer alters the onResizeMethod
to include additional logic like skipping elements, the test would fail:
public onResize(): Observable<Window> {
return this.resizeSubject.asObservable().skip(10);
}