lifecycle.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Alpine from 'alpinejs'
  2. import { wait } from '@testing-library/dom'
  3. global.MutationObserver = class {
  4. observe() {}
  5. }
  6. test('x-init', async () => {
  7. var spanValue
  8. window.setSpanValue = (el) => {
  9. spanValue = el.innerHTML
  10. }
  11. document.body.innerHTML = `
  12. <div x-data="{ foo: 'bar' }" x-init="window.setSpanValue($refs.foo)">
  13. <span x-text="foo" x-ref="foo">baz</span>
  14. </div>
  15. `
  16. Alpine.start()
  17. expect(spanValue).toEqual('baz')
  18. })
  19. test('x-init from data function with callback return for "x-mounted" functionality', async () => {
  20. var valueA
  21. var valueB
  22. window.setValueA = (el) => { valueA = el.innerHTML }
  23. window.setValueB = (el) => { valueB = el.textContent }
  24. window.data = function () {
  25. return {
  26. foo: 'bar',
  27. init() {
  28. window.setValueA(this.$refs.foo)
  29. return () => {
  30. window.setValueB(this.$refs.foo)
  31. }
  32. }
  33. }
  34. }
  35. document.body.innerHTML = `
  36. <div x-data="window.data()" x-init="init()">
  37. <span x-text="foo" x-ref="foo">baz</span>
  38. </div>
  39. `
  40. Alpine.start()
  41. expect(valueA).toEqual('baz')
  42. expect(valueB).toEqual('bar')
  43. })
  44. test('callbacks registered within x-init can affect reactive data changes', async () => {
  45. document.body.innerHTML = `
  46. <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-init="foo()">
  47. <button x-ref="foo"></button>
  48. <span x-text="bar"></span>
  49. </div>
  50. `
  51. Alpine.start()
  52. expect(document.querySelector('span').textContent).toEqual('baz')
  53. document.querySelector('button').click()
  54. await wait(() => { expect(document.querySelector('span').textContent).toEqual('bob') })
  55. })
  56. test('callbacks registered within x-init callback can affect reactive data changes', async () => {
  57. document.body.innerHTML = `
  58. <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-init="() => { foo() }">
  59. <button x-ref="foo"></button>
  60. <span x-text="bar"></span>
  61. </div>
  62. `
  63. Alpine.start()
  64. expect(document.querySelector('span').textContent).toEqual('baz')
  65. document.querySelector('button').click()
  66. await wait(() => { expect(document.querySelector('span').textContent).toEqual('bob') })
  67. })
  68. test('x-init is capable of dispatching an event', async () => {
  69. document.body.innerHTML = `
  70. <div x-data="{ foo: 'bar' }" @update-foo="foo = $event.detail.foo">
  71. <div x-data x-init="$dispatch('update-foo', { foo: 'baz' })"></div>
  72. <span x-text="foo"></span>
  73. </div>
  74. `
  75. Alpine.start()
  76. await wait(() => {
  77. expect(document.querySelector('span').textContent).toEqual('baz')
  78. })
  79. })
  80. test('onBeforeComponentInitialized is capable of modifying the element', async () => {
  81. document.body.innerHTML = `
  82. <div x-data="{ init() { window.foo = 'bar' } }"></div>
  83. `
  84. Alpine.onBeforeComponentInitialized(component => {
  85. if (! component.$el.hasAttribute('x-init') && component.$data.init)
  86. component.$el.setAttribute('x-init', 'init()')
  87. })
  88. Alpine.start()
  89. await wait(() => {
  90. expect(document.querySelector('div').getAttribute('x-init')).toEqual('init()')
  91. expect(window.foo).toEqual('bar')
  92. })
  93. })