bootbox.test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. describe("Bootbox", function() {
  2. "use strict";
  3. it("is attached to the window object", function() {
  4. expect(window.bootbox).to.be.an("object");
  5. });
  6. it("exposes the correct public API", function() {
  7. expect(bootbox.alert).to.be.a("function");
  8. expect(bootbox.confirm).to.be.a("function");
  9. expect(bootbox.dialog).to.be.a("function");
  10. expect(bootbox.setDefaults).to.be.a("function");
  11. expect(bootbox.hideAll).to.be.a("function");
  12. });
  13. describe("hideAll", function() {
  14. beforeEach(function() {
  15. this.hidden = sinon.spy($.fn, "modal");
  16. bootbox.hideAll();
  17. });
  18. it("should hide all .bootbox modals", function() {
  19. expect(this.hidden).to.have.been.calledWithExactly("hide");
  20. });
  21. });
  22. describe("event listeners", function() {
  23. describe("hidden.bs.modal", function() {
  24. beforeEach(function() {
  25. this.dialog = bootbox.alert("hi");
  26. this.removed = sinon.stub(this.dialog, "remove");
  27. this.e = function(target) {
  28. $(this.dialog).trigger($.Event("hidden.bs.modal", {
  29. target: target
  30. }));
  31. };
  32. });
  33. afterEach(function() {
  34. this.removed.restore();
  35. });
  36. describe("when triggered with the wrong target", function() {
  37. beforeEach(function() {
  38. this.e({an: "object"});
  39. });
  40. it("does not remove the dialog", function() {
  41. expect(this.removed).not.to.have.been.called;
  42. });
  43. });
  44. describe("when triggered with the correct target", function() {
  45. beforeEach(function() {
  46. this.e(this.dialog.get(0));
  47. });
  48. it("removes the dialog", function() {
  49. expect(this.removed).to.have.been.called;
  50. });
  51. });
  52. });
  53. });
  54. describe("If $.fn.modal is undefined", function() {
  55. beforeEach(function() {
  56. this.oldModal = window.jQuery.fn.modal;
  57. window.jQuery.fn.modal = undefined;
  58. });
  59. afterEach(function() {
  60. window.jQuery.fn.modal = this.oldModal;
  61. });
  62. describe("When invoking a dialog", function() {
  63. beforeEach(function() {
  64. try {
  65. bootbox.alert("Hi", function() {});
  66. } catch (e) {
  67. this.e = e;
  68. }
  69. });
  70. it("throws the correct error", function() {
  71. expect(this.e.message).to.contain("$.fn.modal is not defined");
  72. });
  73. });
  74. });
  75. describe("adding and removing locales", function() {
  76. describe("bootbox.addLocale", function() {
  77. describe("with invalid values", function() {
  78. beforeEach(function() {
  79. try {
  80. bootbox.addLocale("xy", {
  81. OK: "BTN1"
  82. });
  83. } catch (e) {
  84. this.e = e;
  85. }
  86. });
  87. it("throws the expected error", function() {
  88. expect(this.e.message).to.equal("Please supply a translation for 'CANCEL'");
  89. });
  90. });
  91. describe("with invalid values", function() {
  92. beforeEach(function() {
  93. bootbox
  94. .addLocale("xy", {
  95. OK: "BTN1",
  96. CANCEL: "BTN2",
  97. CONFIRM: "BTN3"
  98. })
  99. .setLocale("xy");
  100. var d1 = bootbox.alert("foo");
  101. var d2 = bootbox.confirm("foo", function() { return true; });
  102. this.labels = {
  103. ok: d1.find(".btn:first").text(),
  104. cancel: d2.find(".btn:first").text(),
  105. confirm: d2.find(".btn:last").text()
  106. };
  107. });
  108. it("shows the expected OK translation", function() {
  109. expect(this.labels.ok).to.equal("BTN1");
  110. });
  111. it("shows the expected CANCEL translation", function() {
  112. expect(this.labels.cancel).to.equal("BTN2");
  113. });
  114. it("shows the expected PROMPT translation", function() {
  115. expect(this.labels.confirm).to.equal("BTN3");
  116. });
  117. });
  118. });
  119. describe("bootbox.removeLocale", function () {
  120. beforeEach(function () {
  121. bootbox.removeLocale("xy");
  122. var d1 = bootbox.alert("foo");
  123. var d2 = bootbox.confirm("foo", function () { return true; });
  124. this.labels = {
  125. ok: d1.find(".btn:first").text(),
  126. cancel: d2.find(".btn:first").text(),
  127. confirm: d2.find(".btn:last").text()
  128. };
  129. });
  130. it("falls back to the default OK translation", function () {
  131. expect(this.labels.ok).to.equal("OK");
  132. });
  133. it("falls back to the default CANCEL translation", function () {
  134. expect(this.labels.cancel).to.equal("Cancel");
  135. });
  136. it("falls back to the default PROMPT translation", function () {
  137. expect(this.labels.confirm).to.equal("OK");
  138. });
  139. });
  140. });
  141. describe("backdrop variations", function() {
  142. beforeEach(function() {
  143. this.e = function(target) {
  144. $(this.dialog).trigger($.Event("click.dismiss.bs.modal", {
  145. target: target
  146. }));
  147. };
  148. });
  149. describe("with the default value", function() {
  150. beforeEach(function() {
  151. this.callback = sinon.spy();
  152. this.dialog = bootbox.alert("hi", this.callback);
  153. });
  154. describe("When triggering the backdrop click dismiss event", function() {
  155. beforeEach(function() {
  156. this.e({an: "object"});
  157. });
  158. it("does not invoke the callback", function() {
  159. expect(this.callback).not.to.have.been.called;
  160. });
  161. });
  162. });
  163. describe("when set to false", function() {
  164. beforeEach(function() {
  165. this.callback = sinon.spy();
  166. this.dialog = bootbox.alert({
  167. message: "hi",
  168. callback: this.callback,
  169. backdrop: false
  170. });
  171. });
  172. describe("When triggering the backdrop click dismiss event", function() {
  173. describe("With the wrong target", function() {
  174. beforeEach(function() {
  175. this.e({an: "object"});
  176. });
  177. it("does not invoke the callback", function() {
  178. expect(this.callback).not.to.have.been.called;
  179. });
  180. });
  181. describe("With the correct target", function() {
  182. beforeEach(function() {
  183. this.e(this.dialog.get(0));
  184. });
  185. it("invokes the callback", function() {
  186. expect(this.callback).to.have.been.called;
  187. });
  188. it("should pass the dialog as `this`", function() {
  189. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  190. });
  191. });
  192. });
  193. });
  194. describe("when set to true", function() {
  195. beforeEach(function() {
  196. this.callback = sinon.spy();
  197. this.dialog = bootbox.alert({
  198. message: "hi",
  199. callback: this.callback,
  200. backdrop: true
  201. });
  202. });
  203. describe("When triggering the backdrop click dismiss event", function() {
  204. describe("With the wrong target", function() {
  205. beforeEach(function() {
  206. this.e({an: "object"});
  207. });
  208. it("does not invoke the callback", function() {
  209. expect(this.callback).not.to.have.been.called;
  210. });
  211. });
  212. describe("With the correct target", function() {
  213. beforeEach(function() {
  214. this.e(this.dialog.children(".modal-backdrop").get(0));
  215. });
  216. it("invokes the callback", function() {
  217. expect(this.callback).to.have.been.called;
  218. });
  219. it("should pass the dialog as `this`", function() {
  220. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  221. });
  222. });
  223. });
  224. });
  225. });
  226. });