constructor.spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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>
  14. `
  15. Alpine.start()
  16. fireEvent.input(document.querySelector('input'), { target: { value: 'bar' }})
  17. await wait(() => { expect(document.querySelector('#A span').innerText).toEqual('bar') })
  18. const div = document.createElement('div')
  19. div.setAttribute('id', 'B')
  20. div.setAttribute('x-data', '{ foo: "baz" }')
  21. div.innerHTML = `
  22. <input x-model="foo">
  23. <span x-text="foo"></span>
  24. `
  25. document.body.appendChild(div)
  26. runObservers[1]([
  27. { addedNodes: [ div ] }
  28. ])
  29. await wait(() => {
  30. expect(document.querySelector('#A span').innerText).toEqual('bar')
  31. expect(document.querySelector('#B span').innerText).toEqual('baz')
  32. })
  33. })
  34. test('auto-initialize new elements added to a component', async () => {
  35. var runObservers = []
  36. global.MutationObserver = class {
  37. constructor(callback) { runObservers.push(callback) }
  38. observe() {}
  39. }
  40. document.body.innerHTML = `
  41. <div x-data="{ count: 0 }">
  42. <span x-text="count"></span>
  43. <div id="target">
  44. </div>
  45. </div>
  46. `
  47. Alpine.start()
  48. expect(document.querySelector('span').innerText).toEqual(0)
  49. document.querySelector('#target').innerHTML = `
  50. <span x-text="count"></span>
  51. <button x-on:click="count++"></button>
  52. `
  53. runObservers[0]([
  54. { target: document.querySelector('#target'), addedNodes: [
  55. document.querySelector('#target span'),
  56. document.querySelector('#target button'),
  57. ] }
  58. ])
  59. await wait(() => { expect(document.querySelector('#target span').innerText).toEqual(0) })
  60. document.querySelector('button').click()
  61. await wait(() => { expect(document.querySelector('span').innerText).toEqual(1) })
  62. await wait(() => { expect(document.querySelector('#target span').innerText).toEqual(1) })
  63. })
  64. test('auto-detect x-data property changes at run-time', async () => {
  65. var runObservers = []
  66. global.MutationObserver = class {
  67. constructor(callback) { runObservers.push(callback) }
  68. observe() {}
  69. }
  70. document.body.innerHTML = `
  71. <div x-data="{ count: 0 }">
  72. <span x-text="count"></span>
  73. </div>
  74. `
  75. Alpine.start()
  76. expect(document.querySelector('span').innerText).toEqual(0)
  77. document.querySelector('div').setAttribute('x-data', '{ count: 1 }')
  78. runObservers[0]([
  79. {
  80. addedNodes: [],
  81. type: 'attributes',
  82. attributeName: 'x-data',
  83. target: document.querySelector('div')
  84. }
  85. ])
  86. await wait(() => { expect(document.querySelector('span').innerText).toEqual(1) })
  87. })