All files / src/shared/services webSocket.service.mock.ts

85.71% Statements 12/14
33.33% Branches 1/3
100% Functions 6/6
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                15x     15x     1x         8x       8x     8x       8x   8x 25x 9x           8x    
import { Observable, of, Subject } from 'rxjs';
import { WebSocketSubjectConfig } from 'rxjs/webSocket';
import { WebSocketService } from './webSocket.service';
 
export class WebSocketServiceMock implements Partial<WebSocketService> {
    private _requestSubject?: Subject<MessageEvent>;
 
    public get requestSubject(): Subject<MessageEvent> {
        Iif (this._requestSubject === undefined) {
            return this.initRequestStream();
        }
        return this._requestSubject;
    }
 
    public constructor(private _responseSubject: Subject<MessageEvent>, private _desiredType: string) {
        
    }
 
    public createSocket(cfg: WebSocketSubjectConfig<MessageEvent>): Subject<MessageEvent> {
        return this.initRequestStream();
    };
 
    public getResponseStream(): Observable<MessageEvent> {
        Iif (this._responseSubject === undefined) {
            return of();
            }
        return this._responseSubject;
    }
 
    private initRequestStream(): Subject<MessageEvent> {
        this._requestSubject = new Subject<MessageEvent>();
        
        this._requestSubject.subscribe((msg) => {
            if (this._desiredType === msg.type) {
                this._responseSubject.next(msg);
                
                // console.error(`### ${msg?.data} ###`);
            }            
        });
 
        return this._requestSubject;
    }
}