1 var tape = require("tape");
\r
3 var asPromise = require("..");
\r
5 tape.test("aspromise", function(test) {
\r
7 test.test(this.name + " - resolve", function(test) {
\r
9 function fn(arg1, arg2, callback) {
\r
10 test.equal(this, ctx, "function should be called with this = ctx");
\r
11 test.equal(arg1, 1, "function should be called with arg1 = 1");
\r
12 test.equal(arg2, 2, "function should be called with arg2 = 2");
\r
13 callback(null, arg2);
\r
18 var promise = asPromise(fn, ctx, 1, 2);
\r
19 promise.then(function(arg2) {
\r
20 test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
\r
22 }).catch(function(err) {
\r
23 test.fail("promise should not be rejected (" + err + ")");
\r
27 test.test(this.name + " - reject", function(test) {
\r
29 function fn(arg1, arg2, callback) {
\r
30 test.equal(this, ctx, "function should be called with this = ctx");
\r
31 test.equal(arg1, 1, "function should be called with arg1 = 1");
\r
32 test.equal(arg2, 2, "function should be called with arg2 = 2");
\r
38 var promise = asPromise(fn, ctx, 1, 2);
\r
39 promise.then(function() {
\r
40 test.fail("promise should not be resolved");
\r
41 }).catch(function(err) {
\r
42 test.equal(err, 1, "promise should be rejected with err = 1");
\r
47 test.test(this.name + " - resolve twice", function(test) {
\r
49 function fn(arg1, arg2, callback) {
\r
50 test.equal(this, ctx, "function should be called with this = ctx");
\r
51 test.equal(arg1, 1, "function should be called with arg1 = 1");
\r
52 test.equal(arg2, 2, "function should be called with arg2 = 2");
\r
53 callback(null, arg2);
\r
54 callback(null, arg1);
\r
60 var promise = asPromise(fn, ctx, 1, 2);
\r
61 promise.then(function(arg2) {
\r
62 test.equal(arg2, 2, "promise should be resolved with arg2 = 2");
\r
64 test.fail("promise should not be resolved twice");
\r
66 }).catch(function(err) {
\r
67 test.fail("promise should not be rejected (" + err + ")");
\r
71 test.test(this.name + " - reject twice", function(test) {
\r
73 function fn(arg1, arg2, callback) {
\r
74 test.equal(this, ctx, "function should be called with this = ctx");
\r
75 test.equal(arg1, 1, "function should be called with arg1 = 1");
\r
76 test.equal(arg2, 2, "function should be called with arg2 = 2");
\r
84 var promise = asPromise(fn, ctx, 1, 2);
\r
85 promise.then(function() {
\r
86 test.fail("promise should not be resolved");
\r
87 }).catch(function(err) {
\r
88 test.equal(err, 1, "promise should be rejected with err = 1");
\r
90 test.fail("promise should not be rejected twice");
\r
95 test.test(this.name + " - reject error", function(test) {
\r
97 function fn(callback) {
\r
98 test.ok(arguments.length === 1 && typeof callback === "function", "function should be called with just a callback");
\r
102 var promise = asPromise(fn, null);
\r
103 promise.then(function() {
\r
104 test.fail("promise should not be resolved");
\r
105 }).catch(function(err) {
\r
106 test.equal(err, 3, "promise should be rejected with err = 3");
\r
111 test.test(this.name + " - reject and error", function(test) {
\r
113 function fn(callback) {
\r
120 var promise = asPromise(fn, null);
\r
121 promise.then(function() {
\r
122 test.fail("promise should not be resolved");
\r
123 }).catch(function(err) {
\r
124 test.equal(err, 3, "promise should be rejected with err = 3");
\r
126 test.fail("promise should not be rejected twice");
\r