bootstrap-switch.tests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. describe("The Checkbox Bootstrap Switch", function() {
  42. it("should conserve its state if onSwitchChange returns false", function() {
  43. var $indeterminateSwitch, $switch;
  44. $switch = createCheckbox().bootstrapSwitch({
  45. onSwitchChange: function(e, s) {
  46. expect(s).toEqual(true);
  47. return false;
  48. }
  49. });
  50. $indeterminateSwitch = createCheckbox().data("indeterminate", true).bootstrapSwitch({
  51. onSwitchChange: function(e, s) {
  52. expect(s).toEqual(true);
  53. return false;
  54. }
  55. });
  56. $switch.click();
  57. $indeterminateSwitch.click();
  58. expect($switch.bootstrapSwitch('state')).toEqual(false);
  59. return expect($indeterminateSwitch.bootstrapSwitch('state')).toEqual(false);
  60. });
  61. return it("should change its state if onSwitchChange not returns false", function() {
  62. var $switch;
  63. $switch = createCheckbox().bootstrapSwitch({
  64. onSwitchChange: function(e, s) {
  65. return expect(s).toEqual(true);
  66. }
  67. });
  68. $switch.click();
  69. return expect($switch.bootstrapSwitch('state')).toEqual(true);
  70. });
  71. });
  72. return describe("The Radio Bootstrap Switch", function() {
  73. it("should conserve its state if onSwitchChange returns false", function() {
  74. var $radio1, $radio2, $radio3;
  75. $radio1 = createRadio().prop("checked", true);
  76. $radio2 = createRadio().prop("checked", false);
  77. $radio3 = createRadio().prop("checked", false);
  78. $('[name="name"]').bootstrapSwitch({
  79. onSwitchChange: function(e, s) {
  80. expect(s).toEqual(true);
  81. return false;
  82. }
  83. });
  84. $radio2.click();
  85. expect($radio1.bootstrapSwitch('state')).toEqual(true);
  86. expect($radio2.bootstrapSwitch('state')).toEqual(false);
  87. return expect($radio3.bootstrapSwitch('state')).toEqual(false);
  88. });
  89. return it("should change its state if onSwitchChange not returns false", function() {
  90. var $radio1, $radio2, $radio3;
  91. $radio1 = createRadio().prop("checked", true);
  92. $radio2 = createRadio().prop("checked", false);
  93. $radio3 = createRadio().prop("checked", false);
  94. $('[name="name"]').bootstrapSwitch({
  95. onSwitchChange: function(e, s) {
  96. return expect(s).toEqual(true);
  97. }
  98. });
  99. $radio2.click();
  100. expect($radio1.bootstrapSwitch('state')).toEqual(false);
  101. expect($radio2.bootstrapSwitch('state')).toEqual(true);
  102. return expect($radio3.bootstrapSwitch('state')).toEqual(false);
  103. });
  104. });
  105. });
  106. }).call(this);