DestroyMethodTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. describe("'destroy()' Method tests", function() {
  2. var testSlider;
  3. function createSliderFn() {
  4. testSlider = new Slider("#testSlider1", {
  5. id: "destroyMethodTestSlider"
  6. });
  7. }
  8. it("removes the extra DOM elements associated with a slider", function() {
  9. createSliderFn();
  10. testSlider.destroy();
  11. var sliderParentElement = $("#testSlider1").parent('div.slider').length;
  12. var sliderChildrenElements = $("#testSlider1").siblings('div.slider-track, div.tooltip').length;
  13. expect(sliderParentElement).toBe(0);
  14. expect(sliderChildrenElements).toBe(0);
  15. });
  16. describe("Destroy slider instance associated with <input> element", function() {
  17. var sourceJS = "temp/bootstrap-slider.js";
  18. var defaultNamespace = 'slider';
  19. var alternateNamespace = 'bootstrapSlider';
  20. it("Should remove the instance associated with the <input> DOM element", function() {
  21. var $testSlider = $('#testSlider1').slider();
  22. var sliderInst = $testSlider.data(defaultNamespace);
  23. expect(sliderInst).toBeTruthy();
  24. $testSlider.slider('destroy');
  25. sliderInst = $testSlider.data(defaultNamespace);
  26. expect(sliderInst).toBeUndefined();
  27. });
  28. describe("Check alternate namespace", function() {
  29. afterEach(function(done) {
  30. $.fn.bootstrapSlider = undefined;
  31. $.fn.slider = undefined;
  32. $.getScript(sourceJS, function() {
  33. done();
  34. });
  35. });
  36. it("Should remove the instance associated with the <input> DOM element with alternate namespace", function(done) {
  37. $.fn.slider = function() {};
  38. $.getScript(sourceJS, function() {
  39. var $testSlider = $('#testSlider1').bootstrapSlider();
  40. $testSlider.bootstrapSlider('destroy');
  41. var sliderInst = $testSlider.data(alternateNamespace);
  42. expect(sliderInst).toBeUndefined();
  43. done();
  44. });
  45. });
  46. });
  47. });
  48. describe("unbinds all slider events", function() {
  49. var flag, evtName;
  50. beforeEach(function() {
  51. createSliderFn();
  52. flag = false;
  53. });
  54. it("unbinds from 'slideStart' event", function() {
  55. evtName = 'slideStart';
  56. $("#destroyMethodTestSlider").on(evtName, function() {
  57. flag = true;
  58. });
  59. testSlider.destroy();
  60. $("#destroyMethodTestSlider").trigger(evtName);
  61. expect(flag).toBeFalsy();
  62. });
  63. it("unbinds from 'slide' event", function() {
  64. evtName = 'slide';
  65. $("#destroyMethodTestSlider").on(evtName, function() {
  66. flag = true;
  67. });
  68. testSlider.destroy();
  69. $("#destroyMethodTestSlider").trigger(evtName);
  70. expect(flag).toBeFalsy();
  71. });
  72. it("unbinds from 'slideStop' event", function() {
  73. evtName = 'slideStop';
  74. $("#destroyMethodTestSlider").on(evtName, function() {
  75. flag = true;
  76. });
  77. testSlider.destroy();
  78. $("#destroyMethodTestSlider").trigger(evtName);
  79. expect(flag).toBeFalsy();
  80. });
  81. it("unbinds from 'slideChange' event", function() {
  82. evtName = 'slideChange';
  83. $("#destroyMethodTestSlider").on(evtName, function() {
  84. flag = true;
  85. });
  86. testSlider.destroy();
  87. $("#destroyMethodTestSlider").trigger(evtName);
  88. expect(flag).toBeFalsy();
  89. });
  90. it("unbinds all slider events and allows you to re-create the slider without runtime error", function() {
  91. // Setup
  92. var createSliderAndBindEvent = function () {
  93. $("#testSlider1").bootstrapSlider({
  94. min: 0,
  95. max: 1000 * 1000,
  96. step: 1000
  97. });
  98. testSlider = $("#testSlider1").data("bootstrapSlider");
  99. testSlider.on("slideStop", function() {});
  100. };
  101. // Destroy existing slider from default bootstrap step
  102. testSlider.destroy();
  103. // Create new Slider
  104. createSliderAndBindEvent();
  105. // Destroy slider
  106. testSlider.destroy();
  107. // Re-create Slider and try to re-bind event
  108. var throwsRuntimeError = false;
  109. try {
  110. createSliderAndBindEvent();
  111. }
  112. catch (e) {
  113. throwsRuntimeError = true;
  114. }
  115. testSlider.destroy();
  116. // reCreateSliderAndBindEvent(): Assert error is not thrown
  117. expect(throwsRuntimeError).toBeFalsy();
  118. });
  119. });
  120. describe("DOM event listener removal tests", function() {
  121. describe("When tooltips are always hidden for single value sliders", function() {
  122. beforeEach(function() {
  123. // Create slider
  124. testSlider = new Slider("#testSlider1", {
  125. id: "destroyMethodTestSlider",
  126. tooltip: "hide"
  127. });
  128. });
  129. it("does not try to remove 'focus' event listener from handle1", function() {
  130. // Set up spy on 'removeEventListener'
  131. spyOn(testSlider.handle1, "removeEventListener");
  132. // Destroy slider
  133. testSlider.destroy();
  134. // Assert
  135. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  136. });
  137. it("does not try to remove 'blur' event listener from handle1", function() {
  138. // Set up spy on 'removeEventListener'
  139. spyOn(testSlider.handle1, "removeEventListener");
  140. // Destroy slider
  141. testSlider.destroy();
  142. // Assert
  143. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  144. });
  145. it("does not try to remove 'mouseenter' event listener from slider", function() {
  146. // Set up spy on 'removeEventListener'
  147. spyOn(testSlider.sliderElem, "removeEventListener");
  148. // Destroy slider
  149. testSlider.destroy();
  150. // Assert
  151. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
  152. });
  153. it("does not try to remove 'mouseleave' event listener from slider", function() {
  154. // Set up spy on 'removeEventListener'
  155. spyOn(testSlider.sliderElem, "removeEventListener");
  156. // Destroy slider
  157. testSlider.destroy();
  158. // Assert
  159. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
  160. });
  161. });
  162. describe("When tooltips are always shown for single value sliders", function() {
  163. beforeEach(function() {
  164. // Create slider
  165. testSlider = new Slider("#testSlider1", {
  166. id: "destroyMethodTestSlider",
  167. tooltip: "always"
  168. });
  169. });
  170. it("does not try to remove 'focus' event listener from handle1 when tooltip is always shown for single handle sliders", function() {
  171. // Set up spy on 'removeEventListener'
  172. spyOn(testSlider.handle1, "removeEventListener");
  173. // Destroy slider
  174. testSlider.destroy();
  175. // Assert
  176. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  177. });
  178. it("does not try to remove 'blur' event listener from handle1 when tooltip is always shown for single handle sliders", function() {
  179. // Set up spy on 'removeEventListener'
  180. spyOn(testSlider.handle1, "removeEventListener");
  181. // Destroy slider
  182. testSlider.destroy();
  183. // Assert
  184. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  185. });
  186. it("does not try to remove 'mouseenter' event listener from slider is always shown for single handle slider", function() {
  187. // Set up spy on 'removeEventListener'
  188. spyOn(testSlider.handle1, "removeEventListener");
  189. // Destroy slider
  190. testSlider.destroy();
  191. // Assert
  192. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
  193. });
  194. it("does not try to remove 'mouseleave' event listener from slider is always shown for single handle slider", function() {
  195. // Set up spy on 'removeEventListener'
  196. spyOn(testSlider.sliderElem, "removeEventListener");
  197. // Destroy slider
  198. testSlider.destroy();
  199. // Assert
  200. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
  201. });
  202. });
  203. describe("When tooltips are always hidden for range sliders", function() {
  204. beforeEach(function() {
  205. // Create slider
  206. testSlider = new Slider("#testSlider1", {
  207. id: "destroyMethodTestSlider",
  208. tooltip: "always",
  209. value: [2,5]
  210. });
  211. });
  212. it("does not try to remove 'focus' event listener from handle1", function() {
  213. // Set up spy on 'removeEventListener'
  214. spyOn(testSlider.handle1, "removeEventListener");
  215. // Destroy slider
  216. testSlider.destroy();
  217. // Assert
  218. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  219. });
  220. it("does not try to remove 'focus' event listener from handle2", function() {
  221. // Set up spy on 'removeEventListener'
  222. spyOn(testSlider.handle2, "removeEventListener");
  223. // Destroy slider
  224. testSlider.destroy();
  225. // Assert
  226. expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  227. });
  228. it("does not try to remove 'blur' event listener from handle1", function() {
  229. // Set up spy on 'removeEventListener'
  230. spyOn(testSlider.handle1, "removeEventListener");
  231. // Destroy slider
  232. testSlider.destroy();
  233. // Assert
  234. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  235. });
  236. it("does not try to remove 'blur' event listener from handle2", function() {
  237. // Set up spy on 'removeEventListener'
  238. spyOn(testSlider.handle2, "removeEventListener");
  239. // Destroy slider
  240. testSlider.destroy();
  241. // Assert
  242. expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  243. });
  244. it("does not try to remove 'mouseenter' event listener from slider", function() {
  245. // Set up spy on 'removeEventListener'
  246. spyOn(testSlider.sliderElem, "removeEventListener");
  247. // Destroy slider
  248. testSlider.destroy();
  249. // Assert
  250. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
  251. });
  252. it("does not try to remove 'mouseleave' event listener from slider", function() {
  253. // Set up spy on 'removeEventListener'
  254. spyOn(testSlider.sliderElem, "removeEventListener");
  255. // Destroy slider
  256. testSlider.destroy();
  257. // Assert
  258. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
  259. });
  260. });
  261. describe("When tooltips are always shown for range sliders", function() {
  262. beforeEach(function() {
  263. // Create slider
  264. testSlider = new Slider("#testSlider1", {
  265. id: "destroyMethodTestSlider",
  266. tooltip: "always",
  267. value: [2,5]
  268. });
  269. });
  270. it("does not try to remove 'focus' event listener from handle1", function() {
  271. // Set up spy on 'removeEventListener'
  272. spyOn(testSlider.handle1, "removeEventListener");
  273. // Destroy slider
  274. testSlider.destroy();
  275. // Assert
  276. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  277. });
  278. it("does not try to remove 'focus' event listener from handle2", function() {
  279. // Set up spy on 'removeEventListener'
  280. spyOn(testSlider.handle2, "removeEventListener");
  281. // Destroy slider
  282. testSlider.destroy();
  283. // Assert
  284. expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
  285. });
  286. it("does not try to remove 'blur' event listener from handle1", function() {
  287. // Set up spy on 'removeEventListener'
  288. spyOn(testSlider.handle1, "removeEventListener");
  289. // Destroy slider
  290. testSlider.destroy();
  291. // Assert
  292. expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  293. });
  294. it("does not try to remove 'blur' event listener from handle1 and handle2", function() {
  295. // Set up spy on 'removeEventListener'
  296. spyOn(testSlider.handle2, "removeEventListener");
  297. // Destroy slider
  298. testSlider.destroy();
  299. // Assert
  300. expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
  301. });
  302. it("does not try to remove 'mouseenter' event listener from slider", function() {
  303. // Set up spy on 'removeEventListener'
  304. spyOn(testSlider.sliderElem, "removeEventListener");
  305. // Destroy slider
  306. testSlider.destroy();
  307. // Assert
  308. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
  309. });
  310. it("does not try to remove 'mouseleave' event listener from slider", function() {
  311. // Set up spy on 'removeEventListener'
  312. spyOn(testSlider.sliderElem, "removeEventListener");
  313. // Destroy slider
  314. testSlider.destroy();
  315. // Assert
  316. expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
  317. });
  318. });
  319. });
  320. });