I'm using create-react-app, Jest and react-testing-library for the configuration of the chatbot project.
I have a React functional component that connects to a WebSocket server and DOM changes according to WebSocket messages, for example
const LiveChat = () => {
const [socket, setSocket] = useState(null)
useEffect(() => {
setSocket(new WebSocket('ws://localhost:1234'))
}, [])
useEffect(() => {
socket && socket.onmessage = message => { handleAgentMessages(message.data) }
}, [socket])
const handleAgentMessages = message => {
const { messageContent, messageType, secureKey } = JSON.parse(message)
if (messageType === TEXT && messageContent) {
alert(messageContent)
playChatMessageSound()
}
...
}
return (
<div className='live-chat' data-testid='live-chat'>
...
</div>
)
}
I want to test when a TEXT message comes, is alertbox appeared with conteining message etc. I have looked through the internet and I find jest-websocket-mock library, but it seems that I need to mock the client with this library as well, but I just want to mock server and expect the client to connect the mocked WebSocket server, do you have any ideas?