1 | let both = require('../both');
|
2 | let sinonChai = require('sinon-chai');
|
3 | let sinon = require('sinon');
|
4 | let chai = require('chai');
|
5 | chai.use(sinonChai);
|
6 | let expect = chai.expect;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | describe('Execute Module', () => {
|
12 |
|
13 | describe('Error handling', () => {
|
14 |
|
15 | let dummyCommandFunc = null;
|
16 |
|
17 | beforeEach(() => {
|
18 | dummyCommandFunc = sinon.spy();
|
19 | });
|
20 |
|
21 | it('throws error if no argument is given', () => {
|
22 | expect(() => {
|
23 | both();
|
24 | }).to.throw(Error);
|
25 | });
|
26 |
|
27 | it('throws error if more than 1 argument is given', () => {
|
28 | expect(() => {
|
29 | both('eins', 'zwei', 'drei');
|
30 | }).to.throw(Error);
|
31 | });
|
32 |
|
33 | it('throws error if argument isn`t a string', () => {
|
34 | expect(() => {
|
35 | both(12345);
|
36 | }).to.throw(Error);
|
37 | });
|
38 |
|
39 | it('throws error if second argument isn`t a object or function', () => {
|
40 | expect(() => {
|
41 | both('dummyCommand', 'noObjectOrFunction');
|
42 | }).to.throw(Error);
|
43 | });
|
44 |
|
45 | it('throws no error if second argument is a object or function', () => {
|
46 | expect(() => {
|
47 | both('dummyCommand', {});
|
48 | }).to.not.throw(Error);
|
49 | });
|
50 |
|
51 | it('throw error if third argument isnt a function', () => {
|
52 | expect(() => {
|
53 | both('dummyCommand', {}, 'notAllowed');
|
54 | }).to.throw(Error);
|
55 | });
|
56 |
|
57 | it('throw nor error if third argument isnt a function', () => {
|
58 | expect(() => {
|
59 | both('dummyCommand', {}, () => {});
|
60 | }).to.not.throw(Error);
|
61 | });
|
62 | });
|
63 |
|
64 | describe('executing commands / queries / listeners', () => {
|
65 |
|
66 | let dummyCommandFunc = null;
|
67 | let dummyListenerFunc = null;
|
68 |
|
69 | beforeEach(() => {
|
70 | dummyCommandFunc = sinon.spy();
|
71 | dummyListenerFunc = sinon.spy();
|
72 | });
|
73 |
|
74 | it('registered command without defined listener', () => {
|
75 | both.command('dummyCommand', dummyCommandFunc);
|
76 |
|
77 | both('dummyCommand');
|
78 |
|
79 | expect(dummyCommandFunc).to.have.been.called;
|
80 | });
|
81 |
|
82 | it('registered listener', () => {
|
83 | both.command('dummyCommand', dummyCommandFunc);
|
84 | both.on('dummyCommand', dummyListenerFunc);
|
85 |
|
86 | both('dummyCommand');
|
87 |
|
88 | expect(dummyListenerFunc).to.have.been.calledOnce;
|
89 | });
|
90 |
|
91 | it('global listener', () => {
|
92 | both.command('dummyCommand', dummyCommandFunc);
|
93 | both.on(dummyListenerFunc);
|
94 |
|
95 | both('dummyCommand');
|
96 |
|
97 | expect(dummyListenerFunc).to.have.been.called;
|
98 | });
|
99 |
|
100 | it('ship payload to global listener with commandName', () => {
|
101 | both.command('dummyCommand', dummyCommandFunc);
|
102 |
|
103 | both.on(dummyListenerFunc);
|
104 |
|
105 | both('dummyCommand');
|
106 |
|
107 | expect(dummyListenerFunc).to.have.been.calledWith('dummyCommand');
|
108 | });
|
109 |
|
110 | it('ship payload to listener with methodName and payload from executed command', () => {
|
111 | both.command('dummyCommand', dummyListenerFunc);
|
112 |
|
113 | both.on(dummyListenerFunc);
|
114 |
|
115 | both('dummyCommand', {
|
116 | dummyAttribute: true
|
117 | });
|
118 |
|
119 | expect(dummyListenerFunc).to.have.been.calledWith('dummyCommand', {
|
120 | dummyAttribute: true
|
121 | });
|
122 | });
|
123 |
|
124 | it('executes a query and runs the expected callback', () => {
|
125 | let dummyCallback = sinon.spy();
|
126 |
|
127 | both.query('dummyQuery', dummyCallback);
|
128 |
|
129 | both('dummyQuery');
|
130 |
|
131 | expect(dummyCallback).to.have.been.called;
|
132 | });
|
133 | });
|
134 |
|
135 | describe('Shipps callback', () => {
|
136 | it('should executes the callback in commands', () => {
|
137 | let dummyCallback = sinon.spy();
|
138 |
|
139 | both.command('dummyCommand', (name, payload, callback) => {
|
140 | callback();
|
141 | });
|
142 |
|
143 | both('dummyCommand', {}, (dummyArgument) => {
|
144 | dummyCallback();
|
145 | });
|
146 |
|
147 | expect(dummyCallback).to.have.been.called;
|
148 | });
|
149 |
|
150 | it('should executes the callback in queries', () => {
|
151 | let dummyCallback = sinon.spy();
|
152 |
|
153 | both.query('dummyCommand', (name, payload, callback) => {
|
154 | callback();
|
155 | });
|
156 |
|
157 | both('dummyCommand', {}, () => {
|
158 | dummyCallback();
|
159 | });
|
160 |
|
161 | expect(dummyCallback).to.have.been.called;
|
162 | });
|
163 | });
|
164 | });
|