morph.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { haveText, html, test } from '../../utils'
  2. test('can morph components and preserve Alpine state',
  3. [html`
  4. <div x-data="{ foo: 'bar' }">
  5. <button @click="foo = 'baz'">Change Foo</button>
  6. <span x-text="foo"></span>
  7. </div>
  8. `],
  9. ({ get }, reload, window, document) => {
  10. let toHtml = document.querySelector('div').outerHTML
  11. get('span').should(haveText('bar'))
  12. get('button').click()
  13. get('span').should(haveText('baz'))
  14. get('div').then(([el]) => window.Alpine.morph(el, toHtml))
  15. get('span').should(haveText('baz'))
  16. },
  17. )
  18. test('morphing target uses outer Alpine scope',
  19. [html`
  20. <article x-data="{ foo: 'bar' }">
  21. <div>
  22. <button @click="foo = 'baz'">Change Foo</button>
  23. <span x-text="foo"></span>
  24. </div>
  25. </article>
  26. `],
  27. ({ get }, reload, window, document) => {
  28. let toHtml = document.querySelector('div').outerHTML
  29. get('span').should(haveText('bar'))
  30. get('button').click()
  31. get('span').should(haveText('baz'))
  32. get('div').then(([el]) => window.Alpine.morph(el, toHtml))
  33. get('span').should(haveText('baz'))
  34. },
  35. )
  36. test('can morph with HTML change and preserve Alpine state',
  37. [html`
  38. <div x-data="{ foo: 'bar' }">
  39. <button @click="foo = 'baz'">Change Foo</button>
  40. <span x-text="foo"></span>
  41. </div>
  42. `],
  43. ({ get }, reload, window, document) => {
  44. let toHtml = document.querySelector('div').outerHTML.replace('Change Foo', 'Changed Foo')
  45. get('span').should(haveText('bar'))
  46. get('button').click()
  47. get('span').should(haveText('baz'))
  48. get('button').should(haveText('Change Foo'))
  49. get('div').then(([el]) => window.Alpine.morph(el, toHtml))
  50. get('span').should(haveText('baz'))
  51. get('button').should(haveText('Changed Foo'))
  52. },
  53. )
  54. test('morphing an element with multiple nested Alpine components preserves scope',
  55. [html`
  56. <div x-data="{ foo: 'bar' }">
  57. <button @click="foo = 'baz'">Change Foo</button>
  58. <span x-text="foo"></span>
  59. <div x-data="{ bob: 'lob' }">
  60. <a href="#" @click.prevent="bob = 'law'">Change Bob</a>
  61. <h1 x-text="bob"></h1>
  62. </div>
  63. </div>
  64. `],
  65. ({ get }, reload, window, document) => {
  66. let toHtml = document.querySelector('div').outerHTML
  67. get('span').should(haveText('bar'))
  68. get('h1').should(haveText('lob'))
  69. get('button').click()
  70. get('a').click()
  71. get('span').should(haveText('baz'))
  72. get('h1').should(haveText('law'))
  73. get('div').then(([el]) => window.Alpine.morph(el, toHtml))
  74. get('span').should(haveText('baz'))
  75. get('h1').should(haveText('law'))
  76. },
  77. )
  78. test('can morph portals',
  79. [html`
  80. <div x-data="{ count: 1 }" id="a">
  81. <button @click="count++">Inc</button>
  82. <template x-portal="foo">
  83. <div>
  84. <h1 x-text="count"></h1>
  85. <h2>hey</h2>
  86. </div>
  87. </template>
  88. </div>
  89. <div id="b">
  90. <template x-portal-target="foo"></template>
  91. </div>
  92. `],
  93. ({ get }, reload, window, document) => {
  94. let toHtml = html`
  95. <div x-data="{ count: 1 }" id="a">
  96. <button @click="count++">Inc</button>
  97. <template x-portal="foo">
  98. <div>
  99. <h1 x-text="count"></h1>
  100. <h2>there</h2>
  101. </div>
  102. </template>
  103. </div>
  104. `
  105. get('h1').should(haveText('1'))
  106. get('h2').should(haveText('hey'))
  107. get('button').click()
  108. get('h1').should(haveText('2'))
  109. get('h2').should(haveText('hey'))
  110. get('div#a').then(([el]) => window.Alpine.morph(el, toHtml))
  111. get('h1').should(haveText('2'))
  112. get('h2').should(haveText('there'))
  113. },
  114. )