clone.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. )
  83. test('wont register extra listeners on x-model on clone',
  84. html`
  85. <script>
  86. document.addEventListener('alpine:initialized', () => {
  87. window.original = document.getElementById('original')
  88. window.copy = document.getElementById('copy')
  89. })
  90. </script>
  91. <button x-data @click="Alpine.clone(original, copy)">click</button>
  92. <div x-data="{ checks: [] }" id="original">
  93. <input type="checkbox" x-model="checks" value="1">
  94. <span x-text="checks"></span>
  95. </div>
  96. <div x-data="{ checks: [] }" id="copy">
  97. <input type="checkbox" x-model="checks" value="1">
  98. <span x-text="checks"></span>
  99. </div>
  100. `,
  101. ({ get }) => {
  102. get('#original span').should(haveText(''))
  103. get('#copy span').should(haveText(''))
  104. get('button').click()
  105. get('#copy span').should(haveText(''))
  106. get('#copy input').click()
  107. get('#copy span').should(haveText('1'))
  108. }
  109. )