Here is my chat service:
import {webSocket, WebSocketSubject} from 'rxjs/webSocket';
import {delayWhen, retryWhen, take} from 'rxjs/operators';
import {timer} from 'rxjs';
...
export class ChatConnectionService {
private readonly _connection: WebSocketSubject<any>;
public readonly messages$: Observable<ChatInboundMessage>;
constructor() {
this._connection = this.getConnection();
this.messages$ = this._connection.asObservable();
}
private getConnection(): WebSocketSubject<any> {
return webSocket(`.../chat`);
}
}
And here is what I've tried so far:
import * as rxJsWebSocket from 'rxjs/webSocket';
describe('ChatConnectionService', () => {
let service: ChatConnectionService;
const subject = new Subject();
let webSocketSpy;
beforeEach(() => {
webSocketSpy = spyOnProperty(rxJsWebSocket, 'webSocket', 'get').and.returnValue(<any>subject);
service = new ChatConnectionService();
});
it('should create a new connection when service instantiated', () => {
expect(webSocketSpy).toHaveBeenCalledTimes(1);
});
});
But it gives me the following error:
Error: <spyOnProperty> : webSocket is not declared configurable
How to replace webSocket
with a Subject so I can test it? RxJs: 6.5.5