-
Notifications
You must be signed in to change notification settings - Fork 960
fix(site): add tests for createMockWebSocket
#19172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a87e93e
wip: commit progress on tests
Parkreiner 1c48afc
chore: get another test working
Parkreiner 5eff4df
chore: get all initial tests passing
Parkreiner c2d07fb
refactor: centralize socket implementation
Parkreiner 21c9eb8
chore: format
Parkreiner 1834c59
refactor: rename variable
Parkreiner 6ee9afc
refactor: rename property
Parkreiner c95aa4b
Merge branch 'main' into mes/socket-mock
Parkreiner cb42aa7
fix: use sets
Parkreiner b63ff10
refactor: clean up types
Parkreiner dab74d7
docs: beef up comment
Parkreiner c17ee61
fix: update protocol back
Parkreiner 9b155b1
Merge branch 'main' into mes/socket-mock
Parkreiner 5f9032d
fix: knip
Parkreiner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
chore: get all initial tests passing
- Loading branch information
commit 5eff4dfe314e658465530ede9c64c4960924f213
There are no files selected for viewing
This file contains hidden or 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,3 +1,4 @@ | ||
import { subscribe } from "node:diagnostics_channel"; | ||
import { createMockWebSocket } from "./websockets"; | ||
|
||
describe(createMockWebSocket.name, () => { | ||
|
@@ -13,8 +14,8 @@ describe(createMockWebSocket.name, () => { | |
} | ||
}) | ||
|
||
it("Sends events from publisher to socket", () => { | ||
const [socket, publisher] = createMockWebSocket("wss://www.dog.ceo/shake"); | ||
it("Sends events from server to socket", () => { | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/shake"); | ||
|
||
const onOpen = jest.fn(); | ||
const onError = jest.fn(); | ||
|
@@ -31,10 +32,10 @@ describe(createMockWebSocket.name, () => { | |
const messageEvent = new MessageEvent<string>("message") | ||
const closeEvent = new CloseEvent("close"); | ||
|
||
publisher.publishOpen(openEvent); | ||
publisher.publishError(errorEvent); | ||
publisher.publishMessage(messageEvent); | ||
publisher.publishClose(closeEvent); | ||
server.publishOpen(openEvent); | ||
server.publishError(errorEvent); | ||
server.publishMessage(messageEvent); | ||
server.publishClose(closeEvent); | ||
|
||
expect(onOpen).toHaveBeenCalledTimes(1); | ||
expect(onOpen).toHaveBeenCalledWith(openEvent); | ||
|
@@ -50,7 +51,7 @@ describe(createMockWebSocket.name, () => { | |
}); | ||
|
||
it("Sends JSON data to the socket for message events", () => { | ||
const [socket, publisher] = createMockWebSocket("wss://www.dog.ceo/wag"); | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/wag"); | ||
const onMessage = jest.fn(); | ||
|
||
// Could type this as a special JSON type, but unknown is good enough, | ||
|
@@ -74,7 +75,7 @@ describe(createMockWebSocket.name, () => { | |
]; | ||
for (const jd of jsonData) { | ||
socket.addEventListener("message", onMessage); | ||
publisher.publishMessage(new MessageEvent<string>("message", { | ||
server.publishMessage(new MessageEvent<string>("message", { | ||
"data": JSON.stringify(jd) | ||
})); | ||
|
||
|
@@ -89,7 +90,7 @@ describe(createMockWebSocket.name, () => { | |
}) | ||
|
||
it("Only registers each socket event handler once", () => { | ||
const [socket, publisher] = createMockWebSocket("wss://www.dog.ceo/borf"); | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/borf"); | ||
|
||
const onOpen = jest.fn(); | ||
const onError = jest.fn(); | ||
|
@@ -108,10 +109,10 @@ describe(createMockWebSocket.name, () => { | |
socket.addEventListener("message", onMessage); | ||
socket.addEventListener("close", onClose); | ||
|
||
publisher.publishOpen(new Event("open")); | ||
publisher.publishError(new Event("error")); | ||
publisher.publishMessage(new MessageEvent<string>("message")); | ||
publisher.publishClose(new CloseEvent("close")); | ||
server.publishOpen(new Event("open")); | ||
server.publishError(new Event("error")); | ||
server.publishMessage(new MessageEvent<string>("message")); | ||
server.publishClose(new CloseEvent("close")); | ||
|
||
expect(onOpen).toHaveBeenCalledTimes(1); | ||
expect(onError).toHaveBeenCalledTimes(1); | ||
|
@@ -120,7 +121,7 @@ describe(createMockWebSocket.name, () => { | |
}); | ||
|
||
it("Lets a socket unsubscribe to event types", () => { | ||
const [socket, publisher] = createMockWebSocket("wss://www.dog.ceo/zoomies"); | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/zoomies"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. zoomies!!! |
||
|
||
const onOpen = jest.fn(); | ||
const onError = jest.fn(); | ||
|
@@ -137,10 +138,10 @@ describe(createMockWebSocket.name, () => { | |
socket.removeEventListener("message", onMessage); | ||
socket.removeEventListener("close", onClose); | ||
|
||
publisher.publishOpen(new Event("open")); | ||
publisher.publishError(new Event("error")); | ||
publisher.publishMessage(new MessageEvent<string>("message")); | ||
publisher.publishClose(new CloseEvent("close")); | ||
server.publishOpen(new Event("open")); | ||
server.publishError(new Event("error")); | ||
server.publishMessage(new MessageEvent<string>("message")); | ||
server.publishClose(new CloseEvent("close")); | ||
|
||
expect(onOpen).not.toHaveBeenCalled(); | ||
expect(onError).not.toHaveBeenCalled(); | ||
|
@@ -149,20 +150,38 @@ describe(createMockWebSocket.name, () => { | |
}); | ||
|
||
it("Renders socket inert after being closed", () => { | ||
const [socket, publisher] = createMockWebSocket("wss://www.dog.ceo/woof"); | ||
expect(publisher.isConnectionOpen).toBe(true); | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/woof"); | ||
expect(server.isConnectionOpen).toBe(true); | ||
|
||
const onMessage = jest.fn(); | ||
socket.addEventListener("message", onMessage); | ||
|
||
socket.close(); | ||
expect(publisher.isConnectionOpen).toBe(false); | ||
expect(server.isConnectionOpen).toBe(false); | ||
|
||
publisher.publishMessage(new MessageEvent<string>("message")); | ||
server.publishMessage(new MessageEvent<string>("message")); | ||
expect(onMessage).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Lets a socket send data to the mock server", () => { | ||
expect.hasAssertions(); | ||
it("Tracks arguments sent by the mock socket", () => { | ||
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/wan-wan"); | ||
const data = JSON.stringify({ | ||
famousDogs: [ | ||
"snoopy", | ||
"clifford", | ||
"lassie", | ||
"beethoven", | ||
"courage the cowardly dog", | ||
], | ||
}); | ||
|
||
socket.send(data); | ||
expect(server.socketSendArguments).toHaveLength(1); | ||
expect(server.socketSendArguments).toEqual([data]); | ||
|
||
socket.close(); | ||
socket.send(data); | ||
expect(server.socketSendArguments).toHaveLength(1); | ||
expect(server.socketSendArguments).toEqual([data]); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
borf!!!