bootstrap-switch.tests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (function() {
  2. describe("Bootstrap Switch:", function() {
  3. var createCheckbox, createRadio, getOptions;
  4. beforeEach(function() {
  5. $.support.transition = false;
  6. return $.fx.off = true;
  7. });
  8. afterEach(function() {
  9. return $("." + $.fn.bootstrapSwitch.defaults.baseClass).bootstrapSwitch("destroy");
  10. });
  11. createCheckbox = function() {
  12. return $("<input>", {
  13. type: "checkbox",
  14. "class": "switch"
  15. }).appendTo("body");
  16. };
  17. createRadio = function() {
  18. return $("<input>", {
  19. type: "radio",
  20. name: "name",
  21. "class": "switch"
  22. }).appendTo("body");
  23. };
  24. getOptions = function($element) {
  25. return $element.data("bootstrap-switch").options;
  26. };
  27. it("should set the default options as element options, except state", function() {
  28. var $switch;
  29. $switch = createCheckbox().prop("checked", true).bootstrapSwitch();
  30. return expect(getOptions($switch)).toEqual($.fn.bootstrapSwitch.defaults);
  31. });
  32. it("should override default options with initialization ones", function() {
  33. var $switch, $switch2;
  34. $switch = createCheckbox().prop("checked", false).bootstrapSwitch();
  35. $switch2 = createCheckbox().bootstrapSwitch({
  36. state: false
  37. });
  38. expect(getOptions($switch).state).toBe(false);
  39. return expect(getOptions($switch2).state).toBe(false);
  40. });
  41. it("should something", function() {
  42. var $switch, eventDoc, eventElement;
  43. eventDoc = eventElement = 0;
  44. $switch = createCheckbox().bootstrapSwitch();
  45. $(document).on("switchChange.bootstrapSwitch", ":checkbox", function(event, state) {
  46. return eventDoc++;
  47. });
  48. $(":checkbox").on("switchChange.bootstrapSwitch", function(event, state) {
  49. return eventElement++;
  50. });
  51. $switch.click();
  52. expect(eventElement).toEqual(eventDoc);
  53. return expect(eventElement).toEqual(1);
  54. });
  55. describe("The Checkbox Bootstrap Switch", function() {
  56. it("should conserve its state if onSwitchChange returns false", function() {
  57. var $indeterminateSwitch, $switch;
  58. $switch = createCheckbox().bootstrapSwitch({
  59. onSwitchChange: function(e, s) {
  60. expect(s).toEqual(true);
  61. return false;
  62. }
  63. });
  64. $indeterminateSwitch = createCheckbox().data("indeterminate", true).bootstrapSwitch({
  65. onSwitchChange: function(e, s) {
  66. expect(s).toEqual(true);
  67. return false;
  68. }
  69. });
  70. $switch.click();
  71. $indeterminateSwitch.click();
  72. expect($switch.bootstrapSwitch('state')).toEqual(false);
  73. return expect($indeterminateSwitch.bootstrapSwitch('state')).toEqual(false);
  74. });
  75. return it("should change its state if onSwitchChange not returns false", function() {
  76. var $switch;
  77. $switch = createCheckbox().bootstrapSwitch({
  78. onSwitchChange: function(e, s) {
  79. return expect(s).toEqual(true);
  80. }
  81. });
  82. $switch.click();
  83. return expect($switch.bootstrapSwitch('state')).toEqual(true);
  84. });
  85. });
  86. return describe("The Radio Bootstrap Switch", function() {
  87. it("should conserve its state if onSwitchChange returns false", function() {
  88. var $radio1, $radio2, $radio3;
  89. $radio1 = createRadio().prop("checked", true);
  90. $radio2 = createRadio().prop("checked", false);
  91. $radio3 = createRadio().prop("checked", false);
  92. $('[name="name"]').bootstrapSwitch({
  93. onSwitchChange: function(e, s) {
  94. expect(s).toEqual(true);
  95. return false;
  96. }
  97. });
  98. $radio2.click();
  99. expect($radio1.bootstrapSwitch('state')).toEqual(true);
  100. expect($radio2.bootstrapSwitch('state')).toEqual(false);
  101. return expect($radio3.bootstrapSwitch('state')).toEqual(false);
  102. });
  103. return it("should change its state if onSwitchChange not returns false", function() {
  104. var $radio1, $radio2, $radio3;
  105. $radio1 = createRadio().prop("checked", true);
  106. $radio2 = createRadio().prop("checked", false);
  107. $radio3 = createRadio().prop("checked", false);
  108. $('[name="name"]').bootstrapSwitch({
  109. onSwitchChange: function(e, s) {
  110. return expect(s).toEqual(true);
  111. }
  112. });
  113. $radio2.click();
  114. expect($radio1.bootstrapSwitch('state')).toEqual(false);
  115. expect($radio2.bootstrapSwitch('state')).toEqual(true);
  116. return expect($radio3.bootstrapSwitch('state')).toEqual(false);
  117. });
  118. });
  119. });
  120. }).call(this);