summaryrefslogtreecommitdiff
path: root/test/ensureAsync.js
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-06-02 18:35:23 -0700
committerAlexander Early <alexander.early@gmail.com>2018-06-02 18:35:32 -0700
commitbd86f42a7d71552d9a502b50235ffc090a1b4a98 (patch)
treef5170115927ad4097f1a393d34b8a52f5c9bbfeb /test/ensureAsync.js
parentb149f7dcc7daba4d7bac5a313bdf6d1a85210cb1 (diff)
downloadasync-bd86f42a7d71552d9a502b50235ffc090a1b4a98.tar.gz
move mocha_tests/ to test/
Diffstat (limited to 'test/ensureAsync.js')
-rw-r--r--test/ensureAsync.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/ensureAsync.js b/test/ensureAsync.js
new file mode 100644
index 0000000..89659e2
--- /dev/null
+++ b/test/ensureAsync.js
@@ -0,0 +1,85 @@
+var async = require('../lib');
+var expect = require('chai').expect;
+var assert = require('assert');
+
+describe('ensureAsync', function() {
+ var passContext = function(cb) {
+ cb(this);
+ };
+
+ it('defer sync functions', function(done) {
+ var sync = true;
+ async.ensureAsync(function (arg1, arg2, cb) {
+ expect(arg1).to.equal(1);
+ expect(arg2).to.equal(2);
+ cb(null, 4, 5);
+ })(1, 2, function (err, arg4, arg5) {
+ expect(err).to.equal(null);
+ expect(arg4).to.equal(4);
+ expect(arg5).to.equal(5);
+ assert(!sync, 'callback called on same tick');
+ done();
+ });
+ sync = false;
+ });
+
+ it('do not defer async functions', function(done) {
+ var sync = false;
+ async.ensureAsync(function (arg1, arg2, cb) {
+ expect(arg1).to.equal(1);
+ expect(arg2).to.equal(2);
+ async.setImmediate(function () {
+ sync = true;
+ cb(null, 4, 5);
+ sync = false;
+ });
+ })(1, 2, function (err, arg4, arg5) {
+ expect(err).to.equal(null);
+ expect(arg4).to.equal(4);
+ expect(arg5).to.equal(5);
+ assert(sync, 'callback called on next tick');
+ done();
+ });
+ });
+
+ it('double wrapping', function(done) {
+ var sync = true;
+ async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) {
+ expect(arg1).to.equal(1);
+ expect(arg2).to.equal(2);
+ cb(null, 4, 5);
+ }))(1, 2, function (err, arg4, arg5) {
+ expect(err).to.equal(null);
+ expect(arg4).to.equal(4);
+ expect(arg5).to.equal(5);
+ assert(!sync, 'callback called on same tick');
+ done();
+ });
+ sync = false;
+ });
+
+
+ it('should propely bind context to the wrapped function', function(done) {
+
+ // call bind after wrapping with ensureAsync
+ var context = {context: "post"};
+ var postBind = async.ensureAsync(passContext);
+ postBind = postBind.bind(context);
+ postBind(function(ref) {
+ expect(ref).to.equal(context);
+ done();
+ });
+ });
+
+ it('should not override the bound context of a function when wrapping', function(done) {
+
+ // call bind before wrapping with ensureAsync
+ var context = {context: "pre"};
+ var preBind = passContext.bind(context);
+ preBind = async.ensureAsync(preBind);
+ preBind(function(ref) {
+ expect(ref).to.equal(context);
+ done();
+ });
+ });
+});