-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.test.js
56 lines (47 loc) · 1.53 KB
/
broker.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
let Broker = require('./broker.js');
test('Adds one caller', () => {
const resource = 'valuable resource';
const cmdProc = jest.fn();
const notifyCb = jest.fn();
const b = new Broker(resource, cmdProc, notifyCb);
const caller1 = 'c1';
b.register(caller1);
expect(notifyCb).toBeCalledWith(caller1);
});
test('Adds 2 callers', () => {
const resource = 'valuable resource';
const cmdProc = jest.fn();
const notifyCb = jest.fn();
const b = new Broker(resource, cmdProc, notifyCb);
const caller1 = 'c1';
const caller2 = 'c2';
b.register(caller1);
b.register(caller2);
expect(notifyCb).toBeCalledWith(caller1);
expect(notifyCb).toHaveBeenCalledTimes(1);
});
test('Sends a command', () => {
const resource = 'valuable resource';
const cmdProc = jest.fn();
const notifyCb = jest.fn();
const b = new Broker(resource, cmdProc, notifyCb);
const caller1 = 'c1';
const cmd = 'command';
b.register(caller1);
b.passCommand(cmd, caller1);
expect(cmdProc).toBeCalledWith(resource, cmd);
expect(cmdProc).toHaveBeenCalledTimes(1);
});
test('Sends a command from object without control', () => {
const resource = 'valuable resource';
const cmdProc = jest.fn();
const notifyCb = jest.fn();
const b = new Broker(resource, cmdProc, notifyCb);
const caller1 = 'c1';
const caller2 = 'c2';
const cmd = 'command';
b.register(caller1);
b.register(caller2);
b.passCommand(cmd, caller2);
expect(cmdProc).toHaveBeenCalledTimes(0);
});