morph.spec.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import { haveAttribute, haveLength, haveText, haveValue, haveHtml, 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 teleports',
  79. [html`
  80. <div x-data="{ count: 1 }" id="a">
  81. <button @click="count++">Inc</button>
  82. <template x-teleport="#b">
  83. <div>
  84. <h1 x-text="count"></h1>
  85. <h2>hey</h2>
  86. </div>
  87. </template>
  88. </div>
  89. <div id="b"></div>
  90. `],
  91. ({ get }, reload, window, document) => {
  92. let toHtml = html`
  93. <div x-data="{ count: 1 }" id="a">
  94. <button @click="count++">Inc</button>
  95. <template x-teleport="#b">
  96. <div>
  97. <h1 x-text="count"></h1>
  98. <h2>there</h2>
  99. </div>
  100. </template>
  101. </div>
  102. `
  103. get('h1').should(haveText('1'))
  104. get('h2').should(haveText('hey'))
  105. get('button').click()
  106. get('h1').should(haveText('2'))
  107. get('h2').should(haveText('hey'))
  108. get('div#a').then(([el]) => window.Alpine.morph(el, toHtml))
  109. get('h1').should(haveText('2'))
  110. get('h2').should(haveText('there'))
  111. },
  112. )
  113. test('can morph',
  114. [html`
  115. <ul>
  116. <li>foo<input></li>
  117. </ul>
  118. `],
  119. ({ get }, reload, window, document) => {
  120. let toHtml = html`
  121. <ul>
  122. <li>bar<input></li>
  123. <li>foo<input></li>
  124. </ul>
  125. `
  126. get('input').type('foo')
  127. get('ul').then(([el]) => window.Alpine.morph(el, toHtml))
  128. get('li').should(haveLength(2))
  129. get('li:nth-of-type(1)').should(haveText('bar'))
  130. get('li:nth-of-type(2)').should(haveText('foo'))
  131. get('li:nth-of-type(1) input').should(haveValue('foo'))
  132. get('li:nth-of-type(2) input').should(haveValue(''))
  133. },
  134. )
  135. test('can morph using lookahead',
  136. [html`
  137. <ul>
  138. <li>foo<input></li>
  139. </ul>
  140. `],
  141. ({ get }, reload, window, document) => {
  142. let toHtml = html`
  143. <ul>
  144. <li>bar<input></li>
  145. <li>baz<input></li>
  146. <li>foo<input></li>
  147. </ul>
  148. `
  149. get('input').type('foo')
  150. get('ul').then(([el]) => window.Alpine.morph(el, toHtml, {lookahead: true}))
  151. get('li').should(haveLength(3))
  152. get('li:nth-of-type(1)').should(haveText('bar'))
  153. get('li:nth-of-type(2)').should(haveText('baz'))
  154. get('li:nth-of-type(3)').should(haveText('foo'))
  155. get('li:nth-of-type(1) input').should(haveValue(''))
  156. get('li:nth-of-type(2) input').should(haveValue(''))
  157. get('li:nth-of-type(3) input').should(haveValue('foo'))
  158. },
  159. )
  160. test('can morph using keys',
  161. [html`
  162. <ul>
  163. <li key="1">foo<input></li>
  164. </ul>
  165. `],
  166. ({ get }, reload, window, document) => {
  167. let toHtml = html`
  168. <ul>
  169. <li key="2">bar<input></li>
  170. <li key="3">baz<input></li>
  171. <li key="1">foo<input></li>
  172. </ul>
  173. `
  174. get('input').type('foo')
  175. get('ul').then(([el]) => window.Alpine.morph(el, toHtml))
  176. get('li').should(haveLength(3))
  177. get('li:nth-of-type(1)').should(haveText('bar'))
  178. get('li:nth-of-type(2)').should(haveText('baz'))
  179. get('li:nth-of-type(3)').should(haveText('foo'))
  180. get('li:nth-of-type(1) input').should(haveValue(''))
  181. get('li:nth-of-type(2) input').should(haveValue(''))
  182. get('li:nth-of-type(3) input').should(haveValue('foo'))
  183. },
  184. )
  185. test('can morph using a custom key function',
  186. [html`
  187. <ul>
  188. <li data-key="1">foo<input></li>
  189. </ul>
  190. `],
  191. ({ get }, reload, window, document) => {
  192. let toHtml = html`
  193. <ul>
  194. <li data-key="2">bar<input></li>
  195. <li data-key="3">baz<input></li>
  196. <li data-key="1">foo<input></li>
  197. </ul>
  198. `
  199. get('input').type('foo')
  200. get('ul').then(([el]) => window.Alpine.morph(el, toHtml, {key(el) {return el.dataset.key}}))
  201. get('li').should(haveLength(3))
  202. get('li:nth-of-type(1)').should(haveText('bar'))
  203. get('li:nth-of-type(2)').should(haveText('baz'))
  204. get('li:nth-of-type(3)').should(haveText('foo'))
  205. get('li:nth-of-type(1) input').should(haveValue(''))
  206. get('li:nth-of-type(2) input').should(haveValue(''))
  207. get('li:nth-of-type(3) input').should(haveValue('foo'))
  208. },
  209. )
  210. test('can morph text nodes',
  211. [html`<h2>Foo <br> Bar</h2>`],
  212. ({ get }, reload, window, document) => {
  213. let toHtml = html`<h2>Foo <br> Baz</h2>`
  214. get('h2').then(([el]) => window.Alpine.morph(el, toHtml))
  215. get('h2').should(haveHtml('Foo <br> Baz'))
  216. },
  217. )
  218. test('can morph with added element before and siblings are different',
  219. [html`
  220. <button>
  221. <div>
  222. <div>second</div>
  223. <div data="false">third</div>
  224. </div>
  225. </button>
  226. `],
  227. ({ get }, reload, window, document) => {
  228. let toHtml = html`
  229. <button>
  230. <div>first</div>
  231. <div>
  232. <div>second</div>
  233. <div data="true">third</div>
  234. </div>
  235. </button>
  236. `
  237. get('button').then(([el]) => window.Alpine.morph(el, toHtml))
  238. get('button > div').should(haveLength(2))
  239. get('button > div:nth-of-type(1)').should(haveText('first'))
  240. get('button > div:nth-of-type(2)').should(haveHtml(`
  241. <div>second</div>
  242. <div data="true">third</div>
  243. `))
  244. },
  245. )
  246. test('can morph using different keys',
  247. [html`
  248. <ul>
  249. <li key="1">foo</li>
  250. </ul>
  251. `],
  252. ({ get }, reload, window, document) => {
  253. let toHtml = html`
  254. <ul>
  255. <li key="2">bar</li>
  256. </ul>
  257. `
  258. get('ul').then(([el]) => window.Alpine.morph(el, toHtml))
  259. get('li').should(haveLength(1))
  260. get('li:nth-of-type(1)').should(haveText('bar'))
  261. get('li:nth-of-type(1)').should(haveAttribute('key', '2'))
  262. },
  263. )
  264. test('can morph different inline nodes',
  265. [html`
  266. <div id="from">
  267. Hello <span>World</span>
  268. </div>
  269. `],
  270. ({ get }, reload, window, document) => {
  271. let toHtml = html`
  272. <div id="to">
  273. Welcome <b>Person</b>!
  274. </div>
  275. `
  276. get('div').then(([el]) => window.Alpine.morph(el, toHtml))
  277. get('div').should(haveHtml('\n Welcome <b>Person</b>!\n '))
  278. },
  279. )
  280. test('can morph multiple nodes',
  281. [html`
  282. <div x-data>
  283. <p></p>
  284. <p></p>
  285. </div>
  286. `],
  287. ({ get }, reload, window, document) => {
  288. let paragraphs = document.querySelectorAll('p')
  289. window.Alpine.morph(paragraphs[0], '<p>1</p')
  290. window.Alpine.morph(paragraphs[1], '<p>2</p')
  291. get('p:nth-of-type(1)').should(haveText('1'))
  292. get('p:nth-of-type(2)').should(haveText('2'))
  293. },
  294. )