mutation.spec.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { haveText, html, test } from '../utils'
  2. test('element side effects are cleaned up after the elements are removed',
  3. html`
  4. <div x-data="{ foo: 1, bar: 1 }">
  5. <button @click="bar++">bar</button>
  6. <a href="#" @click.prevent="$refs.span.remove()">remove</a>
  7. <span x-text="(() => { foo = foo + 1; return bar })" x-ref="span"></span>
  8. <h1 x-text="foo"></h1>
  9. <h2 x-text="bar"></h2>
  10. </div>
  11. `,
  12. ({ get }) => {
  13. get('h1').should(haveText('2'))
  14. get('h2').should(haveText('1'))
  15. get('button').click()
  16. get('h1').should(haveText('3'))
  17. get('h2').should(haveText('2'))
  18. get('a').click()
  19. get('button').click()
  20. get('h1').should(haveText('3'))
  21. get('h2').should(haveText('3'))
  22. }
  23. )
  24. test('can mutate directive value',
  25. html`
  26. <div x-data="{ foo: 'bar', bar: 'baz' }">
  27. <button @click="$refs.target.setAttribute('x-text', 'bar')">change text</button>
  28. <span x-text="foo" x-ref="target"></span>
  29. </div>
  30. `,
  31. ({ get }) => {
  32. get('span').should(haveText('bar'))
  33. get('button').click()
  34. get('span').should(haveText('baz'))
  35. }
  36. )
  37. test('can add new directive',
  38. html`
  39. <div x-data="{ foo: 'bar' }">
  40. <button @click="$refs.target.setAttribute('x-text', 'foo')">change text</button>
  41. <span x-ref="target"></span>
  42. </div>
  43. `,
  44. ({ get }) => {
  45. get('span').should(haveText(''))
  46. get('button').click()
  47. get('span').should(haveText('bar'))
  48. }
  49. )