From 44aa5b76d01960590288e40b1189e9bb1aa48f3c Mon Sep 17 00:00:00 2001 From: Viraj Trivedi Date: Wed, 6 Feb 2019 11:57:16 +0530 Subject: [PATCH] test(RealTimeAPI): Added tests for callMethod() and disconnect() methods #25 --- __tests__/RealTimeAPI.test.ts | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/__tests__/RealTimeAPI.test.ts b/__tests__/RealTimeAPI.test.ts index b5c4e19..62e1aa8 100644 --- a/__tests__/RealTimeAPI.test.ts +++ b/__tests__/RealTimeAPI.test.ts @@ -214,4 +214,53 @@ describe("RealTimeAPI tests", () => { }); }); }); + + it("can call api methods", done => { + const realtimeAPI$ = new RealTimeAPI(url); + + const method = "testMethod"; + const params = ["example-parameter"]; + + realtimeAPI$.callMethod(method, ...params).subscribe(); + + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. + + socket.on("message", data => { + let message = JSON.parse(data); + + expect(message).toHaveProperty("id"); // Expecting to have "id" property in message. + + expect(message).toHaveProperty("msg"); // Expecting to have "msg" property in message. + expect(message.msg).toEqual("method"); // Expecting "msg" to be "method" in message. + + expect(message).toHaveProperty("method"); // Expecting to have "method" property in message. + expect(message.method).toEqual(method); // Expecting "method" to be "testMethod" in message. + + expect(message).toHaveProperty("params"); // Expecting to have "params" property in message. + + expect(message.params).toEqual(params); //Expecting params to be [ "example-parameter" ]. + + done(); + }); + }); + }); + + it("can disconnect", done => { + const realtimeAPI$ = new RealTimeAPI(url); + + realtimeAPI$.keepAlive().subscribe(); + + mockServer.on("connection", (socket: WebSocket) => { + expect(socket.url).toEqual(url); // Expecting websocket url. Connection Successful. + + mockServer.on("close", (socket: WebSocket) => { + // Setting up Close Call listener + expect(socket.url).toEqual(url); // Expecting websocket url. Connection Closed. + done(); + }); + + realtimeAPI$.disconnect(); // Closing the connection. + }); + }); });