123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- describe("'destroy()' Method tests", function() {
- var testSlider;
-
- function createSliderFn() {
- testSlider = new Slider("#testSlider1", {
- id: "destroyMethodTestSlider"
- });
- }
- it("removes the extra DOM elements associated with a slider", function() {
- createSliderFn();
- testSlider.destroy();
- var sliderParentElement = $("#testSlider1").parent('div.slider').length;
- var sliderChildrenElements = $("#testSlider1").siblings('div.slider-track, div.tooltip').length;
- expect(sliderParentElement).toBe(0);
- expect(sliderChildrenElements).toBe(0);
- });
- describe("Destroy slider instance associated with <input> element", function() {
- var sourceJS = "temp/bootstrap-slider.js";
- var defaultNamespace = 'slider';
- var alternateNamespace = 'bootstrapSlider';
- it("Should remove the instance associated with the <input> DOM element", function() {
- var $testSlider = $('#testSlider1').slider();
- var sliderInst = $testSlider.data(defaultNamespace);
- expect(sliderInst).toBeTruthy();
- $testSlider.slider('destroy');
- sliderInst = $testSlider.data(defaultNamespace);
- expect(sliderInst).toBeUndefined();
- });
- describe("Check alternate namespace", function() {
- afterEach(function(done) {
- $.fn.bootstrapSlider = undefined;
- $.fn.slider = undefined;
-
- $.getScript(sourceJS, function() {
- done();
- });
- });
- it("Should remove the instance associated with the <input> DOM element with alternate namespace", function(done) {
- $.fn.slider = function() {};
- $.getScript(sourceJS, function() {
- var $testSlider = $('#testSlider1').bootstrapSlider();
- $testSlider.bootstrapSlider('destroy');
- var sliderInst = $testSlider.data(alternateNamespace);
- expect(sliderInst).toBeUndefined();
- done();
- });
- });
- });
- });
- describe("unbinds all slider events", function() {
- var flag, evtName;
- beforeEach(function() {
- createSliderFn();
- flag = false;
- });
- it("unbinds from 'slideStart' event", function() {
- evtName = 'slideStart';
- $("#destroyMethodTestSlider").on(evtName, function() {
- flag = true;
- });
- testSlider.destroy();
- $("#destroyMethodTestSlider").trigger(evtName);
- expect(flag).toBeFalsy();
- });
- it("unbinds from 'slide' event", function() {
- evtName = 'slide';
- $("#destroyMethodTestSlider").on(evtName, function() {
- flag = true;
- });
- testSlider.destroy();
- $("#destroyMethodTestSlider").trigger(evtName);
- expect(flag).toBeFalsy();
- });
- it("unbinds from 'slideStop' event", function() {
- evtName = 'slideStop';
- $("#destroyMethodTestSlider").on(evtName, function() {
- flag = true;
- });
- testSlider.destroy();
- $("#destroyMethodTestSlider").trigger(evtName);
- expect(flag).toBeFalsy();
- });
- it("unbinds from 'slideChange' event", function() {
- evtName = 'slideChange';
- $("#destroyMethodTestSlider").on(evtName, function() {
- flag = true;
- });
- testSlider.destroy();
- $("#destroyMethodTestSlider").trigger(evtName);
- expect(flag).toBeFalsy();
- });
- it("unbinds all slider events and allows you to re-create the slider without runtime error", function() {
- // Setup
- var createSliderAndBindEvent = function () {
- $("#testSlider1").bootstrapSlider({
- min: 0,
- max: 1000 * 1000,
- step: 1000
- });
- testSlider = $("#testSlider1").data("bootstrapSlider");
- testSlider.on("slideStop", function() {});
- };
- // Destroy existing slider from default bootstrap step
- testSlider.destroy();
- // Create new Slider
- createSliderAndBindEvent();
- // Destroy slider
- testSlider.destroy();
- // Re-create Slider and try to re-bind event
- var throwsRuntimeError = false;
-
- try {
- createSliderAndBindEvent();
- }
- catch (e) {
- throwsRuntimeError = true;
- }
- testSlider.destroy();
- // reCreateSliderAndBindEvent(): Assert error is not thrown
- expect(throwsRuntimeError).toBeFalsy();
- });
- });
- describe("DOM event listener removal tests", function() {
- describe("When tooltips are always hidden for single value sliders", function() {
- beforeEach(function() {
- // Create slider
- testSlider = new Slider("#testSlider1", {
- id: "destroyMethodTestSlider",
- tooltip: "hide"
- });
- });
- it("does not try to remove 'focus' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
- it("does not try to remove 'blur' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
- it("does not try to remove 'mouseenter' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
- });
- it("does not try to remove 'mouseleave' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
- });
- });
- describe("When tooltips are always shown for single value sliders", function() {
- beforeEach(function() {
- // Create slider
- testSlider = new Slider("#testSlider1", {
- id: "destroyMethodTestSlider",
- tooltip: "always"
- });
- });
- it("does not try to remove 'focus' event listener from handle1 when tooltip is always shown for single handle sliders", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
- it("does not try to remove 'blur' event listener from handle1 when tooltip is always shown for single handle sliders", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
- it("does not try to remove 'mouseenter' event listener from slider is always shown for single handle slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
- });
- it("does not try to remove 'mouseleave' event listener from slider is always shown for single handle slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
- });
- });
- describe("When tooltips are always hidden for range sliders", function() {
- beforeEach(function() {
- // Create slider
- testSlider = new Slider("#testSlider1", {
- id: "destroyMethodTestSlider",
- tooltip: "always",
- value: [2,5]
- });
- });
- it("does not try to remove 'focus' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
- it("does not try to remove 'focus' event listener from handle2", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle2, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
- it("does not try to remove 'blur' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
- it("does not try to remove 'blur' event listener from handle2", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle2, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
- it("does not try to remove 'mouseenter' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
- });
- it("does not try to remove 'mouseleave' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
- });
- });
- describe("When tooltips are always shown for range sliders", function() {
- beforeEach(function() {
- // Create slider
- testSlider = new Slider("#testSlider1", {
- id: "destroyMethodTestSlider",
- tooltip: "always",
- value: [2,5]
- });
- });
- it("does not try to remove 'focus' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
-
- it("does not try to remove 'focus' event listener from handle2", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle2, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("focus", undefined, false);
- });
- it("does not try to remove 'blur' event listener from handle1", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle1, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle1.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
-
- it("does not try to remove 'blur' event listener from handle1 and handle2", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.handle2, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.handle2.removeEventListener).not.toHaveBeenCalledWith("blur", undefined, false);
- });
- it("does not try to remove 'mouseenter' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseenter", undefined, false);
- });
- it("does not try to remove 'mouseleave' event listener from slider", function() {
- // Set up spy on 'removeEventListener'
- spyOn(testSlider.sliderElem, "removeEventListener");
- // Destroy slider
- testSlider.destroy();
- // Assert
- expect(testSlider.sliderElem.removeEventListener).not.toHaveBeenCalledWith("mouseleave", undefined, false);
- });
- });
- });
- });
|