custom-magics.spec.js 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { haveText, html, test } from '../utils'
  2. test('can register custom magic properties',
  3. html`
  4. <script>
  5. document.addEventListener('alpine:init', () => {
  6. Alpine.magic('foo', (el) => {
  7. return { bar: 'baz' }
  8. })
  9. })
  10. </script>
  11. <div x-data>
  12. <span x-text="$foo.bar"></span>
  13. </div>
  14. `,
  15. ({ get }) => get('span').should(haveText('baz'))
  16. )
  17. test('magics are lazily accessed',
  18. html`
  19. <script>
  20. window.hasBeenAccessed = false
  21. document.addEventListener('alpine:init', () => {
  22. Alpine.magic('foo', (el) => {
  23. window.hasBeenAccessed = true
  24. })
  25. })
  26. </script>
  27. <div x-data>
  28. <button @click="$el.textContent = window.hasBeenAccessed">clickme</button>
  29. </div>
  30. `,
  31. ({ get }) => {
  32. get('button').click()
  33. get('button').should(haveText('false'))
  34. }
  35. )