$nextTick.spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { haveText, html, test } from '../../utils'
  2. test('$nextTick runs code on the next available managed tick',
  3. html`
  4. <div x-data="{foo: 'bar'}">
  5. <span x-text="foo" x-ref="span"></span>
  6. <button x-on:click="foo = 'baz'; $nextTick(() => {$refs.span.textContent = 'bob'})">click</button>
  7. </div>
  8. `,
  9. ({ get }) => {
  10. get('span').should(haveText('bar'))
  11. get('button').click()
  12. get('span').should(haveText('bob'))
  13. }
  14. )
  15. test('$nextTick waits for x-for to finish rendering',
  16. html`
  17. <div x-data="{list: ['one', 'two'], check: 2}">
  18. <template x-for="item in list">
  19. <span x-text="item"></span>
  20. </template>
  21. <p x-text="check"></p>
  22. <button x-on:click="list = ['one', 'two', 'three']; $nextTick(() => {check = document.querySelectorAll('span').length})">click</button>
  23. </div>
  24. `,
  25. ({ get }) => {
  26. get('p').should(haveText('2'))
  27. get('button').click()
  28. get('p').should(haveText('3'))
  29. }
  30. )
  31. test('$nextTick works with transition',
  32. html`
  33. <div x-data="{ show: false, loggedDisplayStyle: null }" x-init="$nextTick(() => { loggedDisplayStyle = document.querySelector('h1').style.display })">
  34. <h1 x-show="show" x-transition:enter="animation-enter"></h1>
  35. <h2 x-text="loggedDisplayStyle"></h2>
  36. <button @click="show = true; $nextTick(() => { loggedDisplayStyle = document.querySelector('h1').style.display })">click</button>
  37. </div>
  38. `,
  39. ({ get }) => {
  40. get('h2').should(haveText('none'))
  41. get('button').click()
  42. get('h2').should(haveText(''))
  43. }
  44. )