-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
181 lines (133 loc) · 5.56 KB
/
test.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import test from 'ava';
import sinon from 'sinon';
import medi from './medi';
test.beforeEach(t => {
t.context.consoleWarn = console.warn = sinon.spy();
t.context.consoleInfo = console.info = sinon.spy();
t.context.mediator = medi();
});
test('handler a message', async t => {
const handlerFn = sinon.spy();
t.context.mediator.when('somechannel', handlerFn);
t.context.mediator.emit('somechannel', 'somemessage');
t.true(handlerFn.calledWith('somemessage'));
});
test('multiple handlers will be called', async t => {
const handlerOneFn = sinon.spy();
const handlerTwoFn = sinon.spy();
t.context.mediator.when('somechannel', handlerOneFn);
t.context.mediator.when('somechannel', handlerTwoFn);
t.context.mediator.emit('somechannel', 'somemessage');
t.true(handlerOneFn.calledWith('somemessage'));
t.true(handlerTwoFn.calledWith('somemessage'));
});
test('delete a channel', async t => {
const handlerFn = sinon.spy();
t.context.mediator.when('somechannel', handlerFn);
t.context.mediator.delete('somechannel');
t.context.mediator.emit('somechannel', 'somemessage');
t.false(handlerFn.called);
});
test('delete a specific handler on a channel', async t => {
const notCalledHandlerFn = sinon.spy();
const calledHandlerFn = sinon.spy();
t.context.mediator.when('somechannel', notCalledHandlerFn);
t.context.mediator.when('somechannel', calledHandlerFn);
t.context.mediator.delete('somechannel', notCalledHandlerFn);
t.context.mediator.emit('somechannel', 'somemessage');
t.false(notCalledHandlerFn.called);
t.true(calledHandlerFn.calledWith('somemessage'));
});
test('delete a specific handler that doesn\'t exists anymore', async t => {
t.context.mediator = medi({log: true});
t.context.mediator.when('somechannel', () => {});
t.context.mediator.delete('somechannel');
t.context.mediator.delete('somechannel');
t.deepEqual(t.context.consoleWarn.args[0], [
'Delete(): No handlers for channel \"somechannel\"; nothing to delete'
]);
});
test('Trying to delete a handler that doesn\'t exists on the channel should not work', async t => {
t.context.mediator = medi({log: true});
const handler = () => {};
const secondHandlerThatDoesntExist = () => {};
t.context.mediator.when('somechannel', handler);
t.context.mediator.delete('somechannel', secondHandlerThatDoesntExist);
t.deepEqual(t.context.consoleWarn.args[0], [
'Delete(): Given handler does not exists on channel \"somechannel\"'
]);
});
test('filter message handlers', async t => {
t.context.mediator = medi({log: true});
const notCalledHandlerFn = sinon.stub().returns(123);
const calledHandlerFn = sinon.stub().returns(312);
t.context.mediator.when('somechannel', { someprop: 'notmatchingvalue' }, notCalledHandlerFn);
t.context.mediator.when('somechannel', { someprop: 'valuetomatch' }, calledHandlerFn);
const result = await t.context.mediator.emit('somechannel', { someprop: 'valuetomatch' }, 'somemessage');
t.false(notCalledHandlerFn.called);
t.true(calledHandlerFn.calledWith('somemessage'));
t.deepEqual(result, [ 312 ]);
t.deepEqual(t.context.consoleWarn.args[0], [
'Emit(): Not calling channel \"somechannel\", given filter does not match channel\'s filter'
]);
});
test('calling a channel that has a filter without specifing a filter should not be called', async t => {
t.context.mediator = medi({log: true});
const notCalledHandlerFn = sinon.spy();
t.context.mediator.when('somechannel', { filter: 'somefiltervalue' }, notCalledHandlerFn);
t.context.mediator.emit('somechannel', 'somemessage');
t.false(notCalledHandlerFn.called);
t.deepEqual(t.context.consoleWarn.args[0], [
'Emit(): Not calling channel \"somechannel\", channel has a filter; no filter given'
]);
});
test('calling emit with a filter on a channel without a filter, should not work', async t => {
const notCalledHandlerFn = sinon.spy();
t.context.mediator.when('somechannel', notCalledHandlerFn);
t.context.mediator.emit('somechannel', { someprop: 'somevalue' }, 'somemessage');
t.false(notCalledHandlerFn.called);
});
test('fireing promise after all when handlers have been called', async t => {
t.context.mediator.when('somechannel', () => {
return 1;
});
t.context.mediator.when('somechannel', () => {
return new Promise(resolve => {
setTimeout(() => {
resolve(2);
}, 16);
})
});
t.context.mediator.when('somechannel', () => {});
t.context.mediator.when('somechannel', () => {
return 1;
});
const result = await t.context.mediator.emit('somechannel', 'somemessage');
t.deepEqual(result, [1, 2, 1]);
});
test('console will be called when logging is on', async t => {
t.context.mediator = medi({log: true});
t.context.mediator.when('somechannel', () => {});
t.context.mediator.emit('somechannel', 'somemessage');
t.deepEqual(t.context.consoleInfo.args[0], [
'Emit(): Emitting event \"somechannel\" with payload:',
'somemessage',
'and filter: ',
null
]);
t.context.mediator.delete('somechannel');
t.context.mediator.emit('somechannel', 'somemessage');
t.deepEqual(t.context.consoleWarn.args[0], [
'Emit(): No handlers for event \"somechannel\", args: ',
'somemessage'
]);
});
test('console will not be called when logging is off', async t => {
t.context.mediator = medi({log: false});
t.context.mediator.when('somechannel', () => {});
t.context.mediator.emit('somechannel', 'somemessage');
t.false(t.context.consoleInfo.called);
t.context.mediator.delete('somechannel');
t.context.mediator.emit('somechannel', 'somemessage');
t.false(t.context.consoleWarn.called);
});