data.spec.js 777 B

1234567891011121314151617181920212223242526272829303132
  1. import Alpine from 'alpinejs'
  2. import { fireEvent, wait } from '@testing-library/dom'
  3. global.MutationObserver = class {
  4. observe() {}
  5. }
  6. test('data manipulated on component object is reactive', async () => {
  7. document.body.innerHTML = `
  8. <div x-data="{ foo: 'bar' }">
  9. <span x-text="foo"></span>
  10. </div>
  11. `
  12. Alpine.start()
  13. document.querySelector('div').__x.$data.foo = 'baz'
  14. await wait(() => { expect(document.querySelector('span').innerText).toEqual('baz') })
  15. })
  16. test('x-data attribute value is optional', async () => {
  17. document.body.innerHTML = `
  18. <div x-data>
  19. <span x-text="'foo'"></span>
  20. </div>
  21. `
  22. Alpine.start()
  23. expect(document.querySelector('span').innerText).toEqual('foo')
  24. })