x-init.spec.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { haveText, html, test } from '../../utils'
  2. test('sets text on init',
  3. html`
  4. <div x-data="{ foo: 'bar' }" x-init="foo = 'baz'">
  5. <span x-text="foo"></span>
  6. </div>
  7. `,
  8. ({ get }) => get('span').should(haveText('baz'))
  9. )
  10. test('x-init can be used outside of x-data',
  11. html`
  12. <div x-init="$el.textContent = 'foo'"></div>
  13. `,
  14. ({ get }) => get('div').should(haveText('foo'))
  15. )
  16. test('changes made in x-init happen before the rest of the component',
  17. html`
  18. <div x-data="{ foo: 'bar' }" x-init="$refs.foo.innerText = 'yo'">
  19. <span x-text="foo" x-ref="foo">baz</span>
  20. </div>
  21. `,
  22. ({ get }) => get('span').should(haveText('bar'))
  23. )
  24. test('can make deferred changes with $nextTick',
  25. html`
  26. <div x-data="{ foo: 'bar' }" x-init="$nextTick(() => $refs.foo.innerText = 'yo')">
  27. <span x-text="foo" x-ref="foo">baz</span>
  28. </div>
  29. `,
  30. ({ get }) => get('span').should(haveText('yo'))
  31. )
  32. test('x-init will not evaluate expression if it is empty',
  33. html`
  34. <div x-data="{ foo: 'bar' }" x-init=" ">
  35. <span x-text="foo" x-ref="foo">baz</span>
  36. </div>
  37. `,
  38. ({ get }) => get('span').should(haveText('bar'))
  39. )