|
| 1 | +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. |
| 2 | +// See LICENSE.txt for license information. |
| 3 | + |
| 4 | +import { getOrCreateWebSocketClient } from "../index"; |
| 5 | +import NativeWebSocketClient from "../NativeWebSocketClient"; |
| 6 | + |
| 7 | +const mockNativeClient = NativeWebSocketClient as jest.Mocked< |
| 8 | + typeof NativeWebSocketClient |
| 9 | +>; |
| 10 | + |
| 11 | +jest.mock("../NativeWebSocketClient", () => ({ |
| 12 | + __esModule: true, |
| 13 | + default: { |
| 14 | + addListener: jest.fn(), |
| 15 | + removeListeners: jest.fn(), |
| 16 | + ensureClientFor: jest.fn().mockResolvedValue(undefined), |
| 17 | + connectFor: jest.fn().mockResolvedValue(undefined), |
| 18 | + disconnectFor: jest.fn().mockResolvedValue(undefined), |
| 19 | + sendDataFor: jest.fn().mockResolvedValue(undefined), |
| 20 | + sendBinaryDataFor: jest.fn().mockResolvedValue(undefined), |
| 21 | + invalidateClientFor: jest.fn().mockResolvedValue(undefined), |
| 22 | + }, |
| 23 | + WebSocketReadyState: { CONNECTING: 0, OPEN: 1, CLOSING: 2, CLOSED: 3 }, |
| 24 | + WebSocketEvents: { |
| 25 | + OPEN_EVENT: "WebSocketClient-Open", |
| 26 | + CLOSE_EVENT: "WebSocketClient-Close", |
| 27 | + ERROR_EVENT: "WebSocketClient-Error", |
| 28 | + MESSAGE_EVENT: "WebSocketClient-Message", |
| 29 | + READY_STATE_EVENT: "WebSocketClient-ReadyState", |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +// Each test uses a unique URL to avoid the module-level CLIENTS singleton state. |
| 34 | +let urlCounter = 0; |
| 35 | +const nextUrl = () => |
| 36 | + `ws://mattermost.example.com/api/v4/websocket?t=${++urlCounter}`; |
| 37 | + |
| 38 | +describe("WebSocketClient", () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should call sendDataFor when send() is invoked", async () => { |
| 44 | + const url = nextUrl(); |
| 45 | + const { client } = await getOrCreateWebSocketClient(url); |
| 46 | + |
| 47 | + client.send("hello"); |
| 48 | + |
| 49 | + expect(mockNativeClient.sendDataFor).toHaveBeenCalledWith(url, "hello"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should call sendBinaryDataFor when sendBinary() is invoked", async () => { |
| 53 | + const url = nextUrl(); |
| 54 | + const { client } = await getOrCreateWebSocketClient(url); |
| 55 | + const base64Data = "SGVsbG8gV29ybGQ="; |
| 56 | + |
| 57 | + client.sendBinary(base64Data); |
| 58 | + |
| 59 | + expect(mockNativeClient.sendBinaryDataFor).toHaveBeenCalledWith( |
| 60 | + url, |
| 61 | + base64Data, |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should pass the exact url and data to sendBinaryDataFor", async () => { |
| 66 | + const url = nextUrl(); |
| 67 | + const { client } = await getOrCreateWebSocketClient(url); |
| 68 | + const encoded = "dGVzdA=="; |
| 69 | + |
| 70 | + client.sendBinary(encoded); |
| 71 | + |
| 72 | + expect(mockNativeClient.sendBinaryDataFor).toHaveBeenCalledTimes(1); |
| 73 | + expect(mockNativeClient.sendBinaryDataFor).toHaveBeenCalledWith( |
| 74 | + url, |
| 75 | + encoded, |
| 76 | + ); |
| 77 | + }); |
| 78 | +}); |
0 commit comments