UNPKG

3.4 kBtext/coffeescriptView Raw
1describe 'eventric', ->
2
3 domainEventSpecHelper = require 'eventric/domain_event/domain_event.spec_helper'
4
5 Context = null
6 Remote = null
7
8 beforeEach ->
9 Context = require './context'
10 Remote = require './remote'
11
12
13 describe '#context', ->
14
15 it 'should generate a new eventric context', ->
16 context = eventric.context 'context'
17 expect(context).to.be.an.instanceOf Context
18
19
20 it 'should subscribe to all domain events of the context', ->
21 sandbox.spy Context::, 'subscribeToAllDomainEvents'
22 context = eventric.context 'context'
23 expect(context.subscribeToAllDomainEvents).to.have.been.called
24
25
26 it 'should delegate any event from the context to all remote endpoints', (done) ->
27 domainEvent = domainEventSpecHelper.createDomainEvent 'SomeDomainEvent'
28 contextName = domainEvent.context
29 sandbox.stub(Context::, 'subscribeToAllDomainEvents').yields domainEvent
30
31 inmemoryRemoteEndpoint = require('eventric-remote-inmemory').endpoint
32 sandbox.stub inmemoryRemoteEndpoint, 'publish'
33
34 eventric.context contextName
35
36 setTimeout ->
37 expect(inmemoryRemoteEndpoint.publish).to.have.been.calledWith contextName, 'SomeDomainEvent', domainEvent
38 expect(inmemoryRemoteEndpoint.publish).to.have.been.calledWith contextName, 'SomeDomainEvent', sinon.match.string,
39 domainEvent
40 done()
41
42
43 describe '#remoteContext', ->
44
45 it 'should throw an error given no context name', ->
46 expect(-> eventric.remoteContext null ).to.throw Error, /Missing context name/
47
48
49 it 'should return the remote context for a given context name', ->
50 remoteContext = eventric.remoteContext 'ContextName'
51 expect(remoteContext).to.be.an.instanceOf Remote
52
53
54 it 'should cache remote contexts by context name', ->
55 remote1 = eventric.remoteContext 'ContextName'
56 remote2 = eventric.remoteContext 'ContextName'
57 expect(remote1).to.equal remote2
58
59
60 it 'should not set a default remote client on the returned remote context given no default client was set before', ->
61 inmemoryRemote = require 'eventric-remote-inmemory'
62 remoteContext = eventric.remoteContext 'ContextName'
63 expect(remoteContext._client).to.equal inmemoryRemote.client
64
65
66 it 'should set the default remote client on the returned remote context given a default client was set before', ->
67 remoteClient = {}
68 eventric.setDefaultRemoteClient remoteClient
69 remoteContext = eventric.remoteContext 'ContextName'
70 expect(remoteContext._client).to.equal remoteClient
71
72
73 describe '#setDefaultRemoteClient', ->
74
75 it 'should remember the default remote client', ->
76 remoteClient = {}
77 eventric.setDefaultRemoteClient remoteClient
78 expect(eventric._defaultRemoteClient).to.equal remoteClient
79
80
81 describe '#getRegisteredContextNames', ->
82
83 it 'should return all names of all registered contexts', ->
84 eventric.context 'context1'
85 eventric.context 'context2'
86 contextNames = eventric.getRegisteredContextNames()
87 expect(contextNames).to.deep.equal ['context1', 'context2']
88
89
90 describe '#generateUuid', ->
91
92 it 'should ask the uuid generator to generate a uuid', ->
93 uuidGenerator = require './uuid_generator'
94 sandbox.spy uuidGenerator, 'generateUuid'
95 eventric.generateUuid()
96 expect(uuidGenerator.generateUuid).to.have.been.called