1 | chai = require 'chai'
|
2 | expect = chai.expect
|
3 | sinon = require 'sinon'
|
4 |
|
5 | describe 'endpoint', ->
|
6 | sandbox = null
|
7 | endpoint = null
|
8 | ioStub = null
|
9 |
|
10 | beforeEach ->
|
11 | sandbox = sinon.sandbox.create()
|
12 | endpoint = require './endpoint'
|
13 |
|
14 | ioStub =
|
15 | sockets:
|
16 | on: sandbox.stub()
|
17 |
|
18 |
|
19 | afterEach ->
|
20 | sandbox.restore()
|
21 |
|
22 |
|
23 | describe '#initialize', ->
|
24 |
|
25 | it 'should register the connection handler', ->
|
26 | endpoint.initialize
|
27 | ioInstance: ioStub
|
28 |
|
29 | expect(ioStub.sockets.on.calledWith 'connection', sinon.match.func).to.be.ok
|
30 |
|
31 |
|
32 | describe 'Socket Events', ->
|
33 | socketStub = null
|
34 |
|
35 | beforeEach ->
|
36 | socketStub =
|
37 | on: sandbox.stub()
|
38 | join: sandbox.stub()
|
39 | leave: sandbox.stub()
|
40 | ioStub.sockets.on.withArgs('connection').yields socketStub
|
41 |
|
42 |
|
43 | it 'on JoinRoom it should join the room', ->
|
44 | socketStub.on.withArgs('JoinRoom').yields 'RoomName'
|
45 | endpoint.initialize
|
46 | ioInstance: ioStub
|
47 | expect(socketStub.join.calledWith 'RoomName').to.be.ok
|
48 |
|
49 |
|
50 | it 'on LeaveRoom should leave the room', ->
|
51 | socketStub.on.withArgs('LeaveRoom').yields 'RoomName'
|
52 | endpoint.initialize
|
53 | ioInstance: ioStub
|
54 | expect(socketStub.leave.calledWith 'RoomName').to.be.ok
|
55 |
|
56 |
|
57 | describe '#setRPCHandler', ->
|
58 | socketStub = null
|
59 |
|
60 | beforeEach ->
|
61 | socketStub =
|
62 | on: sandbox.stub()
|
63 | emit: sandbox.stub()
|
64 | ioStub.sockets.on.withArgs('connection').yields socketStub
|
65 | endpoint.initialize
|
66 | ioInstance: ioStub
|
67 |
|
68 |
|
69 | it 'should execute the configured handler upon an incoming RPC_Request', ->
|
70 | rpcHandlerStub = sandbox.spy()
|
71 | endpoint.setRPCHandler rpcHandlerStub
|
72 |
|
73 | rpcRequestStub =
|
74 | rpcId: 123
|
75 |
|
76 | socketStub.on.firstCall.args[1] rpcRequestStub
|
77 |
|
78 | expect(rpcHandlerStub.calledWith rpcRequestStub, sinon.match.func).to.be.ok
|
79 |
|
80 |
|
81 | it 'should emit the return value of the configured handler as RPC_Response', ->
|
82 | rpcRequestStub =
|
83 | rpcId: 123
|
84 | responseStub = {}
|
85 |
|
86 | rpcHandlerStub = sandbox.stub().yields null, responseStub
|
87 | endpoint.setRPCHandler rpcHandlerStub
|
88 |
|
89 | socketStub.on.firstCall.args[1] rpcRequestStub
|
90 |
|
91 | expect(socketStub.emit.calledWith 'RPC_Response', rpcId: rpcRequestStub.rpcId, err: null, data: responseStub).to.be.ok
|
92 |
|
93 |
|
94 | describe '#publish', ->
|
95 |
|
96 | channelStub = null
|
97 |
|
98 | beforeEach ->
|
99 | channelStub =
|
100 | emit: sandbox.stub()
|
101 | endpoint.initialize
|
102 | ioInstance: ioStub
|
103 |
|
104 | ioStub.to = sandbox.stub().returns channelStub
|
105 |
|
106 | describe 'given only a context name', ->
|
107 | it 'should emit an event with payload to the correct channel', ->
|
108 | payload = {}
|
109 | endpoint.publish 'context', payload
|
110 |
|
111 | expect(ioStub.to.calledWith 'context').to.be.ok
|
112 | expect(channelStub.emit.calledWith 'context', payload).to.be.ok
|
113 |
|
114 |
|
115 | describe 'given a context name and event name', ->
|
116 | it 'should emit an event with payload to the correct channel', ->
|
117 | payload = {}
|
118 | endpoint.publish 'context', 'EventName', payload
|
119 |
|
120 | expect(ioStub.to.calledWith 'context/EventName').to.be.ok
|
121 | expect(channelStub.emit.calledWith 'context/EventName', payload).to.be.ok
|
122 |
|
123 |
|
124 | describe 'given a context name, event name and aggregate id', ->
|
125 | it 'should emit an event with payload to the correct channel', ->
|
126 | payload = {}
|
127 | endpoint.publish 'context', 'EventName', '12345', payload
|
128 |
|
129 | expect(ioStub.to.calledWith 'context/EventName/12345').to.be.ok
|
130 | expect(channelStub.emit.calledWith 'context/EventName/12345', payload).to.be.ok |
\ | No newline at end of file |