Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

149
Views
How to simulate receiving message from Server to Client in order to unit test SignalR client/manager with react-testing-library and jest?

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?

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs