adbbc398651ce2b6cc03993d584d7ae8235e59df
[aai/esr-gui.git] /
1 var assert = require('assert');
2 var Kareem = require('../');
3
4 describe('execPre', function() {
5   var hooks;
6
7   beforeEach(function() {
8     hooks = new Kareem();
9   });
10
11   it('handles errors with multiple pres', function(done) {
12     var execed = {};
13
14     hooks.pre('cook', function(done) {
15       execed.first = true;
16       done();
17     });
18
19     hooks.pre('cook', function(done) {
20       execed.second = true;
21       done('error!');
22     });
23
24     hooks.pre('cook', function(done) {
25       execed.third = true;
26       done();
27     });
28
29     hooks.execPre('cook', null, function(err) {
30       assert.equal('error!', err);
31       assert.equal(2, Object.keys(execed).length);
32       assert.ok(execed.first);
33       assert.ok(execed.second);
34       done();
35     });
36   });
37
38   it('handles async errors', function(done) {
39     var execed = {};
40
41     hooks.pre('cook', true, function(next, done) {
42       execed.first = true;
43       setTimeout(
44         function() {
45           done('error!');
46         },
47         5);
48
49       next();
50     });
51
52     hooks.pre('cook', true, function(next, done) {
53       execed.second = true;
54       setTimeout(
55         function() {
56           done('other error!');
57         },
58         10);
59
60       next();
61     });
62
63     hooks.execPre('cook', null, function(err) {
64       assert.equal('error!', err);
65       assert.equal(2, Object.keys(execed).length);
66       assert.ok(execed.first);
67       assert.ok(execed.second);
68       done();
69     });
70   });
71
72   it('handles async errors in next()', function(done) {
73     var execed = {};
74
75     hooks.pre('cook', true, function(next, done) {
76       execed.first = true;
77       setTimeout(
78         function() {
79           done('other error!');
80         },
81         15);
82
83       next();
84     });
85
86     hooks.pre('cook', true, function(next, done) {
87       execed.second = true;
88       setTimeout(
89         function() {
90           next('error!');
91           done('another error!');
92         },
93         5);
94     });
95
96     hooks.execPre('cook', null, function(err) {
97       assert.equal('error!', err);
98       assert.equal(2, Object.keys(execed).length);
99       assert.ok(execed.first);
100       assert.ok(execed.second);
101       done();
102     });
103   });
104
105   it('handles async errors in next() when already done', function(done) {
106     var execed = {};
107
108     hooks.pre('cook', true, function(next, done) {
109       execed.first = true;
110       setTimeout(
111         function() {
112           done('other error!');
113         },
114         5);
115
116       next();
117     });
118
119     hooks.pre('cook', true, function(next, done) {
120       execed.second = true;
121       setTimeout(
122         function() {
123           next('error!');
124           done('another error!');
125         },
126         25);
127     });
128
129     hooks.execPre('cook', null, function(err) {
130       assert.equal('other error!', err);
131       assert.equal(2, Object.keys(execed).length);
132       assert.ok(execed.first);
133       assert.ok(execed.second);
134       done();
135     });
136   });
137
138   it('returns correct error when async pre errors', function(done) {
139     var execed = {};
140
141     hooks.pre('cook', true, function(next, done) {
142       execed.first = true;
143       setTimeout(
144         function() {
145           done('other error!');
146         },
147         5);
148
149       next();
150     });
151
152     hooks.pre('cook', function(next) {
153       execed.second = true;
154       setTimeout(
155         function() {
156           next('error!');
157         },
158         15);
159     });
160
161     hooks.execPre('cook', null, function(err) {
162       assert.equal('other error!', err);
163       assert.equal(2, Object.keys(execed).length);
164       assert.ok(execed.first);
165       assert.ok(execed.second);
166       done();
167     });
168   });
169
170   it('lets async pres run when fully sync pres are done', function(done) {
171     var execed = {};
172
173     hooks.pre('cook', true, function(next, done) {
174       execed.first = true;
175       setTimeout(
176         function() {
177           done();
178         },
179         5);
180
181       next();
182     });
183
184     hooks.pre('cook', function() {
185       execed.second = true;
186     });
187
188     hooks.execPre('cook', null, function(err) {
189       assert.ifError(err);
190       assert.equal(2, Object.keys(execed).length);
191       assert.ok(execed.first);
192       assert.ok(execed.second);
193       done();
194     });
195   });
196
197   it('allows passing arguments to the next pre', function(done) {
198     var execed = {};
199
200     hooks.pre('cook', function(next) {
201       execed.first = true;
202       next(null, 'test');
203     });
204
205     hooks.pre('cook', function(next, p) {
206       execed.second = true;
207       assert.equal(p, 'test');
208       next();
209     });
210
211     hooks.pre('cook', function(next, p) {
212       execed.third = true;
213       assert.ok(!p);
214       next();
215     });
216
217     hooks.execPre('cook', null, function(err) {
218       assert.ifError(err);
219       assert.equal(3, Object.keys(execed).length);
220       assert.ok(execed.first);
221       assert.ok(execed.second);
222       assert.ok(execed.third);
223       done();
224     });
225   });
226 });
227
228 describe('execPreSync', function() {
229   var hooks;
230
231   beforeEach(function() {
232     hooks = new Kareem();
233   });
234
235   it('executes hooks synchronously', function() {
236     var execed = {};
237
238     hooks.pre('cook', function() {
239       execed.first = true;
240     });
241
242     hooks.pre('cook', function() {
243       execed.second = true;
244     });
245
246     hooks.execPreSync('cook', null);
247     assert.ok(execed.first);
248     assert.ok(execed.second);
249   });
250
251   it('works with no hooks specified', function() {
252     assert.doesNotThrow(function() {
253       hooks.execPreSync('cook', null);
254     });
255   });
256 });