-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/transport-http/src/subscriptions/mocks/websocket.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import EventEmitter from "events" | ||
|
||
export function createMockWebSocket() { | ||
const eventEmitter = new EventEmitter() | ||
eventEmitter.on("message", (e) => websocket.onmessage(e)) | ||
eventEmitter.on("error", (e) => websocket.onerror(e)) | ||
eventEmitter.on("close", (e) => websocket.onclose(e)) | ||
eventEmitter.on("open", (e) => websocket.onopen(e)) | ||
|
||
const websocket = { | ||
onmessage: jest.fn(), | ||
onclose: jest.fn(), | ||
onopen: jest.fn(), | ||
onerror: jest.fn(), | ||
send: (data: any) => { | ||
connection.onmessage(new MessageEvent("message", { data })) | ||
}, | ||
readyState: 1, | ||
} | ||
|
||
const connection = { | ||
send: (data: any) => { | ||
eventEmitter.emit("message", new MessageEvent("message", { data: JSON.stringify(data) })) | ||
}, | ||
sendError: (error: Error) => { | ||
eventEmitter.emit("error", new ErrorEvent("error", { error })) | ||
}, | ||
close: () => { | ||
websocket.readyState = 3 | ||
eventEmitter.emit("close", new CloseEvent("close")) | ||
}, | ||
onmessage: jest.fn(), | ||
onclose: jest.fn(), | ||
} | ||
|
||
return { | ||
WebSocket: jest.fn(() => { | ||
console.log("NEW WEBSOCKET") | ||
setTimeout(() => { | ||
console.log("OPENING") | ||
eventEmitter.emit("open", new Event("open")) | ||
}, 1000) | ||
return websocket | ||
}), | ||
mockConnection: connection | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
packages/transport-http/src/subscriptions/stream-controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { createMockWebSocket } from "./mocks/websocket" | ||
import { SubscribeMessageResponse } from "./models" | ||
import { StreamController } from "./stream-controller" | ||
import { SubscriptionResponse, SubscriptionTopic } from "./types" | ||
|
||
let mockWs: ReturnType<typeof createMockWebSocket> | ||
|
||
jest.mock("../websocket", () => ({ | ||
WebSocket: jest.fn().mockImplementation(() => mockWs.WebSocket), | ||
})) | ||
|
||
describe("StreamController", () => { | ||
beforeEach(() => { | ||
mockWs = createMockWebSocket() | ||
}) | ||
|
||
test("constructor", () => { | ||
const config = { | ||
hostname: "hostname", | ||
reconnectInterval: 1000, | ||
reconnectAttempts: 10, | ||
} | ||
const streamController = new StreamController(config) | ||
expect(streamController).toBeDefined() | ||
}) | ||
|
||
test("subscribe sends subscribe message", async () => { | ||
const config = { | ||
hostname: "hostname", | ||
reconnectInterval: 1000, | ||
reconnectAttempts: 10, | ||
} | ||
const streamController = new StreamController(config) | ||
const topic = "topic" as SubscriptionTopic | ||
const args = { key: "value" } as any | ||
const onData = jest.fn() | ||
const onError = jest.fn() | ||
|
||
console.log("YO") | ||
|
||
mockWs.mockConnection.onmessage.mockImplementation((event) => { | ||
console.log(event) | ||
const data = JSON.parse(event.data) | ||
expect(data).toEqual({ | ||
topic, | ||
arguments: args, | ||
}) | ||
|
||
const response: SubscribeMessageResponse = { | ||
id: "id", | ||
action: "subscribe", | ||
success: true, | ||
topic, | ||
} | ||
mockWs.mockConnection.send(response) | ||
}) | ||
|
||
const subscription = await streamController.subscribe({ | ||
topic, | ||
args, | ||
onData, | ||
onError, | ||
}) | ||
|
||
expect(mockWs.mockConnection.onmessage).toHaveBeenCalledTimes(1) | ||
|
||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/transport-http/src/subscriptions/subscription-manager.test.ts
This file was deleted.
Oops, something went wrong.