bootstrap-switch.test.js 3.9 KB

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