I have a working SignalR manager with a connection: HubConnection
that is already built with .build()
.
Check the following quick example:
const connection: HubConnection = new HubConnectionBuilder().withUrl(...).build();
connection.start().then(() => {
console.log('connected');
...
connection.on('PushNewMessage', (message) => addNewMessageToReduxStore(message));
connection.on('DeleteMessage', (messageId) => deleteMessageFromStoreWithId(messageId));
...
});
Server sends message ‘PushNewMessage’ with an object with string text and number id. This triggers the addNewMessageToReduxStore(message)
which is tracked for changes and trigger more Redux changes.
I want to write a unit test that starts with “Server sending a message” and compares the final Redux store state after 5 seconds since the message was received from the server to match an expected store state – then the test will pass.
So how to simulate receiving a message from the server in that exact web socket managed by the HubConnection used in the unit test initialization?