button.spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import Button from '../../src/button.js'
  2. import { clearFixture, getFixture, jQueryMock } from '../helpers/fixture.js'
  3. describe('Button', () => {
  4. let fixtureEl
  5. beforeAll(() => {
  6. fixtureEl = getFixture()
  7. })
  8. afterEach(() => {
  9. clearFixture()
  10. })
  11. it('should take care of element either passed as a CSS selector or DOM element', () => {
  12. fixtureEl.innerHTML = '<button data-bs-toggle="button">Placeholder</button>'
  13. const buttonEl = fixtureEl.querySelector('[data-bs-toggle="button"]')
  14. const buttonBySelector = new Button('[data-bs-toggle="button"]')
  15. const buttonByElement = new Button(buttonEl)
  16. expect(buttonBySelector._element).toEqual(buttonEl)
  17. expect(buttonByElement._element).toEqual(buttonEl)
  18. })
  19. describe('VERSION', () => {
  20. it('should return plugin version', () => {
  21. expect(Button.VERSION).toEqual(jasmine.any(String))
  22. })
  23. })
  24. describe('DATA_KEY', () => {
  25. it('should return plugin data key', () => {
  26. expect(Button.DATA_KEY).toEqual('bs.button')
  27. })
  28. })
  29. describe('data-api', () => {
  30. it('should toggle active class on click', () => {
  31. fixtureEl.innerHTML = [
  32. '<button class="btn" data-bs-toggle="button">btn</button>',
  33. '<button class="btn testParent" data-bs-toggle="button"><div class="test"></div></button>'
  34. ].join('')
  35. const btn = fixtureEl.querySelector('.btn')
  36. const divTest = fixtureEl.querySelector('.test')
  37. const btnTestParent = fixtureEl.querySelector('.testParent')
  38. expect(btn).not.toHaveClass('active')
  39. btn.click()
  40. expect(btn).toHaveClass('active')
  41. btn.click()
  42. expect(btn).not.toHaveClass('active')
  43. divTest.click()
  44. expect(btnTestParent).toHaveClass('active')
  45. })
  46. })
  47. describe('toggle', () => {
  48. it('should toggle aria-pressed', () => {
  49. fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button" aria-pressed="false"></button>'
  50. const btnEl = fixtureEl.querySelector('.btn')
  51. const button = new Button(btnEl)
  52. expect(btnEl.getAttribute('aria-pressed')).toEqual('false')
  53. expect(btnEl).not.toHaveClass('active')
  54. button.toggle()
  55. expect(btnEl.getAttribute('aria-pressed')).toEqual('true')
  56. expect(btnEl).toHaveClass('active')
  57. })
  58. })
  59. describe('dispose', () => {
  60. it('should dispose a button', () => {
  61. fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
  62. const btnEl = fixtureEl.querySelector('.btn')
  63. const button = new Button(btnEl)
  64. expect(Button.getInstance(btnEl)).not.toBeNull()
  65. button.dispose()
  66. expect(Button.getInstance(btnEl)).toBeNull()
  67. })
  68. })
  69. describe('jQueryInterface', () => {
  70. it('should handle config passed and toggle existing button', () => {
  71. fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
  72. const btnEl = fixtureEl.querySelector('.btn')
  73. const button = new Button(btnEl)
  74. const spy = spyOn(button, 'toggle')
  75. jQueryMock.fn.button = Button.jQueryInterface
  76. jQueryMock.elements = [btnEl]
  77. jQueryMock.fn.button.call(jQueryMock, 'toggle')
  78. expect(spy).toHaveBeenCalled()
  79. })
  80. it('should create new button instance and call toggle', () => {
  81. fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
  82. const btnEl = fixtureEl.querySelector('.btn')
  83. jQueryMock.fn.button = Button.jQueryInterface
  84. jQueryMock.elements = [btnEl]
  85. jQueryMock.fn.button.call(jQueryMock, 'toggle')
  86. expect(Button.getInstance(btnEl)).not.toBeNull()
  87. expect(btnEl).toHaveClass('active')
  88. })
  89. it('should just create a button instance without calling toggle', () => {
  90. fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
  91. const btnEl = fixtureEl.querySelector('.btn')
  92. jQueryMock.fn.button = Button.jQueryInterface
  93. jQueryMock.elements = [btnEl]
  94. jQueryMock.fn.button.call(jQueryMock)
  95. expect(Button.getInstance(btnEl)).not.toBeNull()
  96. expect(btnEl).not.toHaveClass('active')
  97. })
  98. })
  99. describe('getInstance', () => {
  100. it('should return button instance', () => {
  101. fixtureEl.innerHTML = '<div></div>'
  102. const div = fixtureEl.querySelector('div')
  103. const button = new Button(div)
  104. expect(Button.getInstance(div)).toEqual(button)
  105. expect(Button.getInstance(div)).toBeInstanceOf(Button)
  106. })
  107. it('should return null when there is no button instance', () => {
  108. fixtureEl.innerHTML = '<div></div>'
  109. const div = fixtureEl.querySelector('div')
  110. expect(Button.getInstance(div)).toBeNull()
  111. })
  112. })
  113. describe('getOrCreateInstance', () => {
  114. it('should return button instance', () => {
  115. fixtureEl.innerHTML = '<div></div>'
  116. const div = fixtureEl.querySelector('div')
  117. const button = new Button(div)
  118. expect(Button.getOrCreateInstance(div)).toEqual(button)
  119. expect(Button.getInstance(div)).toEqual(Button.getOrCreateInstance(div, {}))
  120. expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
  121. })
  122. it('should return new instance when there is no button instance', () => {
  123. fixtureEl.innerHTML = '<div></div>'
  124. const div = fixtureEl.querySelector('div')
  125. expect(Button.getInstance(div)).toBeNull()
  126. expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
  127. })
  128. })
  129. })