post.test.js
3.41 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var assert = require('assert');
var Kareem = require('../');
describe('execPost', function() {
var hooks;
beforeEach(function() {
hooks = new Kareem();
});
it('handles errors', function(done) {
hooks.post('cook', function(eggs, callback) {
callback('error!');
});
hooks.execPost('cook', null, [4], function(error, eggs) {
assert.equal('error!', error);
assert.ok(!eggs);
done();
});
});
it('multiple posts', function(done) {
hooks.post('cook', function(eggs, callback) {
setTimeout(
function() {
callback();
},
5);
});
hooks.post('cook', function(eggs, callback) {
setTimeout(
function() {
callback();
},
5);
});
hooks.execPost('cook', null, [4], function(error, eggs) {
assert.ifError(error);
assert.equal(4, eggs);
done();
});
});
it('error posts', function(done) {
var called = {};
hooks.post('cook', function(eggs, callback) {
called.first = true;
callback();
});
hooks.post('cook', function(eggs, callback) {
called.second = true;
callback(new Error('fail'));
});
hooks.post('cook', function(eggs, callback) {
assert.ok(false);
});
hooks.post('cook', function(error, eggs, callback) {
called.fourth = true;
assert.equal(error.message, 'fail');
callback(new Error('fourth'));
});
hooks.post('cook', function(error, eggs, callback) {
called.fifth = true;
assert.equal(error.message, 'fourth');
callback(new Error('fifth'));
});
hooks.execPost('cook', null, [4], function(error, eggs) {
assert.ok(error);
assert.equal(error.message, 'fifth');
assert.deepEqual(called, {
first: true,
second: true,
fourth: true,
fifth: true
});
done();
});
});
it('error posts with initial error', function(done) {
var called = {};
hooks.post('cook', function(eggs, callback) {
assert.ok(false);
});
hooks.post('cook', function(error, eggs, callback) {
called.second = true;
assert.equal(error.message, 'fail');
callback(new Error('second'));
});
hooks.post('cook', function(error, eggs, callback) {
called.third = true;
assert.equal(error.message, 'second');
callback(new Error('third'));
});
hooks.post('cook', function(error, eggs, callback) {
called.fourth = true;
assert.equal(error.message, 'third');
callback();
});
var options = { error: new Error('fail') };
hooks.execPost('cook', null, [4], options, function(error, eggs) {
assert.ok(error);
assert.equal(error.message, 'third');
assert.deepEqual(called, {
second: true,
third: true,
fourth: true
});
done();
});
});
});
describe('execPostSync', function() {
var hooks;
beforeEach(function() {
hooks = new Kareem();
});
it('executes hooks synchronously', function() {
var execed = {};
hooks.post('cook', function() {
execed.first = true;
});
hooks.post('cook', function() {
execed.second = true;
});
hooks.execPostSync('cook', null);
assert.ok(execed.first);
assert.ok(execed.second);
});
it('works with no hooks specified', function() {
assert.doesNotThrow(function() {
hooks.execPostSync('cook', null);
});
});
});