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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

Parkreiner
Copy link
Member

@Parkreiner Parkreiner commented Aug 5, 2025

Needed for #19126 and #18679

Changes made

  • Moved createWebSocket to dedicated file and addressed edge cases for making it a reliable mock
  • Added test cases to validate mock functionality

@Parkreiner Parkreiner requested a review from jaaydenh August 5, 2025 01:29
@Parkreiner Parkreiner self-assigned this Aug 5, 2025
@Parkreiner Parkreiner requested a review from aslilac as a code owner August 5, 2025 01:29
publishOpen: (event: Event) => void;

readonly isConnectionOpen: boolean;
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

Copy link
Member

@aslilac aslilac left a comment

Choose a reason for hiding this comment

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

these tests are really nice, thank you!

left a couple things for you to think about but overall looks great. really good job.

Comment on lines +6 to +7
"http://www.dog.ceo/roll-over",
"https://www.dog.ceo/roll-over",
Copy link
Member

Choose a reason for hiding this comment

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

dog.ceo!!!

Comment on lines +48 to +49
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledWith(closeEvent);
Copy link
Member

Choose a reason for hiding this comment

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

this test makes me feel like I'm looking at a freshly ironed shirt. super nice!

for (const jd of jsonData) {
socket.addEventListener("message", onMessage);
server.publishMessage(
new MessageEvent<string>("message", {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
new MessageEvent<string>("message", {
new MessageEvent("message", {

locally it looks like <string> is being inferred correctly, but if you want it to be more explicit I'm fine with that


expect(onMessage).toHaveBeenCalledTimes(1);
expect(onMessage).toHaveBeenCalledWith(
new MessageEvent<string>("message", {
Copy link
Member

Choose a reason for hiding this comment

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

same here

});

it("Only registers each socket event handler once", () => {
const [socket, server] = createMockWebSocket("wss://www.dog.ceo/borf");
Copy link
Member

Choose a reason for hiding this comment

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

borf!!!

Comment on lines +25 to +27
type CallbackStore = {
[K in keyof EventMap]: ((event: EventMap[K]) => void)[];
};
Copy link
Member

Choose a reason for hiding this comment

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

also I'd rather define this type outside of the function. it doesn't capture any local types.

Comment on lines +33 to +40
let activeProtocol: string;
if (Array.isArray(protocols)) {
activeProtocol = protocols[0] ?? "";
} else if (typeof protocols === "string") {
activeProtocol = protocols;
} else {
activeProtocol = "";
}
Copy link
Member

Choose a reason for hiding this comment

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

why bother accepting multiple protocols if we only ever use the one? protocols isn't referenced after here

Comment on lines +86 to +88
if (!subscribers.includes(callback)) {
subscribers.push(callback);
}
Copy link
Member

Choose a reason for hiding this comment

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

what if we used Sets instead to enforce uniqueness? they're more friendly for this than Arrays

Comment on lines +99 to +103
const subscribers = store[eventType];
if (subscribers.includes(callback)) {
const updated = store[eventType].filter((c) => c !== callback);
store[eventType] = updated as CallbackStore[E];
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const subscribers = store[eventType];
if (subscribers.includes(callback)) {
const updated = store[eventType].filter((c) => c !== callback);
store[eventType] = updated as CallbackStore[E];
}
store[eventType].delete(callback);

...and then this especially gets simplified

Comment on lines +116 to +118
get clientSentData() {
return [...sentData];
},
Copy link
Member

Choose a reason for hiding this comment

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

cheeky little copy for immutability, nice.

onopen: null,
dispatchEvent: jest.fn(),

send: (data) => {
Copy link
Contributor

@jaaydenh jaaydenh Aug 7, 2025

Choose a reason for hiding this comment

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

Can we make send a jest.fn() so that its possible to do something like this?

expect(mockWebSocket.send).toHaveBeenCalledWith(
     expect.stringContaining('"instance_type":"t3.medium"'),
);
		send: jest.fn((data) => {
			if (!isOpen) {
				return;
			}
			sentData.push(data);
		})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants