morph.spec.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { haveText, html, test } from '../../utils'
  2. test('can morph components',
  3. [html`
  4. <div x-data="{ frame: 0 }">
  5. <template x-ref="0">
  6. <h1><div></div>foo</h1>
  7. </template>
  8. <template x-ref="1">
  9. <h1><div x-data="{ text: 'yo' }" x-text="text"></div> foo</h1>
  10. </template>
  11. <template x-ref="2">
  12. <h1><div x-data="{ text: 'yo' }" x-text="text + 'yo'"></div> foo</h1>
  13. </template>
  14. <button @click="frame++">morph</button>
  15. <article x-morph="$refs[frame % 3].innerHTML"></article>
  16. </div>
  17. `],
  18. ({ get }) => {
  19. get('article h1').should(haveText('foo'))
  20. get('button').click()
  21. get('article h1').should(haveText('yo foo'))
  22. get('button').click()
  23. get('article h1').should(haveText('yoyo foo'))
  24. },
  25. )
  26. test('components within morph retain state between',
  27. [html`
  28. <div x-data="{ frame: 0 }">
  29. <template x-ref="0">
  30. <div x-data="{ count: 1 }">
  31. <button @click="count++">inc</button>
  32. <span x-text="String(frame) + count"></span>
  33. </div>
  34. </template>
  35. <template x-ref="1">
  36. <div x-data="{ count: 1 }">
  37. <button @click="count++">inc</button>
  38. <span x-text="String(frame) + 'foo' + count"></span>
  39. </div>
  40. </template>
  41. <button @click="frame++" id="morph">morph</button>
  42. <article x-morph="$refs[frame % 2].innerHTML"></article>
  43. </div>
  44. `],
  45. ({ get }) => {
  46. get('article span').should(haveText('01'))
  47. get('article button').click()
  48. get('article span').should(haveText('02'))
  49. get('#morph').click()
  50. get('article span').should(haveText('1foo2'))
  51. get('article button').click()
  52. get('article span').should(haveText('1foo3'))
  53. },
  54. )