1
0

mutation.spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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('nested element side effects are cleaned up after the parent is removed',
  25. html`
  26. <div x-data="{ foo: 1, bar: 1 }">
  27. <button @click="bar++">bar</button>
  28. <a href="#" @click.prevent="$refs.article.remove()">remove</a>
  29. <article x-ref="article">
  30. <span x-text="(() => { foo = foo + 1; return bar })"></span>
  31. </article>
  32. <h1 x-text="foo"></h1>
  33. <h2 x-text="bar"></h2>
  34. </div>
  35. `,
  36. ({ get }) => {
  37. get('h1').should(haveText('2'))
  38. get('h2').should(haveText('1'))
  39. get('button').click()
  40. get('h1').should(haveText('3'))
  41. get('h2').should(haveText('2'))
  42. get('a').click()
  43. get('button').click()
  44. get('h1').should(haveText('3'))
  45. get('h2').should(haveText('3'))
  46. }
  47. )
  48. test('can mutate directive value',
  49. html`
  50. <div x-data="{ foo: 'bar', bar: 'baz' }">
  51. <button @click="$refs.target.setAttribute('x-text', 'bar')">change text</button>
  52. <span x-text="foo" x-ref="target"></span>
  53. </div>
  54. `,
  55. ({ get }) => {
  56. get('span').should(haveText('bar'))
  57. get('button').click()
  58. get('span').should(haveText('baz'))
  59. }
  60. )
  61. test('can add new directive',
  62. html`
  63. <div x-data="{ foo: 'bar' }">
  64. <button @click="$refs.target.setAttribute('x-text', 'foo')">change text</button>
  65. <span x-ref="target"></span>
  66. </div>
  67. `,
  68. ({ get }) => {
  69. get('span').should(haveText(''))
  70. get('button').click()
  71. get('span').should(haveText('bar'))
  72. }
  73. )