-
Notifications
You must be signed in to change notification settings - Fork 117
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
5 changed files
with
130 additions
and
122 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
47 changes: 0 additions & 47 deletions
47
packages/transport-http/src/subscriptions/mocks/websocket.ts
This file was deleted.
Oops, something went wrong.
141 changes: 86 additions & 55 deletions
141
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 |
---|---|---|
@@ -1,69 +1,100 @@ | ||
import { createMockWebSocket } from "./mocks/websocket" | ||
import { SubscribeMessageResponse } from "./models" | ||
import { StreamController } from "./stream-controller" | ||
import { SubscriptionResponse, SubscriptionTopic } from "./types" | ||
|
||
let mockWs: ReturnType<typeof createMockWebSocket> | ||
import WS from "jest-websocket-mock" | ||
import {WebSocket as mockSocket} from "mock-socket" | ||
import { | ||
SubscribeMessageRequest, | ||
SubscribeMessageResponse, | ||
SubscriptionDataMessage, | ||
UnsubscribeMessageRequest, | ||
} from "./models" | ||
import {StreamController} from "./stream-controller" | ||
import {SubscriptionTopic} from "./types" | ||
|
||
jest.mock("../websocket", () => ({ | ||
WebSocket: jest.fn().mockImplementation(() => mockWs.WebSocket), | ||
WebSocket: mockSocket, | ||
})) | ||
|
||
describe("StreamController", () => { | ||
beforeEach(() => { | ||
mockWs = createMockWebSocket() | ||
}) | ||
test("constructor", () => { | ||
const config = { | ||
hostname: "hostname", | ||
reconnectInterval: 1000, | ||
reconnectAttempts: 10, | ||
} | ||
const streamController = new StreamController(config) | ||
expect(streamController).toBeDefined() | ||
}) | ||
|
||
test("subscribes, receives data, and unsubscribes", async () => { | ||
const config = { | ||
hostname: "wss://localhost:8080", | ||
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() | ||
|
||
const mockWs = new WS("wss://localhost:8080") | ||
|
||
let serverPromise = (async () => { | ||
await mockWs.connected | ||
|
||
const msg = (await mockWs.nextMessage) as string | ||
const data = JSON.parse(msg) as SubscribeMessageRequest | ||
expect(data).toEqual({ | ||
action: "subscribe", | ||
topic, | ||
arguments: args, | ||
}) | ||
|
||
test("constructor", () => { | ||
const config = { | ||
hostname: "hostname", | ||
reconnectInterval: 1000, | ||
reconnectAttempts: 10, | ||
} | ||
const streamController = new StreamController(config) | ||
expect(streamController).toBeDefined() | ||
}) | ||
const response: SubscribeMessageResponse = { | ||
id: "id", | ||
action: "subscribe", | ||
success: true, | ||
topic, | ||
} | ||
mockWs.send(JSON.stringify(response)) | ||
})() | ||
|
||
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() | ||
const [subscription] = await Promise.all([ | ||
streamController.subscribe({ | ||
topic, | ||
args, | ||
onData, | ||
onError, | ||
}), | ||
serverPromise, | ||
]) | ||
|
||
console.log("YO") | ||
expect(subscription).toBeDefined() | ||
expect(subscription.unsubscribe).toBeInstanceOf(Function) | ||
|
||
mockWs.mockConnection.onmessage.mockImplementation((event) => { | ||
console.log(event) | ||
const data = JSON.parse(event.data) | ||
expect(data).toEqual({ | ||
topic, | ||
arguments: args, | ||
}) | ||
serverPromise = (async () => { | ||
const data = { | ||
id: "id", | ||
data: {key: "value"}, | ||
} as SubscriptionDataMessage | ||
mockWs.send(JSON.stringify(data)) | ||
})() | ||
|
||
const response: SubscribeMessageResponse = { | ||
id: "id", | ||
action: "subscribe", | ||
success: true, | ||
topic, | ||
} | ||
mockWs.mockConnection.send(response) | ||
}) | ||
await serverPromise | ||
|
||
const subscription = await streamController.subscribe({ | ||
topic, | ||
args, | ||
onData, | ||
onError, | ||
}) | ||
expect(onData).toHaveBeenCalledTimes(1) | ||
expect(onData).toHaveBeenCalledWith({key: "value"}) | ||
expect(onError).toHaveBeenCalledTimes(0) | ||
|
||
expect(mockWs.mockConnection.onmessage).toHaveBeenCalledTimes(1) | ||
serverPromise = (async () => { | ||
const msg = (await mockWs.nextMessage) as string | ||
const data = JSON.parse(msg) as UnsubscribeMessageRequest | ||
expect(data).toEqual({ | ||
action: "unsubscribe", | ||
id: "id", | ||
}) | ||
})() | ||
|
||
|
||
}) | ||
subscription.unsubscribe() | ||
await serverPromise | ||
}) | ||
}) |
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