1
0

custom-interceptors.spec.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { haveText, html, test } from '../utils'
  2. test('can register custom interceptors',
  3. [html`
  4. <div x-data="{ foo: $magic() }">
  5. <span x-text="foo"></span>
  6. </div>
  7. `,
  8. `
  9. Alpine.magic('magic', () => {
  10. return Alpine.interceptor((initialValue, getter, setter, path, key) => {
  11. return key+path
  12. })
  13. })
  14. `],
  15. ({ get }) => get('span').should(haveText('foofoo'))
  16. )
  17. test('interceptors are nesting aware',
  18. [html`
  19. <div x-data="{ foo: { bar: { baz: $magic() }}}">
  20. <span x-text="foo.bar.baz"></span>
  21. </div>
  22. `,
  23. `
  24. Alpine.magic('magic', () => {
  25. return Alpine.interceptor((initialValue, getter, setter, path, key) => {
  26. return key+path
  27. })
  28. })
  29. `],
  30. ({ get }) => get('span').should(haveText('bazfoo.bar.baz'))
  31. )
  32. test('interceptor system prevents against circular references',
  33. [html`
  34. <div x-data="{ foo: $foo }">
  35. <span x-text="'...'">
  36. </div>
  37. `,
  38. `
  39. Alpine.magic('foo', () => {
  40. return {
  41. get anyGivenProperty() {
  42. return this
  43. }
  44. }
  45. })
  46. `],
  47. ({ get }) => get('span').should(haveText('...'))
  48. )