switch.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { beVisible, haveAttribute, 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 cypress-id="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('[cypress-id="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>
  45. <button x-switch>Enable notifications</button>
  46. <article x-show="$switch.isChecked">
  47. Notifications are enabled.
  48. </article>
  49. </div>
  50. `],
  51. ({ get }) => {
  52. get('article').should(notBeVisible())
  53. get('button').click()
  54. get('article').should(beVisible())
  55. get('button').click()
  56. get('article').should(notBeVisible())
  57. },
  58. )
  59. test('pressing space toggles the switch',
  60. [html`
  61. <div x-data="{ checked: false }">
  62. <div>
  63. <button x-switch x-model="checked">Enable notifications</button>
  64. <article x-show="checked">
  65. Notifications are enabled.
  66. </article>
  67. </div>
  68. </div>
  69. `],
  70. ({ get }) => {
  71. get('article').should(notBeVisible())
  72. get('button').focus()
  73. get('button').type(' ')
  74. get('article').should(beVisible())
  75. get('button').type(' ')
  76. get('article').should(notBeVisible())
  77. },
  78. )