9fcbb0ed21be5e3ccf4c602bfe4d2abb359d6232
[aai/esr-gui.git] /
1 var assert = require('assert');
2 var Kareem = require('../');
3
4 describe('execPost', function() {
5   var hooks;
6
7   beforeEach(function() {
8     hooks = new Kareem();
9   });
10
11   it('handles errors', function(done) {
12     hooks.post('cook', function(eggs, callback) {
13       callback('error!');
14     });
15
16     hooks.execPost('cook', null, [4], function(error, eggs) {
17       assert.equal('error!', error);
18       assert.ok(!eggs);
19       done();
20     });
21   });
22
23   it('multiple posts', function(done) {
24     hooks.post('cook', function(eggs, callback) {
25       setTimeout(
26         function() {
27           callback();
28         },
29         5);
30     });
31
32     hooks.post('cook', function(eggs, callback) {
33       setTimeout(
34         function() {
35           callback();
36         },
37         5);
38     });
39
40     hooks.execPost('cook', null, [4], function(error, eggs) {
41       assert.ifError(error);
42       assert.equal(4, eggs);
43       done();
44     });
45   });
46
47   it('error posts', function(done) {
48     var called = {};
49     hooks.post('cook', function(eggs, callback) {
50       called.first = true;
51       callback();
52     });
53
54     hooks.post('cook', function(eggs, callback) {
55       called.second = true;
56       callback(new Error('fail'));
57     });
58
59     hooks.post('cook', function(eggs, callback) {
60       assert.ok(false);
61     });
62
63     hooks.post('cook', function(error, eggs, callback) {
64       called.fourth = true;
65       assert.equal(error.message, 'fail');
66       callback(new Error('fourth'));
67     });
68
69     hooks.post('cook', function(error, eggs, callback) {
70       called.fifth = true;
71       assert.equal(error.message, 'fourth');
72       callback(new Error('fifth'));
73     });
74
75     hooks.execPost('cook', null, [4], function(error, eggs) {
76       assert.ok(error);
77       assert.equal(error.message, 'fifth');
78       assert.deepEqual(called, {
79         first: true,
80         second: true,
81         fourth: true,
82         fifth: true
83       });
84       done();
85     });
86   });
87
88   it('error posts with initial error', function(done) {
89     var called = {};
90
91     hooks.post('cook', function(eggs, callback) {
92       assert.ok(false);
93     });
94
95     hooks.post('cook', function(error, eggs, callback) {
96       called.second = true;
97       assert.equal(error.message, 'fail');
98       callback(new Error('second'));
99     });
100
101     hooks.post('cook', function(error, eggs, callback) {
102       called.third = true;
103       assert.equal(error.message, 'second');
104       callback(new Error('third'));
105     });
106
107     hooks.post('cook', function(error, eggs, callback) {
108       called.fourth = true;
109       assert.equal(error.message, 'third');
110       callback();
111     });
112
113     var options = { error: new Error('fail') };
114     hooks.execPost('cook', null, [4], options, function(error, eggs) {
115       assert.ok(error);
116       assert.equal(error.message, 'third');
117       assert.deepEqual(called, {
118         second: true,
119         third: true,
120         fourth: true
121       });
122       done();
123     });
124   });
125 });
126
127 describe('execPostSync', function() {
128   var hooks;
129
130   beforeEach(function() {
131     hooks = new Kareem();
132   });
133
134   it('executes hooks synchronously', function() {
135     var execed = {};
136
137     hooks.post('cook', function() {
138       execed.first = true;
139     });
140
141     hooks.post('cook', function() {
142       execed.second = true;
143     });
144
145     hooks.execPostSync('cook', null);
146     assert.ok(execed.first);
147     assert.ok(execed.second);
148   });
149
150   it('works with no hooks specified', function() {
151     assert.doesNotThrow(function() {
152       hooks.execPostSync('cook', null);
153     });
154   });
155 });