1 var assert = require('assert');
2 var Kareem = require('../');
4 describe('wrap()', function() {
7 beforeEach(function() {
11 it('handles pre errors', function(done) {
12 hooks.pre('cook', function(done) {
16 hooks.post('cook', function(obj) {
20 var obj = { bacon: 0, eggs: 0 };
23 args.push(function(error, result) {
24 assert.equal('error!', error);
26 assert.equal(undefined, obj.tofu);
32 function(o, callback) {
33 // Should never get called
41 it('handles pre errors when no callback defined', function(done) {
42 hooks.pre('cook', function(done) {
46 hooks.post('cook', function(obj) {
50 var obj = { bacon: 0, eggs: 0 };
56 function(o, callback) {
57 // Should never get called
71 it('handles errors in wrapped function', function(done) {
72 hooks.pre('cook', function(done) {
76 hooks.post('cook', function(obj) {
80 var obj = { bacon: 0, eggs: 0 };
83 args.push(function(error, result) {
84 assert.equal('error!', error);
86 assert.equal(undefined, obj.tofu);
92 function(o, callback) {
99 it('handles errors in post', function(done) {
100 hooks.pre('cook', function(done) {
104 hooks.post('cook', function(obj, callback) {
109 var obj = { bacon: 0, eggs: 0 };
112 args.push(function(error, result) {
113 assert.equal('error!', error);
115 assert.equal('no', obj.tofu);
121 function(o, callback) {
128 it('defers errors to post hooks if enabled', function(done) {
129 hooks.pre('cook', function(done) {
130 done(new Error('fail'));
133 hooks.post('cook', function(error, callback) {
134 callback(new Error('another error occurred'));
138 args.push(function(error) {
139 assert.equal(error.message, 'another error occurred');
151 { useErrorHandlers: true });
154 it('error handlers with no callback', function(done) {
155 hooks.pre('cook', function(done) {
156 done(new Error('fail'));
159 hooks.post('cook', function(error, callback) {
160 assert.equal(error.message, 'fail');
174 { useErrorHandlers: true });
177 it('error handlers with no error', function(done) {
178 hooks.post('cook', function(error, callback) {
179 callback(new Error('another error occurred'));
183 args.push(function(error) {
184 assert.ifError(error);
195 { useErrorHandlers: true });
198 it('works with no args', function(done) {
199 hooks.pre('cook', function(done) {
203 hooks.post('cook', function(callback) {
208 var obj = { bacon: 0, eggs: 0 };
222 assert.equal('no', obj.tofu);
228 it('handles pre errors with no args', function(done) {
229 hooks.pre('cook', function(done) {
233 hooks.post('cook', function(callback) {
238 var obj = { bacon: 0, eggs: 0 };
252 assert.equal(undefined, obj.tofu);
258 it('handles wrapped function errors with no args', function(done) {
259 hooks.pre('cook', function(done) {
264 hooks.post('cook', function(callback) {
269 var obj = { bacon: 0, eggs: 0 };
283 assert.equal(false, obj.waffles);
284 assert.equal(undefined, obj.tofu);
290 it('handles post errors with no args', function(done) {
291 hooks.pre('cook', function(done) {
296 hooks.post('cook', function(callback) {
301 var obj = { bacon: 0, eggs: 0 };
315 assert.equal(false, obj.waffles);
316 assert.equal('no', obj.tofu);
322 it('can use legacy post behavior', function(done) {
324 hooks.post('cook', function(callback) {
329 var args = [function(error) {
330 assert.equal(called, 0);
332 setTimeout(function() {
333 assert.equal(called, 1);