Permalink
Browse files

add tests for new backdrop behaviour

  • Loading branch information...
1 parent 5477545 commit 199feccf85b09a3698868315f5a2d3f8e60e7d0d @makeusabrew committed Feb 22, 2015
Showing with 116 additions and 3 deletions.
  1. +1 −0 .jshintrc
  2. +7 −1 bootbox.js
  3. +1 −1 karma-base.conf.js
  4. +107 −1 tests/bootbox.test.js
View
@@ -11,6 +11,7 @@
"globals": {
"beforeEach": true,
+ "afterEach": true,
"describe": true,
"it": true,
"define": true,
View
@@ -683,9 +683,15 @@
// However, we still want to sort of respect true
// and invoke the escape mechanism instead
dialog.on("click.dismiss.bs.modal", function(e) {
- if (e.target !== e.currentTarget) {
+ // @NOTE: the target varies in >= 3.3.x releases since the modal backdrop
+ // moved *inside* the outer dialog rather than *alongside* it
+ // As such backdrop: true only currently works with 3.3.x Bootstrap releases
+ var target = options.backdrop ? dialog.children(".modal-backdrop").get(0) : e.currentTarget;
+
+ if (e.target !== target) {
return;
}
+
dialog.trigger("escape.close.bb");
});
}
View
@@ -37,7 +37,7 @@ module.exports = function(params) {
singleRun: true,
coverageReporter: {
- type: "cobertura",
+ type: "html",
dir: "tests/coverage"
},
View
@@ -30,15 +30,20 @@ describe("Bootbox", function() {
beforeEach(function() {
this.dialog = bootbox.alert("hi");
+ this.removed = sinon.stub(this.dialog, "remove");
+
this.e = function(target) {
- this.removed = sinon.stub(this.dialog, "remove");
$(this.dialog).trigger($.Event("hidden.bs.modal", {
target: target
}));
};
});
+ afterEach(function() {
+ this.removed.restore();
+ });
+
describe("when triggered with the wrong target", function() {
beforeEach(function() {
this.e({an: "object"});
@@ -160,4 +165,105 @@ describe("Bootbox", function() {
});
});
});
+
+ describe("backdrop variations", function() {
+ beforeEach(function() {
+ this.e = function(target) {
+ $(this.dialog).trigger($.Event("click.dismiss.bs.modal", {
+ target: target
+ }));
+ };
+ });
+
+ describe("with the default value", function() {
+ beforeEach(function() {
+ this.callback = sinon.spy();
+ this.dialog = bootbox.alert("hi", this.callback);
+ });
+
+ describe("When triggering the backdrop click dismiss event", function() {
+ beforeEach(function() {
+ this.e({an: "object"});
+ });
+
+ it("does not invoke the callback", function() {
+ expect(this.callback).not.to.have.been.called;
+ });
+ });
+ });
+
+ describe("when set to false", function() {
+ beforeEach(function() {
+ this.callback = sinon.spy();
+ this.dialog = bootbox.alert({
+ message: "hi",
+ callback: this.callback,
+ backdrop: false
+ });
+ });
+
+ describe("When triggering the backdrop click dismiss event", function() {
+ describe("With the wrong target", function() {
+ beforeEach(function() {
+ this.e({an: "object"});
+ });
+
+ it("does not invoke the callback", function() {
+ expect(this.callback).not.to.have.been.called;
+ });
+ });
+
+ describe("With the correct target", function() {
+ beforeEach(function() {
+ this.e(this.dialog.get(0));
+ });
+
+ it("invokes the callback", 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);
+ });
+ });
+ });
+ });
+
+ describe("when set to true", function() {
+ beforeEach(function() {
+ this.callback = sinon.spy();
+ this.dialog = bootbox.alert({
+ message: "hi",
+ callback: this.callback,
+ backdrop: true
+ });
+ });
+
+ describe("When triggering the backdrop click dismiss event", function() {
+ describe("With the wrong target", function() {
+ beforeEach(function() {
+ this.e({an: "object"});
+ });
+
+ it("does not invoke the callback", function() {
+ expect(this.callback).not.to.have.been.called;
+ });
+ });
+
+ describe("With the correct target", function() {
+ beforeEach(function() {
+ this.e(this.dialog.children(".modal-backdrop").get(0));
+ });
+
+ it("invokes the callback", 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);
+ });
+ });
+ });
+ });
+ });
});

0 comments on commit 199fecc

Please sign in to comment.