alert.test.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. describe("bootbox.alert", function() {
  2. "use strict";
  3. var self;
  4. beforeEach(function() {
  5. self = this;
  6. this.text = function(selector) {
  7. return this.find(selector).text();
  8. };
  9. this.find = function(selector) {
  10. return this.dialog.find(selector);
  11. };
  12. });
  13. describe("basic usage tests", function() {
  14. describe("with no arguments", function() {
  15. beforeEach(function() {
  16. this.create = function() {
  17. bootbox.alert();
  18. };
  19. });
  20. it("throws an error regarding argument length", function() {
  21. expect(this.create).to.throw(/argument length/);
  22. });
  23. });
  24. describe("with one argument", function() {
  25. describe("where the argument is a string", function() {
  26. beforeEach(function() {
  27. this.dialog = bootbox.alert("Hello world!");
  28. });
  29. it("applies the bootbox-alert class to the dialog", function() {
  30. expect(this.dialog.hasClass("bootbox-alert")).to.be.true;
  31. });
  32. it("shows the expected body copy", function() {
  33. expect(this.text(".bootbox-body")).to.equal("Hello world!");
  34. });
  35. it("shows an OK button", function() {
  36. expect(this.text(".modal-footer button:first")).to.equal("OK");
  37. });
  38. it("applies the primary class to the button", function() {
  39. expect(this.find(".modal-footer button:first").hasClass("btn-primary")).to.be.true;
  40. });
  41. it("shows a close button inside the body", function() {
  42. expect(this.text(".modal-body button")).to.equal("×");
  43. });
  44. it("applies the close class to the close button", function() {
  45. expect(this.find(".modal-body button").hasClass("close")).to.be.true;
  46. });
  47. it("applies the correct data-dismiss attribute to the close button", function() {
  48. expect(this.find("button.close").attr("data-dismiss")).to.equal("modal");
  49. });
  50. it("applies the correct aria-hidden attribute to the close button", function() {
  51. expect(this.find("button.close").attr("aria-hidden")).to.equal("true");
  52. });
  53. it("applies the correct class to the body", function() {
  54. expect($("body").hasClass("modal-open")).to.be.true;
  55. });
  56. });
  57. });
  58. describe("with two arguments", function() {
  59. describe("where the second argument is not a function", function() {
  60. beforeEach(function() {
  61. this.create = function() {
  62. bootbox.alert("Hello world!", "not a callback");
  63. };
  64. });
  65. it("throws an error requiring a callback", function() {
  66. expect(this.create).to.throw(/alert requires callback property to be a function when provided/);
  67. });
  68. });
  69. describe("where the second argument is a function", function() {
  70. beforeEach(function() {
  71. this.create = function() {
  72. self.dialog = bootbox.alert("Hello world!", function() {});
  73. };
  74. });
  75. it("does not throw an error", function() {
  76. expect(this.create).not.to.throw(Error);
  77. });
  78. });
  79. });
  80. describe("with three arguments", function() {
  81. beforeEach(function() {
  82. this.create = function() {
  83. bootbox.alert(1, 2, 3);
  84. };
  85. });
  86. it("throws an error regarding argument length", function() {
  87. expect(this.create).to.throw(/argument length/);
  88. });
  89. });
  90. });
  91. describe("configuration options tests", function() {
  92. beforeEach(function() {
  93. this.options = {
  94. message: "Hello world",
  95. callback: function() {}
  96. };
  97. this.create = function() {
  98. self.dialog = bootbox.alert(self.options);
  99. };
  100. });
  101. describe("with a custom ok button", function() {
  102. beforeEach(function() {
  103. this.options.buttons = {
  104. ok: {
  105. label: "Custom OK",
  106. className: "btn-danger"
  107. }
  108. };
  109. this.create();
  110. this.button = this.dialog.find(".btn:first");
  111. });
  112. it("adds the correct ok button", function() {
  113. expect(this.button.text()).to.equal("Custom OK");
  114. expect(this.button.hasClass("btn-danger")).to.be.true;
  115. });
  116. });
  117. describe("with an unrecognised button key", function() {
  118. beforeEach(function() {
  119. this.options.buttons = {
  120. "Another key": {
  121. label: "Custom OK",
  122. className: "btn-danger"
  123. }
  124. };
  125. });
  126. it("throws an error", function() {
  127. expect(this.create).to.throw("button key Another key is not allowed (options are ok)");
  128. });
  129. });
  130. describe("with a custom title", function() {
  131. beforeEach(function() {
  132. this.options.title = "Hello?";
  133. this.create();
  134. });
  135. it("shows the correct title", function() {
  136. expect(this.text("h4")).to.equal("Hello?");
  137. });
  138. });
  139. });
  140. describe("callback tests", function() {
  141. describe("with no callback", function() {
  142. beforeEach(function() {
  143. this.dialog = bootbox.alert({
  144. message:"Hello!"
  145. });
  146. this.hidden = sinon.spy(this.dialog, "modal");
  147. });
  148. describe("when dismissing the dialog by clicking OK", function() {
  149. beforeEach(function() {
  150. this.dialog.find(".btn-primary").trigger("click");
  151. });
  152. it("should hide the modal", function() {
  153. expect(this.hidden).to.have.been.calledWithExactly("hide");
  154. });
  155. });
  156. describe("when clicking the close button", function() {
  157. beforeEach(function() {
  158. this.dialog.find(".close").trigger("click");
  159. });
  160. it("should hide the modal", function() {
  161. expect(this.hidden).to.have.been.calledWithExactly("hide");
  162. });
  163. });
  164. describe("when triggering the escape event", function() {
  165. beforeEach(function() {
  166. this.dialog.trigger("escape.close.bb");
  167. });
  168. it("should hide the modal", function() {
  169. expect(this.hidden).to.have.been.calledWithExactly("hide");
  170. });
  171. });
  172. });
  173. describe("with a simple callback", function() {
  174. beforeEach(function() {
  175. this.callback = sinon.spy();
  176. this.dialog = bootbox.alert({
  177. message:"Hello!",
  178. callback: this.callback
  179. });
  180. this.hidden = sinon.spy(this.dialog, "modal");
  181. });
  182. describe("when dismissing the dialog by clicking OK", function() {
  183. beforeEach(function() {
  184. this.dialog.find(".btn-primary").trigger("click");
  185. });
  186. it("should invoke the callback", function() {
  187. expect(this.callback).to.have.been.called;
  188. });
  189. it("should pass the dialog as `this`", function() {
  190. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  191. });
  192. it("should hide the modal", function() {
  193. expect(this.hidden).to.have.been.calledWithExactly("hide");
  194. });
  195. });
  196. describe("when clicking the close button", function() {
  197. beforeEach(function() {
  198. this.dialog.find(".close").trigger("click");
  199. });
  200. it("should invoke the callback", function() {
  201. expect(this.callback).to.have.been.called;
  202. });
  203. it("should pass the dialog as `this`", function() {
  204. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  205. });
  206. it("should hide the modal", function() {
  207. expect(this.hidden).to.have.been.calledWithExactly("hide");
  208. });
  209. });
  210. describe("when triggering the escape event", function() {
  211. beforeEach(function() {
  212. this.dialog.trigger("escape.close.bb");
  213. });
  214. it("should invoke the callback", function() {
  215. expect(this.callback).to.have.been.called;
  216. });
  217. it("should pass the dialog as `this`", function() {
  218. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  219. });
  220. it("should hide the modal", function() {
  221. expect(this.hidden).to.have.been.calledWithExactly("hide");
  222. });
  223. });
  224. });
  225. describe("with a callback which returns false", function() {
  226. beforeEach(function() {
  227. this.callback = sinon.stub();
  228. this.callback.returns(false);
  229. this.dialog = bootbox.alert({
  230. message:"Hello!",
  231. callback: this.callback
  232. });
  233. this.hidden = sinon.spy(this.dialog, "modal");
  234. });
  235. describe("when dismissing the dialog by clicking OK", function() {
  236. beforeEach(function() {
  237. this.dialog.find(".btn-primary").trigger("click");
  238. });
  239. it("should invoke the callback", function() {
  240. expect(this.callback).to.have.been.called;
  241. });
  242. it("should pass the dialog as `this`", function() {
  243. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  244. });
  245. it("should not hide the modal", function() {
  246. expect(this.hidden).not.to.have.been.called;
  247. });
  248. });
  249. describe("when clicking the close button", function() {
  250. beforeEach(function() {
  251. this.dialog.find(".close").trigger("click");
  252. });
  253. it("should invoke the callback", function() {
  254. expect(this.callback).to.have.been.called;
  255. });
  256. it("should pass the dialog as `this`", function() {
  257. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  258. });
  259. it("should not hide the modal", function() {
  260. expect(this.hidden).not.to.have.been.called;
  261. });
  262. });
  263. describe("when triggering the escape event", function() {
  264. beforeEach(function() {
  265. this.dialog.trigger("escape.close.bb");
  266. });
  267. it("should invoke the callback", function() {
  268. expect(this.callback).to.have.been.called;
  269. });
  270. it("should pass the dialog as `this`", function() {
  271. expect(this.callback.thisValues[0]).to.equal(this.dialog);
  272. });
  273. it("should not hide the modal", function() {
  274. expect(this.hidden).not.to.have.been.called;
  275. });
  276. });
  277. });
  278. });
  279. });