1
0

clone.spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { haveText, html, test } from '../utils'
  2. test('can clone a component',
  3. html`
  4. <script>
  5. document.addEventListener('alpine:initialized', () => {
  6. window.original = document.getElementById('original')
  7. window.copy = document.getElementById('copy')
  8. window.copy.removeAttribute('x-ignore')
  9. delete window.copy._x_ignore
  10. })
  11. </script>
  12. <button x-data @click="Alpine.clone(original, copy)">click</button>
  13. <div x-data="{ foo: 'bar' }" id="original">
  14. <h1 @click="foo = 'baz'">click me</h1>
  15. <span x-text="foo"></span>
  16. </div>
  17. <div x-data="{ foo: 'bar' }" id="copy" x-ignore>
  18. <h1 @click="foo = 'baz'">click me</h1>
  19. <span x-text="foo"></span>
  20. </div>
  21. `,
  22. ({ get }) => {
  23. get('#original h1').click()
  24. get('#original span').should(haveText('baz'))
  25. get('#copy span').should(haveText(''))
  26. get('button').click()
  27. get('#copy span').should(haveText('baz'))
  28. }
  29. )
  30. test('wont run init on clone',
  31. html`
  32. <script>
  33. document.addEventListener('alpine:initialized', () => {
  34. window.original = document.getElementById('original')
  35. window.copy = document.getElementById('copy')
  36. window.copy.removeAttribute('x-ignore')
  37. delete window.copy._x_ignore
  38. })
  39. </script>
  40. <button x-data @click="Alpine.clone(original, copy)">click</button>
  41. <div x-data="{ count: 0 }" x-init="count++" id="original">
  42. <span x-text="count"></span>
  43. </div>
  44. <div x-data="{ count: 0 }" x-init="count++" id="copy" x-ignore>
  45. <span x-text="count"></span>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('#original span').should(haveText('1'))
  50. get('#copy span').should(haveText(''))
  51. get('button').click()
  52. get('#copy span').should(haveText('1'))
  53. }
  54. )
  55. test('wont register listeners on clone',
  56. html`
  57. <script>
  58. document.addEventListener('alpine:initialized', () => {
  59. window.original = document.getElementById('original')
  60. window.copy = document.getElementById('copy')
  61. window.copy.removeAttribute('x-ignore')
  62. delete window.copy._x_ignore
  63. })
  64. </script>
  65. <button x-data @click="Alpine.clone(original, copy)">click</button>
  66. <div x-data="{ count: 0 }" x-init="count++" id="original">
  67. <span x-text="count"></span>
  68. </div>
  69. <div x-data="{ count: 0 }" x-init="count++" id="copy" x-ignore>
  70. <h1 @click="count++">inc</h1>
  71. <span x-text="count"></span>
  72. </div>
  73. `,
  74. ({ get }) => {
  75. get('#original span').should(haveText('1'))
  76. get('#copy span').should(haveText(''))
  77. get('button').click()
  78. get('#copy span').should(haveText('1'))
  79. get('#copy h1').click()
  80. get('#copy span').should(haveText('1'))
  81. }
  82. )