react-use-websocket
react-use-websocket copied to clipboard
Add tests for disconnect using connect argument
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.CONNECTINGon the reconnect is happening, but in the tests it happens to fast to read the state out and assert on it. - Ideally the
WSmock fromjest-mock-websocketcould 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.