UNPKG

2.97 kBtext/coffeescriptView Raw
1chai = require 'chai'
2expect = chai.expect
3sinon = require 'sinon'
4eventric = require 'eventric'
5sinonChai = require 'sinon-chai'
6chai.use sinonChai
7
8describe 'SocketIO remote scenario', ->
9 socketIORemoteEndpoint = null
10 socketIORemoteClient = null
11 socketServer = null
12 socketClient = null
13
14 before (done) ->
15 socketServer = require('socket.io')()
16 socketServer.listen 3000
17 socketIORemoteEndpoint = require './endpoint'
18 socketIORemoteEndpoint.initialize ioInstance: socketServer, ->
19 eventric.addRemoteEndpoint 'socketio', socketIORemoteEndpoint
20 socketClient = require('socket.io-client')('http://localhost:3000')
21 socketClient.on 'connect', ->
22 socketIORemoteClient = require 'eventric-remote-socketio-client'
23 socketIORemoteClient.initialize ioClientInstance: socketClient
24 .then ->
25 done()
26
27
28 after ->
29 socketIORemoteEndpoint.close()
30 socketIORemoteClient.disconnect()
31 require._cache = {}
32
33
34 describe 'creating an example context and adding a socketio remote endpoint', ->
35 exampleRemote = null
36 socketIORemoteClient = null
37 doSomethingStub = null
38 createSomethingStub = null
39 modifySomethingStub = null
40
41 beforeEach (done) ->
42 exampleContext = require './example_context'
43 doSomethingStub = sinon.stub()
44 createSomethingStub = sinon.stub()
45 modifySomethingStub = sinon.stub()
46
47 exampleContext.addCommandHandlers
48 DoSomething: doSomethingStub
49
50 exampleContext.initialize()
51 .then ->
52 exampleRemote = eventric.remote 'Example'
53 exampleRemote.addClient 'socketio', socketIORemoteClient
54 exampleRemote.set 'default client', 'socketio'
55 done()
56
57
58 it 'should be possible to receive and execute commands', (done) ->
59 exampleRemote.command 'CreateSomething'
60 .then (aggregateId) ->
61 exampleRemote.command 'DoSomething', aggregateId: aggregateId
62 .then ->
63 expect(doSomethingStub).to.have.been.calledOnce
64 done()
65
66
67 it 'should be possible to subscribe handlers to domain events', (done) ->
68 exampleRemote.subscribeToDomainEvent 'SomethingCreated'
69 .then (aggregateId) ->
70 createSomethingStub()
71 exampleRemote.unsubscribeFromDomainEvent aggregateId
72 exampleRemote.command 'CreateSomething'
73 .then ->
74 expect(createSomethingStub).to.have.been.calledOnce
75 done()
76
77
78 it 'should be possible to subscribe handlers to domain events with specific aggregate ids', (done) ->
79 exampleRemote.subscribeToDomainEventWithAggregateId 'SomethingModified'
80 .then (aggregateId) ->
81 modifySomethingStub()
82 exampleRemote.unsubscribeFromDomainEvent aggregateId
83 exampleRemote.command 'CreateSomething'
84 .then (aggregateId) ->
85 exampleRemote.command 'ModifySomething', id: aggregateId
86 .then ->
87 expect(modifySomethingStub).to.have.been.calledOnce
88 done()