How to mock EventSourcePolyfill messages in Cypress?

19 Views Asked by At

We use event-source-polyfill library for server sent events and I got the job to write Cypress tests. I tried to mock the messages, the EventSourcePolyfill event, the service itself, but can't figure it out. Does anyone have an example for it?

The service:

export class SseService {
    private eventSources = {};

    start(url: string, token: HttpHeaders, id: string): Observable<any> {
        this.eventSources[id]?.close();
        this.eventSources[id] = new EventSourcePolyfill(url,
            {
                headers: {
                    'x-api-token': token.get('x-api-token'),
                },
                heartbeatTimeout: this.fiveMinutes,
            });
        return new Observable(observer => {
            this.eventSources[id].onmessage = (event) => {
                observer.next(event.data);
            };
            this.eventSources[id].onerror = x => {
                observer.error(x);
            };
            return () => {
                this.eventSources[id].close();
            };
        });
    }

}
0

There are 0 best solutions below