constructor.spec.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import projectX from 'project-x'
  2. import { fireEvent, wait } from '@testing-library/dom'
  3. global.MutationObserver = class {
  4. observe() {}
  5. }
  6. test('auto-detect new components and dont lose state of existing ones', async () => {
  7. var runObserver
  8. global.MutationObserver = class {
  9. constructor(callback) { runObserver = callback }
  10. observe() {}
  11. }
  12. document.body.innerHTML = `
  13. <div id="A" x-data="{ foo: '' }">
  14. <input x-model="foo">
  15. <span x-text="foo"></span>
  16. </div>
  17. `
  18. projectX.start()
  19. fireEvent.input(document.querySelector('input'), { target: { value: 'bar' }})
  20. await wait(() => { expect(document.querySelector('#A span').innerText).toEqual('bar') })
  21. const div = document.createElement('div')
  22. div.setAttribute('id', 'B')
  23. div.setAttribute('x-data', '{ foo: "baz" }')
  24. div.innerHTML = `
  25. <input x-model="foo">
  26. <span x-text="foo"></span>
  27. `
  28. document.body.appendChild(div)
  29. runObserver([
  30. { addedNodes: [ div ] }
  31. ])
  32. await wait(() => {
  33. expect(document.querySelector('#A span').innerText).toEqual('bar')
  34. expect(document.querySelector('#B span').innerText).toEqual('baz')
  35. })
  36. })