Path: blob/master/node_modules/@protobufjs/aspromise/tests/index.js
1129 views
var tape = require("tape");12var asPromise = require("..");34tape.test("aspromise", function(test) {56test.test(this.name + " - resolve", function(test) {78function fn(arg1, arg2, callback) {9test.equal(this, ctx, "function should be called with this = ctx");10test.equal(arg1, 1, "function should be called with arg1 = 1");11test.equal(arg2, 2, "function should be called with arg2 = 2");12callback(null, arg2);13}1415var ctx = {};1617var promise = asPromise(fn, ctx, 1, 2);18promise.then(function(arg2) {19test.equal(arg2, 2, "promise should be resolved with arg2 = 2");20test.end();21}).catch(function(err) {22test.fail("promise should not be rejected (" + err + ")");23});24});2526test.test(this.name + " - reject", function(test) {2728function fn(arg1, arg2, callback) {29test.equal(this, ctx, "function should be called with this = ctx");30test.equal(arg1, 1, "function should be called with arg1 = 1");31test.equal(arg2, 2, "function should be called with arg2 = 2");32callback(arg1);33}3435var ctx = {};3637var promise = asPromise(fn, ctx, 1, 2);38promise.then(function() {39test.fail("promise should not be resolved");40}).catch(function(err) {41test.equal(err, 1, "promise should be rejected with err = 1");42test.end();43});44});4546test.test(this.name + " - resolve twice", function(test) {4748function fn(arg1, arg2, callback) {49test.equal(this, ctx, "function should be called with this = ctx");50test.equal(arg1, 1, "function should be called with arg1 = 1");51test.equal(arg2, 2, "function should be called with arg2 = 2");52callback(null, arg2);53callback(null, arg1);54}5556var ctx = {};57var count = 0;5859var promise = asPromise(fn, ctx, 1, 2);60promise.then(function(arg2) {61test.equal(arg2, 2, "promise should be resolved with arg2 = 2");62if (++count > 1)63test.fail("promise should not be resolved twice");64test.end();65}).catch(function(err) {66test.fail("promise should not be rejected (" + err + ")");67});68});6970test.test(this.name + " - reject twice", function(test) {7172function fn(arg1, arg2, callback) {73test.equal(this, ctx, "function should be called with this = ctx");74test.equal(arg1, 1, "function should be called with arg1 = 1");75test.equal(arg2, 2, "function should be called with arg2 = 2");76callback(arg1);77callback(arg2);78}7980var ctx = {};81var count = 0;8283var promise = asPromise(fn, ctx, 1, 2);84promise.then(function() {85test.fail("promise should not be resolved");86}).catch(function(err) {87test.equal(err, 1, "promise should be rejected with err = 1");88if (++count > 1)89test.fail("promise should not be rejected twice");90test.end();91});92});9394test.test(this.name + " - reject error", function(test) {9596function fn(callback) {97test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");98throw 3;99}100101var promise = asPromise(fn, null);102promise.then(function() {103test.fail("promise should not be resolved");104}).catch(function(err) {105test.equal(err, 3, "promise should be rejected with err = 3");106test.end();107});108});109110test.test(this.name + " - reject and error", function(test) {111112function fn(callback) {113callback(3);114throw 4;115}116117var count = 0;118119var promise = asPromise(fn, null);120promise.then(function() {121test.fail("promise should not be resolved");122}).catch(function(err) {123test.equal(err, 3, "promise should be rejected with err = 3");124if (++count > 1)125test.fail("promise should not be rejected twice");126test.end();127});128});129});130131132