mutation.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. )
  74. test('can pause and queue mutations for later resuming/flushing',
  75. html`
  76. <div x-data="{ foo: 1 }">
  77. <button x-on:click="setTimeout(() => foo++)" x-ref="btn">foo</button>
  78. <h1 x-text="foo"></h1>
  79. <a href="#" @click="$refs.btn.removeAttribute('x-on:click')" id="remove">remove</a>
  80. <a href="#" @click="$refs.btn.setAttribute('x-on:click', 'foo++')" id="add">add</a>
  81. <a href="#" @click="Alpine.deferMutations()" id="defer">add</a>
  82. <a href="#" @click="Alpine.flushAndStopDeferringMutations()" id="flush">add</a>
  83. </div>
  84. `,
  85. ({ get }) => {
  86. get('h1').should(haveText('1'))
  87. get('button').click()
  88. get('h1').should(haveText('2'))
  89. get('#remove').click()
  90. get('button').click()
  91. get('h1').should(haveText('2'))
  92. get('#defer').click()
  93. get('#add').click()
  94. get('button').click()
  95. get('h1').should(haveText('2'))
  96. get('#flush').click()
  97. get('button').click()
  98. get('h1').should(haveText('3'))
  99. }
  100. )