lifecycle.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.innerText }
  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('x-created', async () => {
  45. var spanValue
  46. window.setSpanValue = (el) => {
  47. spanValue = el.innerHTML
  48. }
  49. document.body.innerHTML = `
  50. <div x-data="{ foo: 'bar' }" x-created="window.setSpanValue($refs.foo)">
  51. <span x-text="foo" x-ref="foo">baz</span>
  52. </div>
  53. `
  54. Alpine.start()
  55. expect(spanValue).toEqual('baz')
  56. })
  57. test('x-mounted', async () => {
  58. var spanValue
  59. window.setSpanValue = (el) => {
  60. spanValue = el.innerText
  61. }
  62. document.body.innerHTML = `
  63. <div x-data="{ foo: 'bar' }" x-mounted="window.setSpanValue($refs.foo)">
  64. <span x-text="foo" x-ref="foo">baz</span>
  65. </div>
  66. `
  67. Alpine.start()
  68. expect(spanValue).toEqual('bar')
  69. })
  70. test('callbacks registered within x-created can affect reactive data changes', async () => {
  71. document.body.innerHTML = `
  72. <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-created="foo()">
  73. <button x-ref="foo"></button>
  74. <span x-text="bar"></span>
  75. </div>
  76. `
  77. Alpine.start()
  78. expect(document.querySelector('span').innerText).toEqual('baz')
  79. document.querySelector('button').click()
  80. await wait(() => { expect(document.querySelector('span').innerText).toEqual('bob') })
  81. })
  82. test('callbacks registered within x-mounted can affect reactive data changes', async () => {
  83. document.body.innerHTML = `
  84. <div x-data="{ bar: 'baz', foo() { this.$refs.foo.addEventListener('click', () => { this.bar = 'bob' }) } }" x-mounted="foo()">
  85. <button x-ref="foo"></button>
  86. <span x-text="bar"></span>
  87. </div>
  88. `
  89. Alpine.start()
  90. expect(document.querySelector('span').innerText).toEqual('baz')
  91. document.querySelector('button').click()
  92. await wait(() => { expect(document.querySelector('span').innerText).toEqual('bob') })
  93. })