html.spec.js 927 B

123456789101112131415161718192021222324252627282930313233343536
  1. import Alpine from 'alpinejs'
  2. import { wait } from '@testing-library/dom'
  3. global.MutationObserver = class {
  4. observe() {}
  5. }
  6. test('x-html on init', async () => {
  7. document.body.innerHTML = `
  8. <div x-data="{ foo: '<h1>bar</h1>' }">
  9. <span x-html="foo"></span>
  10. </div>
  11. `
  12. Alpine.start()
  13. await wait(() => { expect(document.querySelector('span').innerHTML).toEqual('<h1>bar</h1>') })
  14. })
  15. test('x-html on triggered update', async () => {
  16. document.body.innerHTML = `
  17. <div x-data="{ foo: '' }">
  18. <button x-on:click="foo = '<h1>bar</h1>'"></button>
  19. <span x-html="foo"></span>
  20. </div>
  21. `
  22. Alpine.start()
  23. await wait(() => { expect(document.querySelector('span').innerHTML).toEqual('') })
  24. document.querySelector('button').click()
  25. await wait(() => { expect(document.querySelector('span').innerHTML).toEqual('<h1>bar</h1>') })
  26. })