Created
February 23, 2024 14:32
-
-
Save BrunoQuaresma/84cee3aed0ef6b5c3d6e4a1602001617 to your computer and use it in GitHub Desktop.
Storybook decorator that supports WebSocket messages
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
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