Skip to content

Commit

Permalink
fix value of 'this' in callbacks, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
makeusabrew committed Feb 22, 2015
1 parent f9e368f commit 2266385
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 12 deletions.
13 changes: 7 additions & 6 deletions bootbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
*/
options.buttons.ok.callback = options.onEscape = function() {
if ($.isFunction(options.callback)) {
return options.callback();
return options.callback.call(this);
}
return true;
};
Expand All @@ -324,11 +324,11 @@
* overrides; undo anything the user tried to set they shouldn't have
*/
options.buttons.cancel.callback = options.onEscape = function() {
return options.callback(false);
return options.callback.call(this, false);
};

options.buttons.confirm.callback = function() {
return options.callback(true);
return options.callback.call(this, true);
};

// confirm specific validation
Expand Down Expand Up @@ -382,7 +382,7 @@
options.message = form;

options.buttons.cancel.callback = options.onEscape = function() {
return options.callback(null);
return options.callback.call(this, null);
};

options.buttons.confirm.callback = function() {
Expand Down Expand Up @@ -413,7 +413,7 @@
break;
}

return options.callback(value);
return options.callback.call(this, value);
};

options.show = false;
Expand Down Expand Up @@ -519,6 +519,8 @@
break;
}

// @TODO provide an attributes option instead
// and simply map that as keys: vals
if (options.placeholder) {
input.attr("placeholder", options.placeholder);
}
Expand Down Expand Up @@ -699,7 +701,6 @@
var callbackKey = $(this).data("bb-handler");

processCallback(e, dialog, callbacks[callbackKey]);

});

dialog.on("click", ".bootbox-close-button", function(e) {
Expand Down
23 changes: 21 additions & 2 deletions tests/alert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

// @TODO: fix this; object is coming back un-jquerified
it.skip("should pass the dialog as `this`", function() {
it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

Expand All @@ -257,6 +256,10 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

it("should hide the modal", function() {
expect(this.hidden).to.have.been.calledWithExactly("hide");
});
Expand All @@ -271,6 +274,10 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

it("should hide the modal", function() {
expect(this.hidden).to.have.been.calledWithExactly("hide");
});
Expand Down Expand Up @@ -299,6 +306,10 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

it("should not hide the modal", function() {
expect(this.hidden).not.to.have.been.called;
});
Expand All @@ -313,6 +324,10 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

it("should not hide the modal", function() {
expect(this.hidden).not.to.have.been.called;
});
Expand All @@ -327,6 +342,10 @@ describe("bootbox.alert", function() {
expect(this.callback).to.have.been.called;
});

it("should pass the dialog as `this`", function() {
expect(this.callback.thisValues[0]).to.equal(this.dialog);
});

it("should not hide the modal", function() {
expect(this.hidden).not.to.have.been.called;
});
Expand Down
18 changes: 18 additions & 0 deletions tests/confirm.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly true

Expand All @@ -172,6 +175,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly false

Expand All @@ -185,6 +191,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly false

Expand All @@ -209,6 +218,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly true

Expand All @@ -222,6 +234,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly false

Expand All @@ -235,6 +250,9 @@ describe "bootbox.confirm", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "with the correct value", ->
expect(@callback).to.have.been.calledWithExactly false

Expand Down
12 changes: 12 additions & 0 deletions tests/dialog.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ describe "bootbox.dialog", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "should hide the modal", ->
expect(@hidden).to.have.been.calledWithExactly "hide"

Expand Down Expand Up @@ -214,6 +217,9 @@ describe "bootbox.dialog", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

describe "when its value is not an object or function", ->
beforeEach ->
@badCreate = =>
Expand Down Expand Up @@ -292,6 +298,9 @@ describe "bootbox.dialog", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "should hide the modal", ->
expect(@hidden).to.have.been.calledWithExactly "hide"

Expand All @@ -311,6 +320,9 @@ describe "bootbox.dialog", ->
it "should invoke the callback", ->
expect(@callback).to.have.been.called

it "should pass the dialog as `this`", ->
expect(@callback.thisValues[0]).to.equal @dialog

it "should not hide the modal", ->
expect(@hidden).not.to.have.been.called

Expand Down
Loading

0 comments on commit 2266385

Please sign in to comment.