Skip to content

Commit

Permalink
Pass correct thisp along in npost and napply.
Browse files Browse the repository at this point in the history
Fixes kriskowal#74, with tests.
  • Loading branch information
domenic committed May 26, 2012
1 parent 8341cd1 commit 0cf6075
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ function nbind(callback /* thisp, ...args*/) {
*/
exports.npost = npost;
function npost(object, name, args) {
return napply(object[name], name, args);
return napply(object[name], object, args);
}

/**
Expand All @@ -1448,7 +1448,7 @@ function npost(object, name, args) {
exports.ninvoke = ninvoke;
function ninvoke(object, name /*, ...args*/) {
var args = array_slice(arguments, 2);
return napply(object[name], name, args);
return napply(object[name], object, args);
}

defend(exports);
Expand Down
68 changes: 68 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,20 @@ describe("thenables", function () {

describe("node support", function () {

var exception = new Error("That is not your favorite color.");

var obj = {
method: function (a, b, c, callback) {
callback(null, a + b + c);
},
thispChecker: function (callback) {
callback(null, this === obj);
},
errorCallbacker: function (a, b, c, callback) {
callback(exception);
}
};

describe("napply", function (done) {

it("fulfills with callback result", function () {
Expand Down Expand Up @@ -729,6 +743,60 @@ describe("node support", function () {

});

describe("npost", function (done) {

it("fulfills with callback result", function () {
return Q.npost(obj, "method", [1, 2, 3])
.then(function (sum) {
expect(sum).toEqual(6);
});
});

it("gets the correct thisp", function () {
return Q.npost(obj, "thispChecker", [])
.then(function (result) {
expect(result).toBe(true);
});
});

it("rejects with callback error", function () {
return Q.npost(obj, "errorCallbacker", [1, 2, 3])
.then(function (sum) {
expect("blue").toBe("no, yellow!");
}, function (_exception) {
expect(_exception).toBe(exception);
});
});

});

describe("ninvoke", function (done) {

it("fulfills with callback result", function () {
return Q.ninvoke(obj, "method", 1, 2, 3)
.then(function (sum) {
expect(sum).toEqual(6);
});
});

it("gets the correct thisp", function () {
return Q.ninvoke(obj, "thispChecker")
.then(function (result) {
expect(result).toBe(true);
});
});

it("rejects with callback error", function () {
return Q.ninvoke(obj, "errorCallbacker", 1, 2, 3)
.then(function (sum) {
expect("blue").toBe("no, yellow!");
}, function (_exception) {
expect(_exception).toBe(exception);
});
});

});

describe("deferred.makeNodeResolver", function () {

it("fulfills a promise", function () {
Expand Down

0 comments on commit 0cf6075

Please sign in to comment.