forked from GladysAssistant/Gladys
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ladysAssistant#931 websocket is not open bug (GladysAssistant#936) * Fix GladysAssistant#932 : fix start scene button in scene list * Fix GladysAssistant#931 : WebSocket is not open bug * Add tests on Websocket manager
- Loading branch information
1 parent
c17fbf4
commit 1fa4fed
Showing
3 changed files
with
103 additions
and
22 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
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
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)); | ||
}); | ||
}); |