react-use-websocket icon indicating copy to clipboard operation
react-use-websocket copied to clipboard

Add tests for disconnect using connect argument

Open Eric162 opened this issue 8 months ago • 0 comments

In the README, it states that since version 2.0.0, you can disconnect from the socket using the connect (third) argument of the hook, but there doesn't seem to be a test for it.

Components can close/unsubscribe from a WebSocket by passing false as the third parameter. This provides a more explicit solution than the previous method of setting the socketUrl to null. Both methods work and are supported usage.

This PR adds a test so nobody else has to investigate whether it's the library's code or their own which isn't working 😅 (spoiler it was my own code).

A few points of note:

  • The ReadyState.CONNECTING on the reconnect is happening, but in the tests it happens to fast to read the state out and assert on it.
  • Ideally the WS mock from jest-mock-websocket could tell us how many connections there are (is that just 0 or 1 ?), but alas it cannot. So using some mocks of the events to validate that we have connected and disconnected the right amount of times.

Eric162 avatar May 09 '25 16:05 Eric162