custom-bind.spec.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { haveText, html, test } from '../utils'
  2. test('can register custom bind object',
  3. html`
  4. <script>
  5. document.addEventListener('alpine:init', () => {
  6. Alpine.bind('Foo', {
  7. 'x-init'() { this.$el.innerText = 'bar' },
  8. })
  9. })
  10. </script>
  11. <div x-data x-bind="Foo"></div>
  12. `,
  13. ({ get }) => get('div').should(haveText('bar'))
  14. )
  15. test('can register custom bind as function',
  16. html`
  17. <script>
  18. document.addEventListener('alpine:init', () => {
  19. Alpine.bind('Foo', () => ({
  20. 'x-init'() { this.$el.innerText = 'bar' },
  21. }))
  22. })
  23. </script>
  24. <div x-data x-bind="Foo"></div>
  25. `,
  26. ({ get }) => get('div').should(haveText('bar'))
  27. )
  28. test('can consume custom bind as function',
  29. html`
  30. <script>
  31. document.addEventListener('alpine:init', () => {
  32. Alpine.bind('Foo', (subject) => ({
  33. 'x-init'() { this.$el.innerText = subject },
  34. }))
  35. })
  36. </script>
  37. <div x-data x-bind="Foo('bar')"></div>
  38. `,
  39. ({ get }) => get('div').should(haveText('bar'))
  40. )
  41. test('can bind directives individually to an element',
  42. html`
  43. <script>
  44. document.addEventListener('alpine:init', () => {
  45. Alpine.bind(document.querySelector('#one'), () => ({
  46. 'x-text'() { return 'foo' },
  47. }))
  48. })
  49. </script>
  50. <div x-data id="one"></div>
  51. `,
  52. ({ get }) => get('div').should(haveText('foo'))
  53. )