-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a7cfcd
commit f2d481d
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |