intersect.spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. )
  112. test('.margin',
  113. [html`
  114. <div x-data="{ count: 0 }">
  115. <span x-text="count"></span>
  116. <div id="buffer-top" style="height: calc(100vh - 50px); margin-top: 100vh; background: pink"></div>
  117. <div id="buffer-bottom" style="height: 50px; background: green"></div>
  118. <div x-intersect.margin.100px="count++;$nextTick(() => console.log(count))" id="1">hi</div>
  119. </div>
  120. `],
  121. ({ get }) => {
  122. get('span').should(haveText('0'))
  123. get('#buffer-top').scrollIntoView({duration: 100})
  124. get('span').should(haveText('1'))
  125. get('#1').scrollIntoView({duration: 100})
  126. get('span').should(haveText('1'))
  127. get('span').scrollIntoView({duration: 100})
  128. get('span').should(haveText('1'))
  129. get('#buffer-top').scrollIntoView({duration: 100})
  130. get('span').should(haveText('2'))
  131. get('#1').scrollIntoView({duration: 100})
  132. get('span').should(haveText('2'))
  133. },
  134. )
  135. test('.threshold',
  136. [html`
  137. <div x-data="{ count: 0 }">
  138. <div x-ref="foo" style="width: 250px; overflow: scroll; display: flex; justify-content: start">
  139. <div style="min-width: 250px;">first</div>
  140. <div style="min-width: 250px" x-intersect.threshold.50="count++;">second</div>
  141. </div>
  142. <button @click="$refs.foo.scrollTo({ left: 15 })" id="1">first</button>
  143. <button @click="$refs.foo.scrollTo({ left: 250 })" id="2">second</button>
  144. <span x-text="count"></span>
  145. </div>
  146. `],
  147. ({ get }) => {
  148. get('span').should(haveText('0'))
  149. get('#1').click()
  150. get('span').should(haveText('0'))
  151. get('#2').click()
  152. get('span').should(haveText('1'))
  153. },
  154. )