bootstrap-button.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. $(function () {
  2. module("bootstrap-buttons")
  3. test("should provide no conflict", function () {
  4. var button = $.fn.button.noConflict()
  5. ok(!$.fn.button, 'button was set back to undefined (org value)')
  6. $.fn.button = button
  7. })
  8. test("should be defined on jquery object", function () {
  9. ok($(document.body).button, 'button method is defined')
  10. })
  11. test("should return element", function () {
  12. ok($(document.body).button()[0] == document.body, 'document.body returned')
  13. })
  14. test("should return set state to loading", function () {
  15. var btn = $('<button class="btn" data-loading-text="fat">mdo</button>')
  16. equals(btn.html(), 'mdo', 'btn text equals mdo')
  17. btn.button('loading')
  18. equals(btn.html(), 'fat', 'btn text equals fat')
  19. stop()
  20. setTimeout(function () {
  21. ok(btn.attr('disabled'), 'btn is disabled')
  22. ok(btn.hasClass('disabled'), 'btn has disabled class')
  23. start()
  24. }, 0)
  25. })
  26. test("should return reset state", function () {
  27. var btn = $('<button class="btn" data-loading-text="fat">mdo</button>')
  28. equals(btn.html(), 'mdo', 'btn text equals mdo')
  29. btn.button('loading')
  30. equals(btn.html(), 'fat', 'btn text equals fat')
  31. stop()
  32. setTimeout(function () {
  33. ok(btn.attr('disabled'), 'btn is disabled')
  34. ok(btn.hasClass('disabled'), 'btn has disabled class')
  35. start()
  36. stop()
  37. }, 0)
  38. btn.button('reset')
  39. equals(btn.html(), 'mdo', 'btn text equals mdo')
  40. setTimeout(function () {
  41. ok(!btn.attr('disabled'), 'btn is not disabled')
  42. ok(!btn.hasClass('disabled'), 'btn does not have disabled class')
  43. start()
  44. }, 0)
  45. })
  46. test("should toggle active", function () {
  47. var btn = $('<button class="btn">mdo</button>')
  48. ok(!btn.hasClass('active'), 'btn does not have active class')
  49. btn.button('toggle')
  50. ok(btn.hasClass('active'), 'btn has class active')
  51. })
  52. test("should toggle active when btn children are clicked", function () {
  53. var btn = $('<button class="btn" data-toggle="button">mdo</button>')
  54. , inner = $('<i></i>')
  55. btn
  56. .append(inner)
  57. .appendTo($('#qunit-fixture'))
  58. ok(!btn.hasClass('active'), 'btn does not have active class')
  59. inner.click()
  60. ok(btn.hasClass('active'), 'btn has class active')
  61. })
  62. test("should toggle active when btn children are clicked within btn-group", function () {
  63. var btngroup = $('<div class="btn-group" data-toggle="buttons-checkbox"></div>')
  64. , btn = $('<button class="btn">fat</button>')
  65. , inner = $('<i></i>')
  66. btngroup
  67. .append(btn.append(inner))
  68. .appendTo($('#qunit-fixture'))
  69. ok(!btn.hasClass('active'), 'btn does not have active class')
  70. inner.click()
  71. ok(btn.hasClass('active'), 'btn has class active')
  72. })
  73. test("should check for closest matching toggle", function () {
  74. var group = $("<div data-toggle='buttons-radio'></div>")
  75. , btn1 = $("<button class='btn active'></button>")
  76. , btn2 = $("<button class='btn'></button>")
  77. , wrap = $("<div></div>")
  78. wrap.append(btn1, btn2)
  79. group
  80. .append(wrap)
  81. .appendTo($('#qunit-fixture'))
  82. ok(btn1.hasClass('active'), 'btn1 has active class')
  83. ok(!btn2.hasClass('active'), 'btn2 does not have active class')
  84. btn2.click()
  85. ok(!btn1.hasClass('active'), 'btn1 does not have active class')
  86. ok(btn2.hasClass('active'), 'btn2 has active class')
  87. })
  88. })