forked from TimBeyer/cls-bluebird
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ff6935
commit cbdf3ee
Showing
6 changed files
with
141 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* cls-bluebird tests | ||
* Utilities | ||
* Functions to run a set of tests relating to testing that callbacks have been bound to CLS context. | ||
* Mixin to Utils prototype. | ||
*/ | ||
|
||
/* global it */ | ||
|
||
// Exports | ||
|
||
module.exports = { | ||
/** | ||
* Run set of tests on a method to ensure callback is always bound to CLS context. | ||
* Function `fn` should take provided `promise` and call the method being tested on it. | ||
* `fn` is called with a `promise` and a `handler` function which should be attached as the callback to the method under test. | ||
* e.g. `promise.then(handler)` | ||
* | ||
* If handler is being attached to catch rejections, `options.catches` should be `true` | ||
* | ||
* @param {Function} fn - Test function | ||
* @param {Object} [options] - Options object | ||
* @param {boolean} [options.catches] - true if method catches rejected promises e.g. `promise.catch()` | ||
* @param {string} [options.name] - Name of test ('binds callback' if not provided) | ||
* @returns {undefined} | ||
*/ | ||
testSetCallbackBound: function(fn, options) { | ||
var u = this; | ||
options = options || {}; | ||
|
||
var makePromise = options.catches ? u.rejectSyncMethod() : u.resolveSyncMethod(); | ||
|
||
it(options.name || 'binds callback', function(done) { | ||
var p = makePromise(); | ||
u.runInContext(function(context) { | ||
u.checkBound(function(handler) { | ||
fn(p, handler); | ||
}, context, done); | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* cls-bluebird tests | ||
* Utilities | ||
* Functions to run a set of tests relating to testing that callbacks are run sync/async. | ||
* Mixin to Utils prototype. | ||
*/ | ||
|
||
/* global describe, it */ | ||
|
||
// Exports | ||
|
||
module.exports = { | ||
/** | ||
* Run set of tests on a method to ensure always calls callback asynchronously. | ||
* Function `fn` should take provided `promise` and call the method being tested on it. | ||
* `fn` is called with a `promise` and a `handler` function which should be attached as the callback to the method under test. | ||
* e.g. `promise.then(handler)` | ||
* | ||
* If handler is being attached to catch rejections, `options.catches` should be `true` | ||
* | ||
* @param {Function} fn - Test function | ||
* @param {Object} [options] - Options object | ||
* @param {boolean} [options.catches] - true if method catches rejected promises e.g. `promise.catch()` | ||
* @returns {undefined} | ||
*/ | ||
testSetCallbackAsync: function(fn, options) { | ||
var u = this; | ||
options = options || {}; | ||
|
||
var makePromiseSync = options.catches ? u.rejectSyncMethod() : u.resolveSyncMethod(), | ||
makePromiseAsync = options.catches ? u.rejectAsyncMethod() : u.resolveAsyncMethod(); | ||
|
||
describe('attached sync to', function() { | ||
it('settled promise', function(done) { | ||
var p = makePromiseSync(); | ||
u.checkAsync(function(handler) { | ||
fn(p, handler); | ||
}, done); | ||
}); | ||
|
||
it('pending promise', function(done) { | ||
var p = makePromiseAsync(); | ||
u.checkAsync(function(handler) { | ||
fn(p, handler); | ||
}, done); | ||
}); | ||
}); | ||
|
||
describe('attached async to', function() { | ||
it('settled promise', function(done) { | ||
var p = makePromiseSync(); | ||
u.suppressUnhandledRejections(p); | ||
setImmediate(function() { | ||
u.checkAsync(function(handler) { | ||
fn(p, handler); | ||
}, done); | ||
}); | ||
}); | ||
|
||
it('pending promise', function(done) { | ||
var p = makePromiseAsync(); | ||
u.suppressUnhandledRejections(p); | ||
setImmediate(function() { | ||
u.checkAsync(function(handler) { | ||
fn(p, handler); | ||
}, done); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters