Skip to content

Commit

Permalink
feat: emitting connection_error if a connection fails (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso authored Jul 20, 2021
1 parent 057ae0e commit 77deff8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
11 changes: 10 additions & 1 deletion __tests__/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('Ping', (done) => {
}, 10000)

test('Close', () => {

expect(WebSocketHandler.ws.started).toBe(true);
expect(WebSocketHandler.ws.connected).toBe(true);
expect(WebSocketHandler.ws.isOnline).toBe(true);
Expand All @@ -38,3 +37,13 @@ test('Close', () => {
expect(WebSocketHandler.ws.connected).toBe(false);
expect(WebSocketHandler.ws.isOnline).toBe(false);
})

test('Error on ws should emit connection_error event', () => {
expect.hasAssertions();

WebSocketHandler.ws.on('connection_error', (evt) => {
expect(evt.message).toStrictEqual('expect-me');
});

WebSocketHandler.ws.onError(new Error('expect-me'));
})
1 change: 1 addition & 0 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class WS extends EventEmitter {
* @param {Object} evt Event that contains the error
*/
onError(evt) {
this.emit('connection_error', evt);
this.onClose();
// console.log('ws error', window.navigator.onLine, evt);
}
Expand Down

0 comments on commit 77deff8

Please sign in to comment.