constructor.spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import Alpine from 'alpinejs'
  2. import { fireEvent, wait } from '@testing-library/dom'
  3. test('auto-detect new components and dont lose state of existing ones', async () => {
  4. var runObservers = []
  5. global.MutationObserver = class {
  6. constructor(callback) { runObservers.push(callback) }
  7. observe() {}
  8. }
  9. document.body.innerHTML = `
  10. <div id="A" x-data="{ foo: '' }">
  11. <input x-model="foo">
  12. <span x-text="foo"></span>
  13. <div id="B"></div>
  14. </div>
  15. `
  16. Alpine.start()
  17. fireEvent.input(document.querySelector('input'), { target: { value: 'bar' }})
  18. await wait(() => { expect(document.querySelector('#A span').innerText).toEqual('bar') })
  19. document.querySelector('#B').innerHTML = `
  20. <div x-data="{foo: 'baz'}">
  21. <input x-model="foo">
  22. <span x-text="foo"></span>
  23. </div>
  24. `
  25. runObservers[0]([
  26. {
  27. target: document.querySelector('#A'),
  28. type: 'childList',
  29. addedNodes: [ document.querySelector('#B div') ],
  30. }
  31. ])
  32. await wait(() => {
  33. expect(document.querySelector('#A span').innerText).toEqual('bar')
  34. expect(document.querySelector('#B span').innerText).toEqual('baz')
  35. })
  36. })
  37. test('auto-detect new components that are wrapped in non-new component tags', async () => {
  38. var runObservers = []
  39. global.MutationObserver = class {
  40. constructor(callback) { runObservers.push(callback) }
  41. observe() {}
  42. }
  43. document.body.innerHTML = `
  44. <div id="A" x-data="{ foo: '' }">
  45. <input x-model="foo">
  46. <span x-text="foo"></span>
  47. <div id="B"></div>
  48. </div>
  49. `
  50. Alpine.start()
  51. fireEvent.input(document.querySelector('input'), { target: { value: 'bar' }})
  52. await wait(() => { expect(document.querySelector('#A span').innerText).toEqual('bar') })
  53. document.querySelector('#B').innerHTML = `
  54. <section>
  55. <div x-data="{foo: 'baz'}">
  56. <input x-model="foo">
  57. <span x-text="foo"></span>
  58. </div>
  59. </section>
  60. `
  61. runObservers[0]([
  62. {
  63. target: document.querySelector('#A'),
  64. type: 'childList',
  65. addedNodes: [ document.querySelector('#B section') ],
  66. }
  67. ])
  68. await wait(() => {
  69. expect(document.querySelector('#A span').innerText).toEqual('bar')
  70. expect(document.querySelector('#B span').innerText).toEqual('baz')
  71. })
  72. })
  73. test('auto-initialize new elements added to a component', async () => {
  74. var runObservers = []
  75. global.MutationObserver = class {
  76. constructor(callback) { runObservers.push(callback) }
  77. observe() {}
  78. }
  79. document.body.innerHTML = `
  80. <div x-data="{ count: 0 }">
  81. <span x-text="count"></span>
  82. <div id="target">
  83. </div>
  84. </div>
  85. `
  86. Alpine.start()
  87. expect(document.querySelector('span').innerText).toEqual(0)
  88. document.querySelector('#target').innerHTML = `
  89. <span x-text="count"></span>
  90. <button x-on:click="count++"></button>
  91. `
  92. runObservers[0]([
  93. { target: document.querySelector('#target'), addedNodes: [
  94. document.querySelector('#target span'),
  95. document.querySelector('#target button'),
  96. ] }
  97. ])
  98. await wait(() => { expect(document.querySelector('#target span').innerText).toEqual(0) })
  99. document.querySelector('button').click()
  100. await wait(() => { expect(document.querySelector('span').innerText).toEqual(1) })
  101. await wait(() => { expect(document.querySelector('#target span').innerText).toEqual(1) })
  102. })
  103. test('auto-detect x-data property changes at run-time', async () => {
  104. var runObservers = []
  105. global.MutationObserver = class {
  106. constructor(callback) { runObservers.push(callback) }
  107. observe() {}
  108. }
  109. document.body.innerHTML = `
  110. <div x-data="{ count: 0 }">
  111. <span x-text="count"></span>
  112. </div>
  113. `
  114. Alpine.start()
  115. expect(document.querySelector('span').innerText).toEqual(0)
  116. document.querySelector('div').setAttribute('x-data', '{ count: 1 }')
  117. runObservers[0]([
  118. {
  119. addedNodes: [],
  120. type: 'attributes',
  121. attributeName: 'x-data',
  122. target: document.querySelector('div')
  123. }
  124. ])
  125. await wait(() => { expect(document.querySelector('span').innerText).toEqual(1) })
  126. })