Skip to content

Commit

Permalink
Add tests on Websocket manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Nov 9, 2020
1 parent 3a7cfcd commit f2d481d
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions server/test/websockets/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const EventEmitter = require('events');
const WebSocket = require('ws');
const { assert, fake } = require('sinon');
const WebsocketManager = require('../../api/websockets');

describe('Websockets', () => {
it('should send message to all users', () => {
const wss = new EventEmitter();
const gladys = {
event: new EventEmitter(),
};
const websocketManager = new WebsocketManager(wss, gladys);
const client = {
readyState: WebSocket.OPEN,
send: fake.returns(0),
};
const message = {
type: 'test',
payload: 'test',
};
websocketManager.userConnected({ id: 'aa0eaee9-5b90-4287-841a-0237f9d75832' }, client);
websocketManager.sendMessageAllUsers(message);
assert.calledWith(client.send, JSON.stringify(message));
});
it('should not send message to all users', () => {
const wss = new EventEmitter();
const gladys = {
event: new EventEmitter(),
};
const websocketManager = new WebsocketManager(wss, gladys);
const client = {
readyState: WebSocket.CLOSED,
send: fake.returns(0),
};
const message = {
type: 'test',
payload: 'test',
};
websocketManager.userConnected({ id: 'aa0eaee9-5b90-4287-841a-0237f9d75832' }, client);
websocketManager.sendMessageAllUsers(message);
assert.notCalled(client.send);
});
it('should send message to one users', () => {
const wss = new EventEmitter();
const gladys = {
event: new EventEmitter(),
};
const websocketManager = new WebsocketManager(wss, gladys);
const client = {
readyState: WebSocket.OPEN,
send: fake.returns(0),
};
const message = {
type: 'test',
payload: 'test',
};
websocketManager.userConnected({ id: 'aa0eaee9-5b90-4287-841a-0237f9d75832' }, client);
websocketManager.sendMessageUser({ ...message, userId: 'aa0eaee9-5b90-4287-841a-0237f9d75832' });
assert.calledWith(client.send, JSON.stringify(message));
});
});

0 comments on commit f2d481d

Please sign in to comment.