UNPKG

7.93 kBtext/coffeescriptView Raw
1require('es6-promise').polyfill()
2
3chai = require 'chai'
4expect = chai.expect
5sinon = require 'sinon'
6
7describe 'endpoint', ->
8 sandbox = null
9 endpoint = null
10 ioStub = null
11
12 beforeEach ->
13 sandbox = sinon.sandbox.create()
14 endpoint = require './endpoint'
15
16 ioStub =
17 sockets:
18 on: sandbox.stub()
19
20
21 afterEach ->
22 sandbox.restore()
23
24
25 describe '#initialize', ->
26
27 it 'should register the a connection handler on the Socket.IO server', ->
28 endpoint.initialize
29 ioInstance: ioStub
30
31 expect(ioStub.sockets.on.calledWith 'connection', sinon.match.func).to.be.ok
32
33
34 describe 'receiving a JoinRoom socket event', ->
35 socketStub = null
36
37 beforeEach ->
38 socketStub =
39 on: sandbox.stub()
40 join: sandbox.stub()
41 leave: sandbox.stub()
42 ioStub.sockets.on.withArgs('connection').yields socketStub
43 socketStub.on.withArgs('JoinRoom').yields 'RoomName'
44
45
46 describe 'given no rpc request middleware', ->
47
48 it 'should join the room', ->
49 endpoint.initialize
50 ioInstance: ioStub
51 expect(socketStub.join.calledWith 'RoomName').to.be.ok
52
53
54 describe 'given a rpc request middleware', ->
55
56 rpcRequestMiddlewareStub = null
57
58 beforeEach ->
59 rpcRequestMiddlewareStub = sandbox.stub()
60
61 describe 'always', ->
62
63 it 'should call the middleware and pass in the room name and the assiocated socket', (done) ->
64 rpcRequestMiddlewareStub.returns new Promise (resolve) -> resolve()
65 endpoint.initialize
66 ioInstance: ioStub
67 rpcRequestMiddleware: rpcRequestMiddlewareStub
68
69 setTimeout ->
70 expect(rpcRequestMiddlewareStub).to.have.been.called
71 expect(rpcRequestMiddlewareStub.firstCall.args[0]).to.equal 'RoomName'
72 expect(rpcRequestMiddlewareStub.firstCall.args[1]).to.equal socketStub
73 done()
74
75
76 describe 'which resolves', ->
77
78 it 'should join the room', (done) ->
79 rpcRequestMiddlewareStub.returns new Promise (resolve) -> resolve()
80 endpoint.initialize
81 ioInstance: ioStub
82 rpcRequestMiddleware: rpcRequestMiddlewareStub
83 setTimeout ->
84 expect(socketStub.join.calledWith 'RoomName').to.be.ok
85 done()
86
87
88 describe 'which rejects', ->
89
90 it 'should not join the room', (done) ->
91 rpcRequestMiddlewareStub.returns new Promise (resolve, reject) -> reject()
92 endpoint.initialize
93 ioInstance: ioStub
94 rpcRequestMiddleware: rpcRequestMiddlewareStub
95 setTimeout ->
96 expect(socketStub.join.calledOnce).to.be.false
97 done()
98
99
100
101 describe 'receiving a LeaveRoom socket event', ->
102
103 socketStub = null
104
105 beforeEach ->
106 socketStub =
107 on: sandbox.stub()
108 join: sandbox.stub()
109 leave: sandbox.stub()
110 ioStub.sockets.on.withArgs('connection').yields socketStub
111
112
113 it 'should leave the room', ->
114 socketStub.on.withArgs('LeaveRoom').yields 'RoomName'
115 endpoint.initialize
116 ioInstance: ioStub
117 expect(socketStub.leave.calledWith 'RoomName').to.be.ok
118
119
120 describe 'receiving a RPC_Request event', ->
121 socketStub = null
122 rpcRequestFake = null
123 rpcHandlerStub = null
124
125 beforeEach ->
126 rpcRequestFake =
127 rpcId: 123
128 socketStub =
129 on: sandbox.stub()
130 emit: sandbox.stub()
131 rpcHandlerStub = sandbox.stub()
132 ioStub.sockets.on.withArgs('connection').yields socketStub
133 socketStub.on.withArgs('RPC_Request').yields rpcRequestFake
134 endpoint.setRPCHandler rpcHandlerStub
135
136
137 describe 'given no rpc request middleware', ->
138
139 it 'should execute the configured rpc handler', ->
140 endpoint.initialize
141 ioInstance: ioStub
142 expect(rpcHandlerStub.calledWith rpcRequestFake, sinon.match.func).to.be.ok
143
144
145 it 'should emit the return value of the configured handler as RPC_Response', ->
146 responseFake = {}
147 rpcHandlerStub.yields null, responseFake
148 endpoint.initialize
149 ioInstance: ioStub
150 expect(socketStub.emit.calledWith 'RPC_Response', rpcId: rpcRequestFake.rpcId, err: null, data: responseFake).to.be.ok
151
152
153 describe 'given a rpc request middleware', ->
154
155 rpcRequestMiddlewareStub = null
156
157 beforeEach ->
158 rpcRequestMiddlewareStub = sandbox.stub()
159
160
161 describe 'always', ->
162
163 it 'should call the middleware and pass in the rpc request data and the assiocated socket', (done) ->
164 rpcRequestMiddlewareStub.returns new Promise (resolve) -> resolve()
165 endpoint.initialize
166 ioInstance: ioStub
167 rpcRequestMiddleware: rpcRequestMiddlewareStub
168
169 setTimeout ->
170 expect(rpcRequestMiddlewareStub).to.have.been.called
171 expect(rpcRequestMiddlewareStub.firstCall.args[0]).to.equal rpcRequestFake
172 expect(rpcRequestMiddlewareStub.firstCall.args[1]).to.equal socketStub
173 done()
174
175
176 describe 'which resolves', ->
177
178 responseFake = null
179
180 beforeEach (done) ->
181 responseFake = {}
182 rpcRequestMiddlewareStub.returns new Promise (resolve) -> resolve()
183 rpcHandlerStub.yields null, responseFake
184 endpoint.initialize
185 ioInstance: ioStub
186 rpcRequestMiddleware: rpcRequestMiddlewareStub
187 setTimeout ->
188 done()
189
190
191 it 'should execute the configured rpc handler', ->
192 expect(rpcHandlerStub.calledWith rpcRequestFake, sinon.match.func).to.be.ok
193
194
195 it 'should emit the return value of the configured handler as RPC_Response', ->
196 expect(socketStub.emit.calledWith 'RPC_Response', rpcId: rpcRequestFake.rpcId, err: null, data: responseFake).to.be.ok
197
198
199 describe 'which rejects', ->
200
201 responseFake = null
202 errorFake = null
203
204 beforeEach (done) ->
205 responseFake = {}
206 errorFake = {}
207 rpcRequestMiddlewareStub.returns new Promise (resolve, reject) -> reject errorFake
208 rpcHandlerStub.yields null, responseFake
209 endpoint.initialize
210 ioInstance: ioStub
211 rpcRequestMiddleware: rpcRequestMiddlewareStub
212 setTimeout ->
213 done()
214
215
216 it 'should not execute the configured rpc handler', ->
217 expect(rpcHandlerStub.calledWith rpcRequestFake, sinon.match.func).to.be.false
218
219
220 it 'should emit a RPC_Response event with an error', ->
221 expect(socketStub.emit.calledWith 'RPC_Response', rpcId: rpcRequestFake.rpcId, err: errorFake, data: null).to.be.ok
222
223
224
225 describe '#publish', ->
226
227 channelStub = null
228
229 beforeEach ->
230 channelStub =
231 emit: sandbox.stub()
232 endpoint.initialize
233 ioInstance: ioStub
234
235 ioStub.to = sandbox.stub().returns channelStub
236
237 describe 'given only a context name', ->
238 it 'should emit an event with payload to the correct channel', ->
239 payload = {}
240 endpoint.publish 'context', payload
241
242 expect(ioStub.to.calledWith 'context').to.be.ok
243 expect(channelStub.emit.calledWith 'context', payload).to.be.ok
244
245
246 describe 'given a context name and event name', ->
247 it 'should emit an event with payload to the correct channel', ->
248 payload = {}
249 endpoint.publish 'context', 'EventName', payload
250
251 expect(ioStub.to.calledWith 'context/EventName').to.be.ok
252 expect(channelStub.emit.calledWith 'context/EventName', payload).to.be.ok
253
254
255 describe 'given a context name, event name and aggregate id', ->
256 it 'should emit an event with payload to the correct channel', ->
257 payload = {}
258 endpoint.publish 'context', 'EventName', '12345', payload
259
260 expect(ioStub.to.calledWith 'context/EventName/12345').to.be.ok
261 expect(channelStub.emit.calledWith 'context/EventName/12345', payload).to.be.ok
\No newline at end of file