popover.spec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { beVisible, haveAttribute, html, notBeVisible, notHaveAttribute, test } from '../../../utils'
  2. test('button toggles panel',
  3. [html`
  4. <div x-data x-popover>
  5. <button x-popover:button>Toggle</button>
  6. <ul x-popover:panel>
  7. Dialog Contents!
  8. </ul>
  9. </div>
  10. `],
  11. ({ get }) => {
  12. get('ul').should(notBeVisible())
  13. get('button').click()
  14. get('ul').should(beVisible())
  15. get('button').click()
  16. get('ul').should(notBeVisible())
  17. },
  18. )
  19. test('popover can be rendered statically',
  20. [html`
  21. <div x-data x-popover>
  22. <button x-popover:button>Toggle</button>
  23. <ul x-popover:panel static>
  24. Dialog Contents!
  25. </ul>
  26. </div>
  27. `],
  28. ({ get }) => {
  29. get('ul').should(beVisible())
  30. get('button').click()
  31. get('ul').should(beVisible())
  32. },
  33. )
  34. test('has accessibility attributes',
  35. [html`
  36. <div x-data x-popover>
  37. <button x-popover:button>Toggle</button>
  38. <ul x-popover:panel>
  39. Dialog Contents!
  40. </ul>
  41. </div>
  42. `],
  43. ({ get }) => {
  44. get('button').should(haveAttribute('aria-expanded', 'false'))
  45. get('button').should(notHaveAttribute('aria-controls'))
  46. get('button').click()
  47. get('button').should(haveAttribute('aria-expanded', 'true'))
  48. get('button').should(haveAttribute('aria-controls', 'alpine-popover-panel-1'))
  49. },
  50. )
  51. test('escape closes panel',
  52. [html`
  53. <div x-data x-popover>
  54. <button x-popover:button>Toggle</button>
  55. <ul x-popover:panel>
  56. Dialog Contents!
  57. </ul>
  58. </div>
  59. `],
  60. ({ get }) => {
  61. get('ul').should(notBeVisible())
  62. get('button').click()
  63. get('ul').should(beVisible())
  64. get('body').type('{esc}')
  65. get('ul').should(notBeVisible())
  66. },
  67. )
  68. test('clicking outside closes panel',
  69. [html`
  70. <div>
  71. <div x-data x-popover>
  72. <button x-popover:button>Toggle</button>
  73. <ul x-popover:panel>
  74. Dialog Contents!
  75. </ul>
  76. </div>
  77. <h1>Click away to me</h1>
  78. </div>
  79. `],
  80. ({ get }) => {
  81. get('ul').should(notBeVisible())
  82. get('button').click()
  83. get('ul').should(beVisible())
  84. get('h1').click()
  85. get('ul').should(notBeVisible())
  86. },
  87. )
  88. test('focusing away closes panel',
  89. [html`
  90. <div>
  91. <div x-data x-popover>
  92. <button x-popover:button>Toggle</button>
  93. <ul x-popover:panel>
  94. Dialog Contents!
  95. </ul>
  96. </div>
  97. <a href="#">Focus Me</a>
  98. </div>
  99. `],
  100. ({ get }) => {
  101. get('ul').should(notBeVisible())
  102. get('button').click()
  103. get('ul').should(beVisible())
  104. cy.focused().tab()
  105. get('ul').should(notBeVisible())
  106. },
  107. )
  108. test('focusing away doesnt close panel if focusing inside a group',
  109. [html`
  110. <div x-data>
  111. <div x-popover:group>
  112. <div x-data x-popover id="1">
  113. <button x-popover:button>Toggle 1</button>
  114. <ul x-popover:panel>
  115. Dialog 1 Contents!
  116. </ul>
  117. </div>
  118. <div x-data x-popover id="2">
  119. <button x-popover:button>Toggle 2</button>
  120. <ul x-popover:panel>
  121. Dialog 2 Contents!
  122. </ul>
  123. </div>
  124. </div>
  125. <a href="#">Focus Me</a>
  126. </div>
  127. `],
  128. ({ get }) => {
  129. get('#1 ul').should(notBeVisible())
  130. get('#2 ul').should(notBeVisible())
  131. get('#1 button').click()
  132. get('#1 ul').should(beVisible())
  133. get('#2 ul').should(notBeVisible())
  134. cy.focused().tab()
  135. get('#1 ul').should(beVisible())
  136. get('#2 ul').should(notBeVisible())
  137. cy.focused().tab()
  138. get('#1 ul').should(notBeVisible())
  139. get('#2 ul').should(notBeVisible())
  140. },
  141. )
  142. test.retry(5)('focusing away still closes panel inside a group if the focus attribute is present',
  143. [html`
  144. <div x-data>
  145. <div x-popover:group>
  146. <div x-data x-popover id="1">
  147. <button x-popover:button>Toggle 1</button>
  148. <ul x-popover:panel focus>
  149. <a href="#">Dialog 1 Contents!</a>
  150. </ul>
  151. </div>
  152. <div x-data x-popover id="2">
  153. <button x-popover:button>Toggle 2</button>
  154. <ul x-popover:panel>
  155. <a href="#">Dialog 2 Contents!</a>
  156. </ul>
  157. </div>
  158. </div>
  159. <a href="#">Focus Me</a>
  160. </div>
  161. `],
  162. ({ get }) => {
  163. get('#1 ul').should(notBeVisible())
  164. get('#2 ul').should(notBeVisible())
  165. get('#1 button').click()
  166. get('#1 ul').should(beVisible())
  167. get('#2 ul').should(notBeVisible())
  168. cy.focused().tab()
  169. get('#1 ul').should(notBeVisible())
  170. get('#2 ul').should(notBeVisible())
  171. },
  172. )