Closed alduro closed 3 years ago
Hi @alduro,
lastJsonMessage is generated with the following:
const lastJsonMessage = useMemo(() => {
if (lastMessage) {
try {
return JSON.parse(lastMessage.data);
} catch (e) {
return UNPARSABLE_JSON_OBJECT;
}
}
return null;
},[lastMessage]);
UNPARSABLE_JSON_OBJECT is a static empty object. So where no message has been received from the websocket server, lastJsonMessage will be null -- otherwise it will be a JSON-parsed representation of the message (or an empty object if the message isn't parsable). As far as mocking it, that depends on how you are mocking the websocket connection in jest -- I recommend jest-websocket-mock. You can see how I test the library here.
Specifically, here is testing lastJsonMessage:
Hi, I have this code
and I was wondering how should I mock lastJsonMessge in a test ?
Thanks in advance.