intersect.spec.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { haveText, test, html } from '../../utils'
  2. test('can intersect',
  3. [html`
  4. <div x-data="{ count: 0 }">
  5. <span x-text="count"></span>
  6. <div x-intersect="count++" style="margin-top: 100vh;" id="1">hi</div>
  7. </div>
  8. `],
  9. ({ get }) => {
  10. get('span').should(haveText('0'))
  11. get('#1').scrollIntoView({duration: 100})
  12. get('span').should(haveText('1'))
  13. get('span').scrollIntoView({duration: 100})
  14. get('span').should(haveText('1'))
  15. get('#1').scrollIntoView({duration: 100})
  16. get('span').should(haveText('2'))
  17. },
  18. )
  19. test('It should evaluate with ":enter" only when the component is intersected',
  20. [html`
  21. <div x-data="{ count: 0 }">
  22. <span x-text="count"></span>
  23. <div x-intersect:enter="count++" style="margin-top: 100vh;" id="1">hi</div>
  24. </div>
  25. `],
  26. ({ get }) => {
  27. get('span').should(haveText('0'))
  28. get('#1').scrollIntoView({duration: 100})
  29. get('span').should(haveText('1'))
  30. get('span').scrollIntoView({duration: 100})
  31. get('span').should(haveText('1'))
  32. get('#1').scrollIntoView({duration: 100})
  33. get('span').should(haveText('2'))
  34. },
  35. )
  36. test('It should evaluate with ":leave" only when the component is not intersected',
  37. [html`
  38. <div x-data="{ count: 0 }">
  39. <span x-text="count"></span>
  40. <div x-intersect:leave="count++" style="margin-top: 100vh;" id="1">hi</div>
  41. </div>
  42. `],
  43. ({ get }) => {
  44. get('span').should(haveText('1'))
  45. get('#1').scrollIntoView({duration: 100})
  46. get('span').should(haveText('1'))
  47. get('span').scrollIntoView({duration: 100})
  48. get('span').should(haveText('2'))
  49. get('#1').scrollIntoView({duration: 100})
  50. get('span').should(haveText('2'))
  51. get('span').scrollIntoView({duration: 100})
  52. get('span').should(haveText('3'))
  53. },
  54. )
  55. test('.once',
  56. [html`
  57. <div x-data="{ count: 0 }" x-init="setTimeout(() => count++, 300)">
  58. <span x-text="count"></span>
  59. <div x-intersect.once="count++" style="margin-top: 100vh;" id="1">hi</div>
  60. </div>
  61. `],
  62. ({ get }) => {
  63. get('span').should(haveText('0'))
  64. get('#1').scrollIntoView({duration: 100})
  65. get('span').should(haveText('1'))
  66. get('span').scrollIntoView({duration: 100})
  67. get('span').should(haveText('1'))
  68. get('#1').scrollIntoView({duration: 100})
  69. get('span').should(haveText('2'))
  70. },
  71. )