This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
/
mediator_spec.coffee
77 lines (60 loc) · 2.23 KB
/
mediator_spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
sinon = require 'sinon'
{mediator, Model} = require '../src/chaplin'
describe 'mediator', ->
it 'should be a simple object', ->
expect(mediator).to.be.an 'object'
it 'should have seal method and be sealed', ->
expect(mediator.seal).to.be.a 'function'
expect(mediator).to.be.sealed
it 'should have Pub/Sub methods', ->
expect(mediator.subscribe).to.be.a 'function'
expect(mediator.subscribeOnce).to.be.a 'function'
expect(mediator.unsubscribe).to.be.a 'function'
expect(mediator.publish).to.be.a 'function'
it 'should have readonly Pub/Sub and Resp/Req methods', ->
methods = [
'subscribe', 'subscribeOnce', 'unsubscribe', 'publish',
'setHandler', 'execute', 'removeHandlers'
]
for method in methods
expect(mediator).to.have.ownPropertyDescriptor method,
value: mediator[method]
writable: false
enumerable: true
configurable: false
it 'should publish messages to subscribers', ->
spy = sinon.spy()
eventName = 'foo'
payload = 'payload'
mediator.subscribe eventName, spy
mediator.publish eventName, payload
expect(spy).to.have.been.calledOnce
mediator.unsubscribe eventName, spy
it 'should publish messages to subscribers once', ->
spy = sinon.spy()
eventName = 'foo'
payload = 'payload'
mediator.subscribeOnce eventName, spy
mediator.publish eventName, payload
mediator.publish eventName, 'second'
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith payload
it 'should allow to unsubscribe to events', ->
spy = sinon.spy()
eventName = 'foo'
payload = 'payload'
mediator.subscribe eventName, spy
mediator.unsubscribe eventName, spy
mediator.publish eventName, payload
expect(spy).to.not.have.been.calledWith payload
it 'should have response / request methods', ->
expect(mediator.setHandler).to.be.a 'function'
expect(mediator.execute).to.be.a 'function'
expect(mediator.removeHandlers).to.be.a 'function'
it 'should allow to set and execute handlers', ->
response = 'austrian'
spy = sinon.stub().returns response
name = 'ancap'
mediator.setHandler name, spy
expect(mediator.execute name).to.equal response