trap.spec.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { haveText, test, html, haveFocus } 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. )