switch.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { 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. // @todo: add test for default-checked
  79. // @todo: add test for hidden input