Currently, I am in the process of creating a custom client that integrates the robust SignalR
library:
export class RealTimeCommunicator {
/** SignalR hub connection */
private hubConnection: HubConnection | null = null;
private buildConnection(url: string): HubConnection {
return new HubConnectionBuilder()
.withUrl(url)
.build();
}
public subscribe(onStart: () => void, success: (data: IVehicleInfo) => void, fail?: (err: Error) => void): () => void {
this.hubConnection = this.buildConnection("/someurl");
this.hubConnection.on("send", data => {
success(data);
});
this.hubConnection.start()
.then(onStart)
.catch(err => fail && fail(err));
return this.unsubscribe;
}
public subscribeToVehicleDetails(vehicleId: number, success: (data: IVehicleInfo) => void, fail?: (err: Error) => void): () => void {
return this.subscribe(() => this.hubConnection!.invoke("EnrolInVehicleGroupAsync", vehicleId), success, fail);
}
To test this functionality, I need to mock out SignalR
, so I have introduced __mocks__/@aspnet/signalr.ts
into the codebase:
let SignalR = jest.genMockFromModule("@aspnet/signalr");
class HubConnection {
public on(methodName: string, callback: () => string): Promise<void> {
this.successCallback = callback;
return Promise.resolve()
}
public start(): Promise<void> { return Promise.resolve() }
public successCallback: () => string = () => "failure";
}
let hubConnection = new HubConnection();
let HubConnectionBuilder = () => ({
withUrl: (url) => ({
build: () => hubConnection
})
});
SignalR = {
HubConnectionBuilder: HubConnectionBuilder,
HubConnection: hubConnection
};
module.exports = SignalR;
In an effort to validate the functionality, I am trying to spy on the on
method to ensure its invocation, and ideally confirm the correct registration of my success handler:
import { RealTimeCommunicator } from "./RealTimeCommunicator";
jest.mock('SignalR');
describe("RTC", () => {
test("foo", () => {
const ConnectionBuilder = require("SignalR").HubConnectionBuilder;
const connection = new ConnectionBuilder().withUrl("").build();
const spy = jest.spyOn(connection, 'on');
expect(spy).toHaveBeenCalled(); //Expectation fails
const successText = "success!";
const successCallBack: () => string = () => successText;
const rtc = new RealTimeCommunicator();
rtc.subscribeToVehicleDetails(1, successCallBack, _ => _);
expect(connection.successCallback()).toBe(successText); // connection.successCallback is undefined
})
});
While debugging the SignalR
mock implementation, I can confirm that the on
method is called; however, the spy mechanism seems unable to detect it. Furthermore, the successCallback
addition to my mock appears as undefined in the test scenario.