HTMLify
post.test.js
Views: 6 | Author: cody
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 'use strict'; const assert = require('assert'); const 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('unshift', function() { var f1 = function() {}; var f2 = function() {}; hooks.post('cook', f1); hooks.post('cook', f2, true); assert.strictEqual(hooks._posts.get('cook')[0].fn, f2); assert.strictEqual(hooks._posts.get('cook')[1].fn, f1); }); it('arbitrary options', function() { const f1 = function() {}; const f2 = function() {}; hooks.post('cook', { foo: 'bar' }, f1); hooks.post('cook', { bar: 'baz' }, f2, true); assert.equal(hooks._posts.get('cook')[1].foo, 'bar'); assert.equal(hooks._posts.get('cook')[0].bar, 'baz'); }); it('throws error if no function', function() { assert.throws(() => hooks.post('test'), /got "undefined"/); }); 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(); }); }); it('supports returning a promise', function(done) { var calledPost = 0; hooks.post('cook', function() { return new Promise(resolve => { setTimeout(() => { ++calledPost; resolve(); }, 100); }); }); hooks.execPost('cook', null, [], {}, function(error) { assert.ifError(error); assert.equal(calledPost, 1); 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); }); }); }); |