switch.spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { beHidden, beVisible, haveAttribute, haveClasses, haveText, html, notBeVisible, notExist, test } from '../../../utils'
  2. test('has accessibility attributes',
  3. [html`
  4. <div x-data="{ checked: false }">
  5. <article x-switch:group>
  6. <label x-switch:label>Enable notifications</label>
  7. <span description x-switch:description>A description of the switch.</span>
  8. <button x-switch x-model="checked">Enable Notifications</button>
  9. </article>
  10. </div>
  11. `],
  12. ({ get }) => {
  13. get('label').should(haveAttribute('id', 'alpine-switch-label-1'))
  14. get('[description]').should(haveAttribute('id', 'alpine-switch-description-1'))
  15. get('button').should(haveAttribute('type', 'button'))
  16. get('button').should(haveAttribute('aria-labelledby', 'alpine-switch-label-1'))
  17. get('button').should(haveAttribute('aria-describedby', 'alpine-switch-description-1'))
  18. get('button').should(haveAttribute('role', 'switch'))
  19. get('button').should(haveAttribute('tabindex', 0))
  20. get('button').should(haveAttribute('aria-checked', 'false'))
  21. get('button').click()
  22. get('button').should(haveAttribute('aria-checked', 'true'))
  23. },
  24. )
  25. test('works with x-model',
  26. [html`
  27. <div x-data="{ checked: false }">
  28. <button x-switch x-model="checked">Enable notifications</button>
  29. <article x-show="checked">
  30. Notifications are enabled.
  31. </article>
  32. </div>
  33. `],
  34. ({ get }) => {
  35. get('article').should(notBeVisible())
  36. get('button').click()
  37. get('article').should(beVisible())
  38. get('button').click()
  39. get('article').should(notBeVisible())
  40. },
  41. )
  42. test('works with internal state/$switch.isChecked',
  43. [html`
  44. <div x-data>
  45. <button x-switch x-bind:class="$switch.isChecked ? 'foo' : 'bar'">
  46. Enable notifications
  47. </button>
  48. </div>
  49. `],
  50. ({ get }) => {
  51. get('button').should(haveClasses(['bar']))
  52. get('button').click()
  53. get('button').should(haveClasses(['foo']))
  54. get('button').click()
  55. get('button').should(haveClasses(['bar']))
  56. },
  57. )
  58. test('pressing space toggles the switch',
  59. [html`
  60. <div x-data="{ checked: false }">
  61. <div>
  62. <button x-switch x-model="checked">Enable notifications</button>
  63. <article x-show="checked">
  64. Notifications are enabled.
  65. </article>
  66. </div>
  67. </div>
  68. `],
  69. ({ get }) => {
  70. get('article').should(notBeVisible())
  71. get('button').focus()
  72. get('button').type(' ')
  73. get('article').should(beVisible())
  74. get('button').type(' ')
  75. get('article').should(notBeVisible())
  76. },
  77. )
  78. test('default-checked',
  79. [html`
  80. <div x-data>
  81. <div>
  82. <button
  83. x-switch
  84. default-checked
  85. :class="$switch.isChecked ? 'checked' : 'not-checked'"
  86. >Enable notifications</button>
  87. </div>
  88. </div>
  89. `],
  90. ({ get }) => {
  91. get('button').should(haveClasses(['checked']))
  92. get('button').click()
  93. get('button').should(haveClasses(['not-checked']))
  94. },
  95. )
  96. test('name and value props',
  97. [html`
  98. <div x-data>
  99. <div>
  100. <button
  101. x-switch
  102. name="notifications"
  103. value="yes"
  104. >Enable notifications</button>
  105. </div>
  106. </div>
  107. `],
  108. ({ get }) => {
  109. get('input').should(notExist())
  110. get('button').click()
  111. get('input').should(beHidden())
  112. .should(haveAttribute('name', 'notifications'))
  113. .should(haveAttribute('value', 'yes'))
  114. .should(haveAttribute('type', 'hidden'))
  115. get('button').click()
  116. get('input').should(notExist())
  117. },
  118. )
  119. test('value defaults to "on"',
  120. [html`
  121. <div x-data>
  122. <div>
  123. <button
  124. x-switch
  125. name="notifications"
  126. >Enable notifications</button>
  127. </div>
  128. </div>
  129. `],
  130. ({ get }) => {
  131. get('input').should(notExist())
  132. get('button').click()
  133. get('input').should(beHidden())
  134. .should(haveAttribute('name', 'notifications'))
  135. .should(haveAttribute('value', 'on'))
  136. .should(haveAttribute('type', 'hidden'))
  137. get('button').click()
  138. get('input').should(notExist())
  139. },
  140. )