Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Created February 23, 2024 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoQuaresma/84cee3aed0ef6b5c3d6e4a1602001617 to your computer and use it in GitHub Desktop.
Save BrunoQuaresma/84cee3aed0ef6b5c3d6e4a1602001617 to your computer and use it in GitHub Desktop.
Storybook decorator that supports WebSocket messages
export const withWebSocket = (Story: FC, { parameters }: StoryContext) => {
if (!parameters.webSocket) {
console.warn(
"Looks like you forgot to add websocket messages to the story",
);
}
// @ts-expect-error -- TS doesn't know about the global WebSocket
window.WebSocket = function () {
return {
addEventListener: (
type: string,
callback: (ev: Record<"data", string>) => void,
) => {
if (type === "message") {
parameters.webSocket?.messages.forEach((message) => {
callback({ data: message });
});
}
},
close: () => {},
};
};
return <Story />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment