1
0

intersect.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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('.half',
  56. [html`
  57. <div x-data="{ count: 0 }">
  58. <span x-text="count"></span>
  59. <div id="container" style="height: 400px; overflow-y: scroll;">
  60. <div style="height: 410px;">spacer</div>
  61. <div style="height: 400px" x-intersect.half="count++">
  62. <div style="text-align: center;">content</div>
  63. </div>
  64. </div>
  65. </div>
  66. `],
  67. ({ get }) => {
  68. get('span').should(haveText('0'))
  69. get('#container').scrollTo(0, 100, {duration: 100})
  70. get('span').should(haveText('0'))
  71. get('#container').scrollTo(0, 210, {duration: 100})
  72. get('span').should(haveText('1'))
  73. },
  74. )
  75. test('.full',
  76. [html`
  77. <div x-data="{ count: 0 }">
  78. <span x-text="count"></span>
  79. <div id="container" style="height: 400px; overflow-y: scroll;">
  80. <div style="height: 401px;">spacer</div>
  81. <div style="height: 400px" x-intersect.full="count++">
  82. <div style="text-align: center;">content</div>
  83. </div>
  84. </div>
  85. </div>
  86. `],
  87. ({ get }) => {
  88. get('span').should(haveText('0'))
  89. get('#container').scrollTo(0, 200, {duration: 100})
  90. get('span').should(haveText('0'))
  91. get('#container').scrollTo(0, 400, {duration: 100})
  92. get('span').should(haveText('1'))
  93. },
  94. )
  95. test('.once',
  96. [html`
  97. <div x-data="{ count: 0 }" x-init="setTimeout(() => count++, 300)">
  98. <span x-text="count"></span>
  99. <div x-intersect.once="count++" style="margin-top: 100vh;" id="1">hi</div>
  100. </div>
  101. `],
  102. ({ get }) => {
  103. get('span').should(haveText('0'))
  104. get('#1').scrollIntoView({duration: 100})
  105. get('span').should(haveText('1'))
  106. get('span').scrollIntoView({duration: 100})
  107. get('span').should(haveText('1'))
  108. get('#1').scrollIntoView({duration: 100})
  109. get('span').should(haveText('2'))
  110. },
  111. )