focus.spec.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { haveText, test, html, haveFocus, notHaveAttribute, haveAttribute, notHaveFocus, notHaveText } from '../../utils'
  2. test('can trap focus',
  3. [html`
  4. <div x-data="{ open: false }">
  5. <input type="text" id="1">
  6. <button id="2" @click="open = true">open</button>
  7. <div>
  8. <div x-trap="open">
  9. <input type="text" id="3">
  10. <button @click="open = false" id="4">close</button>
  11. </div>
  12. </div>
  13. </div>
  14. `],
  15. ({ get }, reload) => {
  16. get('#1').click()
  17. get('#1').should(haveFocus())
  18. get('#2').click()
  19. get('#3').should(haveFocus())
  20. cy.focused().tab()
  21. get('#4').should(haveFocus())
  22. cy.focused().tab()
  23. get('#3').should(haveFocus())
  24. cy.focused().tab({shift: true})
  25. get('#4').should(haveFocus())
  26. cy.focused().click()
  27. get('#2').should(haveFocus())
  28. },
  29. )
  30. test('works with clone',
  31. [html`
  32. <div id="foo" x-data="{
  33. open: false,
  34. triggerClone() {
  35. var original = document.getElementById('foo');
  36. var copy = original.cloneNode(true);
  37. Alpine.clone(original, copy);
  38. var p = document.createElement('p');
  39. p.textContent = 'bar';
  40. copy.appendChild(p);
  41. original.parentNode.replaceChild(copy, original);
  42. }
  43. }">
  44. <button id="one" @click="open = true">Trap</button>
  45. <div x-trap="open">
  46. <input type="text">
  47. <button id="two" @click="triggerClone()">Test</button>
  48. </div>
  49. </div>
  50. `],
  51. ({ get, wait }, reload) => {
  52. get('#one').click()
  53. get('#two').click()
  54. get('p').should(haveText('bar'))
  55. }
  56. )
  57. test('releases focus when x-if is destroyed',
  58. [html`
  59. <div x-data="{ open: false }">
  60. <button id="1" @click="open = true">open</button>
  61. <template x-if="open">
  62. <div x-trap="open">
  63. <button @click="open = false" id="2">close</button>
  64. </div>
  65. </template>
  66. </div>
  67. `],
  68. ({ get }, reload) => {
  69. get('#1').click()
  70. get('#2').should(haveFocus())
  71. get('#2').click()
  72. get('#1').should(haveFocus())
  73. },
  74. )
  75. test('can trap focus with inert',
  76. [html`
  77. <div x-data="{ open: false }">
  78. <h1>I should have aria-hidden when outside trap</h1>
  79. <button id="open" @click="open = true">open</button>
  80. <div x-trap.inert="open">
  81. <button @click="open = false" id="close">close</button>
  82. </div>
  83. </div>
  84. `],
  85. ({ get }, reload) => {
  86. get('#open').should(notHaveAttribute('aria-hidden', 'true'))
  87. get('#open').click()
  88. get('#open').should(haveAttribute('aria-hidden', 'true'))
  89. get('#close').click()
  90. get('#open').should(notHaveAttribute('aria-hidden', 'true'))
  91. },
  92. )
  93. test('can trap focus with noscroll',
  94. [html`
  95. <div x-data="{ open: false }">
  96. <button id="open" @click="open = true">open</button>
  97. <div x-trap.noscroll="open">
  98. <button @click="open = false" id="close">close</button>
  99. </div>
  100. <div style="height: 100vh">&nbsp;</div>
  101. </div>
  102. `],
  103. ({ get, window }, reload) => {
  104. window().then((win) => {
  105. let scrollbarWidth = win.innerWidth - win.document.documentElement.clientWidth
  106. get('#open').click()
  107. get('html').should(haveAttribute('style', `overflow: hidden; padding-right: ${scrollbarWidth}px;`))
  108. get('#close').click()
  109. get('html').should(notHaveAttribute('style', `overflow: hidden; padding-right: ${scrollbarWidth}px;`))
  110. })
  111. },
  112. )
  113. test('can trap focus with noreturn',
  114. [html`
  115. <div x-data="{ open: false }" x-trap.noreturn="open">
  116. <input id="input" @focus="open = true">
  117. <div x-show="open">
  118. <button @click="open = false" id="close">close</button>
  119. </div>
  120. </div>
  121. `],
  122. ({ get }) => {
  123. get('#input').focus()
  124. get('#close')
  125. get('#close').click()
  126. get('#input').should(notHaveFocus())
  127. },
  128. )
  129. test('$focus.focus',
  130. [html`
  131. <div x-data>
  132. <button id="press-me" @click="$focus.focus(document.querySelector('#focus-me'))">Focus Other</button>
  133. <button id="focus-me">Other</button>
  134. </div>
  135. `],
  136. ({ get }) => {
  137. get('#focus-me').should(notHaveFocus())
  138. get('#press-me').click()
  139. get('#focus-me').should(haveFocus())
  140. },
  141. )
  142. test('$focus.focusable',
  143. [html`
  144. <div x-data>
  145. <div id="1" x-text="$focus.focusable($el)"></div>
  146. <button id="2" x-text="$focus.focusable($el)"></button>
  147. </div>
  148. `],
  149. ({ get }) => {
  150. get('#1').should(haveText('false'))
  151. get('#2').should(haveText('true'))
  152. },
  153. )
  154. test('$focus.focusables',
  155. [html`
  156. <div x-data>
  157. <h1 x-text="$focus.within($refs.container).focusables().length"></h1>
  158. <div x-ref="container">
  159. <button>1</button>
  160. <div>2</div>
  161. <button>3</button>
  162. </div>
  163. </div>
  164. `],
  165. ({ get }) => {
  166. get('h1').should(haveText('2'))
  167. },
  168. )
  169. test('$focus.focused',
  170. [html`
  171. <div x-data>
  172. <button @click="$el.textContent = $el.isSameNode($focus.focused())">im-focused</button>
  173. </div>
  174. `],
  175. ({ get }) => {
  176. get('button').click()
  177. get('button').should(haveText('true'))
  178. },
  179. )
  180. test('$focus.lastFocused',
  181. [html`
  182. <div x-data>
  183. <button id="1" x-ref="first">first-focused</button>
  184. <button id="2" @click="$el.textContent = $refs.first.isSameNode($focus.lastFocused())">second-focused</button>
  185. </div>
  186. `],
  187. ({ get }) => {
  188. get('#1').click()
  189. get('#2').click()
  190. get('#2').should(haveText('true'))
  191. },
  192. )
  193. test('$focus.within',
  194. [html`
  195. <div x-data>
  196. <button id="1" x-text="$focus.within($refs.first).focusables().length"></button>
  197. <div x-ref="first">
  198. <button>1</button>
  199. <button>2</button>
  200. </div>
  201. <div>
  202. <button>1</button>
  203. <button>2</button>
  204. <button>3</button>
  205. </div>
  206. </div>
  207. `],
  208. ({ get }) => {
  209. get('#1').should(haveText('2'))
  210. },
  211. )
  212. test('$focus.next',
  213. [html`
  214. <div x-data>
  215. <div x-ref="first">
  216. <button id="1" @click="$focus.within($refs.first).next(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
  217. <button>2</button>
  218. </div>
  219. </div>
  220. `],
  221. ({ get }) => {
  222. get('#1').click()
  223. get('#1').should(haveText('2'))
  224. },
  225. )
  226. test('$focus.prev',
  227. [html`
  228. <div x-data>
  229. <div x-ref="first">
  230. <button>2</button>
  231. <button id="1" @click="$focus.within($refs.first).prev(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
  232. </div>
  233. </div>
  234. `],
  235. ({ get }) => {
  236. get('#1').click()
  237. get('#1').should(haveText('2'))
  238. },
  239. )
  240. test('$focus.wrap',
  241. [html`
  242. <div x-data>
  243. <div x-ref="first">
  244. <button>2</button>
  245. <button id="1" @click="$focus.within($refs.first).wrap().next(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
  246. </div>
  247. </div>
  248. `],
  249. ({ get }) => {
  250. get('#1').click()
  251. get('#1').should(haveText('2'))
  252. },
  253. )
  254. test('$focus.first',
  255. [html`
  256. <div x-data>
  257. <button id="1" @click="$focus.within($refs.first).first(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
  258. <div x-ref="first">
  259. <button>2</button>
  260. <button>3</button>
  261. </div>
  262. </div>
  263. `],
  264. ({ get }) => {
  265. get('#1').click()
  266. get('#1').should(haveText('2'))
  267. },
  268. )
  269. test('$focus.last',
  270. [html`
  271. <div x-data>
  272. <button id="1" @click="$focus.within($refs.first).last(); $nextTick(() => $el.textContent = $focus.focused().textContent)">1</button>
  273. <div x-ref="first">
  274. <button>2</button>
  275. <button>3</button>
  276. </div>
  277. </div>
  278. `],
  279. ({ get }) => {
  280. get('#1').click()
  281. get('#1').should(haveText('3'))
  282. },
  283. )