Skip to content

Commit

Permalink
Tests WIP 11
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 3, 2016
1 parent 6ff6935 commit cbdf3ee
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 139 deletions.
53 changes: 7 additions & 46 deletions test/methods/catch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Tests for .catch()
*/

/* global describe, it */
/* global describe */

// Imports
var runTests = require('../support');
Expand All @@ -18,51 +18,12 @@ runTests('.catch()', function(Promise, u) { // jshint ignore:line
});

describe('calls callback asynchronously when handler', function() {
describe('attached sync to', function() {
it('settled promise', function(done) {
var p = u.rejectSync();
u.checkAsync(function(handler) {
p.catch(handler);
}, done);
});

it('pending promise', function(done) {
var p = u.rejectAsync();
u.checkAsync(function(handler) {
p.catch(handler);
}, done);
});
});

describe('attached async to', function() {
it('settled promise', function(done) {
var p = u.rejectSync();
u.suppressUnhandledRejections(p);
setImmediate(function() {
u.checkAsync(function(handler) {
p.catch(handler);
}, done);
});
});

it('pending promise', function(done) {
var p = u.rejectAsync();
u.suppressUnhandledRejections(p);
setImmediate(function() {
u.checkAsync(function(handler) {
p.catch(handler);
}, done);
});
});
});
u.testSetCallbackAsync(function(p, handler) {
p.catch(handler);
}, {catches: true});
});

it('binds callback', function(done) {
var p = u.rejectSync();
u.runInContext(function(context) {
u.checkBound(function(handler) {
p.catch(handler);
}, context, done);
});
});
u.testSetCallbackBound(function(p, handler) {
p.catch(handler);
}, {catches: true});
});
100 changes: 12 additions & 88 deletions test/methods/then.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Tests for .then()
*/

/* global describe, it */
/* global describe */

// Imports
var runTests = require('../support');
Expand All @@ -27,101 +27,25 @@ runTests('.then()', function(Promise, u) { // jshint ignore:line

describe('calls callback asynchronously when', function() {
describe('resolve handler', function() {
describe('attached sync to', function() {
it('settled promise', function(done) {
var p = u.resolveSync();
u.checkAsync(function(handler) {
p.then(handler);
}, done);
});

it('pending promise', function(done) {
var p = u.resolveAsync();
u.checkAsync(function(handler) {
p.then(handler);
}, done);
});
});

describe('attached async to', function() {
it('settled promise', function(done) {
var p = u.resolveSync();
setImmediate(function() {
u.checkAsync(function(handler) {
p.then(handler);
}, done);
});
});

it('pending promise', function(done) {
var p = u.resolveAsync();
setImmediate(function() {
u.checkAsync(function(handler) {
p.then(handler);
}, done);
});
});
u.testSetCallbackAsync(function(p, handler) {
p.then(handler);
});
});

describe('reject handler', function() {
describe('attached sync to', function() {
it('settled promise', function(done) {
var p = u.rejectSync();
u.checkAsync(function(handler) {
p.then(undefined, handler);
}, done);
});

it('pending promise', function(done) {
var p = u.rejectAsync();
u.checkAsync(function(handler) {
p.then(undefined, handler);
}, done);
});
});

describe('attached async to', function() {
it('settled promise', function(done) {
var p = u.rejectSync();
u.suppressUnhandledRejections(p);
setImmediate(function() {
u.checkAsync(function(handler) {
p.then(undefined, handler);
}, done);
});
});

it('pending promise', function(done) {
var p = u.rejectAsync();
u.suppressUnhandledRejections(p);
setImmediate(function() {
u.checkAsync(function(handler) {
p.then(undefined, handler);
}, done);
});
});
});
u.testSetCallbackAsync(function(p, handler) {
p.then(undefined, handler);
}, {catches: true});
});
});

describe('binds callback on', function() {
it('resolve handler', function(done) {
var p = u.resolveSync();
u.runInContext(function(context) {
u.checkBound(function(handler) {
p.then(handler);
}, context, done);
});
});
u.testSetCallbackBound(function(p, handler) {
p.then(handler);
}, {name: 'resolve handler'});

it('reject handler', function(done) {
var p = u.rejectSync();
u.runInContext(function(context) {
u.checkBound(function(handler) {
p.then(undefined, handler);
}, context, done);
});
});
u.testSetCallbackBound(function(p, handler) {
p.then(undefined, handler);
}, {name: 'reject handler', catches: true});
});
});
42 changes: 42 additions & 0 deletions test/support/testSets/binding.js
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);
});
});
}
};
6 changes: 3 additions & 3 deletions test/support/testSets.js → test/support/testSets/promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* cls-bluebird tests
* Utilities
* Functions to run a set of tests.
* Functions to run a set of tests relating to testing that methods return a promise of correct type.
* Mixin to Utils prototype.
*/

Expand All @@ -24,7 +24,7 @@ module.exports = {
*
* @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 {boolean} [options.catches] - true if method catches rejected promises e.g. `promise.catch()`
* @returns {undefined}
*/
testSetProtoMethodReturnsPromise: function(fn, options) {
Expand Down Expand Up @@ -150,7 +150,7 @@ module.exports = {
});
},

/*
/**
* Run set of tests on a value-taking method to ensure always returns a promise
* inherited from correct Promise constructor.
*
Expand Down
71 changes: 71 additions & 0 deletions test/support/testSets/syncAsync.js
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);
});
});
});
}
};
8 changes: 6 additions & 2 deletions test/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var _ = require('lodash');
// Imports
var promises = require('./promises'),
checks = require('./checks'),
testSets = require('./testSets');
testSetsPromise = require('./testSets/promise'),
testSetsSyncAsync = require('./testSets/syncAsync'),
testSetsBinding = require('./testSets/binding');

// Exports

Expand Down Expand Up @@ -128,6 +130,8 @@ Utils.prototype = {
// mixins
_.extend(Utils.prototype, promises);
_.extend(Utils.prototype, checks);
_.extend(Utils.prototype, testSets);
_.extend(Utils.prototype, testSetsPromise);
_.extend(Utils.prototype, testSetsSyncAsync);
_.extend(Utils.prototype, testSetsBinding);

module.exports = Utils;

0 comments on commit cbdf3ee

Please sign in to comment.