Skip to content

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 14 commits into from
Aug 7, 2025
Merged
Prev Previous commit
Next Next commit
refactor: rename property
  • Loading branch information
Parkreiner committed Aug 5, 2025
commit 6ee9afc035ae4a0cfbda4091f64dab84fd4ecb61
8 changes: 4 additions & 4 deletions site/src/testHelpers/websockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ describe(createMockWebSocket.name, () => {
});

socket.send(data);
expect(server.socketSendArguments).toHaveLength(1);
expect(server.socketSendArguments).toEqual([data]);
expect(server.clientSentData).toHaveLength(1);
expect(server.clientSentData).toEqual([data]);

socket.close();
socket.send(data);
expect(server.socketSendArguments).toHaveLength(1);
expect(server.socketSendArguments).toEqual([data]);
expect(server.clientSentData).toHaveLength(1);
expect(server.clientSentData).toEqual([data]);
});
});
10 changes: 5 additions & 5 deletions site/src/testHelpers/websockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type MockWebSocketServer = Readonly<{
publishOpen: (event: Event) => void;

readonly isConnectionOpen: boolean;
readonly socketSendArguments: readonly SocketSendData[];
readonly clientSentData: readonly SocketSendData[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one part I was a little torn on, and I'd be open to alternative ways to track data sent from the mock socket to the mock server

It feels like the "boy scout" way of doing this would be with subscription callbacks, but since we'd likely just be checking the contents for a single isolated test case, that'd add more boilerplate to all test cases when we really just care about what data got sent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally seems good enough to me

}>;

export function createMockWebSocket(
Expand Down Expand Up @@ -47,7 +47,7 @@ export function createMockWebSocket(
open: [],
};

const sendData: SocketSendData[] = [];
const sentData: SocketSendData[] = [];

const mockSocket: WebSocket = {
CONNECTING: 0,
Expand All @@ -71,7 +71,7 @@ export function createMockWebSocket(
if (!isOpen) {
return;
}
sendData.push(data);
sentData.push(data);
},

addEventListener: <E extends WebSocketEventType>(
Expand Down Expand Up @@ -113,8 +113,8 @@ export function createMockWebSocket(
return isOpen;
},

get socketSendArguments() {
return [...sendData];
get clientSentData() {
return [...sentData];
},

publishOpen: (event) => {
Expand Down
Loading