Skip to content

Commit

Permalink
WebsocketProviderTest updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Furter committed Mar 27, 2019
1 parent a4ad5e6 commit 8240a27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/web3-providers/src/providers/WebsocketProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default class WebsocketProvider extends AbstractSocketProvider {

try {
this.connection.send(JSON.stringify(payload));
} catch(error) {
} catch (error) {
reject(error);
}

Expand Down Expand Up @@ -236,7 +236,9 @@ export default class WebsocketProvider extends AbstractSocketProvider {
}

this.once('connect', () => {
this.sendPayload(payload).then(resolve).catch(reject);
this.sendPayload(payload)
.then(resolve)
.catch(reject);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ describe('WebsocketProviderTest', () => {
expect(socketMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
});

it('calls sendPayload and returns with a rejected promise because of the connection.send() method', async () => {
socketMock.OPEN = 4;
socketMock.readyState = 4;
socketMock.CONNECTING = 0;
socketMock.send = jest.fn(() => {
throw new Error('Nope');
});
websocketProvider.timeout = 2;

await expect(websocketProvider.sendPayload({id: '0x0'})).rejects.toThrow('Nope');

expect(socketMock.send).toHaveBeenCalledWith('{"id":"0x0"}');
});

it('calls sendPayload with a timeout defined and returns with a resolved promise', async () => {
socketMock.OPEN = 4;
socketMock.readyState = 4;
Expand Down

0 comments on commit 8240a27

Please sign in to comment.