x-teleport.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { exist, haveText, html, notExist, test } from '../../utils'
  2. test('can use a x-teleport',
  3. [html`
  4. <div x-data="{ count: 1 }" id="a">
  5. <button @click="count++">Inc</button>
  6. <template x-teleport="#b">
  7. <span x-text="count"></span>
  8. </template>
  9. </div>
  10. <div id="b"></div>
  11. `],
  12. ({ get }) => {
  13. get('#b span').should(haveText('1'))
  14. get('button').click()
  15. get('#b span').should(haveText('2'))
  16. },
  17. )
  18. test('can use a x-teleport.append',
  19. [html`
  20. <div x-data="{ count: 1 }" id="a">
  21. <button @click="count++">Inc</button>
  22. <template x-teleport.append="#b">
  23. <span x-text="count"></span>
  24. </template>
  25. </div>
  26. <div id="b"></div>
  27. `],
  28. ({ get }) => {
  29. get('#b + span').should(haveText('1'))
  30. get('button').click()
  31. get('#b + span').should(haveText('2'))
  32. },
  33. )
  34. test('can use a x-teleport.prepend',
  35. [html`
  36. <div x-data="{ count: 1 }" id="a">
  37. <button @click="count++">Inc</button>
  38. <template x-teleport.prepend="#b">
  39. <span x-text="count"></span>
  40. </template>
  41. </div>
  42. <div id="b"></div>
  43. `],
  44. ({ get }) => {
  45. get('#a + span').should(haveText('1'))
  46. get('button').click()
  47. get('#a + span').should(haveText('2'))
  48. },
  49. )
  50. test('can teleport multiple',
  51. [html`
  52. <div x-data="{ count: 1 }" id="a">
  53. <button @click="count++">Inc</button>
  54. <template x-teleport="#b">
  55. <h1 x-text="count"></h1>
  56. </template>
  57. <template x-teleport="#b">
  58. <h2 x-text="count + 1"></h2>
  59. </template>
  60. </div>
  61. <div id="b"></div>
  62. `],
  63. ({ get }) => {
  64. get('#b h1').should(haveText('1'))
  65. get('#b h2').should(haveText('2'))
  66. get('button').click()
  67. get('#b h1').should(haveText('2'))
  68. get('#b h2').should(haveText('3'))
  69. },
  70. )
  71. test('teleported targets forward events to teleport source if listeners are attached',
  72. [html`
  73. <div x-data="{ count: 1 }" id="a">
  74. <button @click="count++">Inc</button>
  75. <template x-teleport="#b" @click="count++">
  76. <h1 x-text="count"></h1>
  77. </template>
  78. </div>
  79. <div id="b"></div>
  80. `],
  81. ({ get }) => {
  82. get('#b h1').should(haveText('1'))
  83. get('button').click()
  84. get('#b h1').should(haveText('2'))
  85. get('h1').click()
  86. get('#b h1').should(haveText('3'))
  87. },
  88. )
  89. test('removing teleport source removes teleported target',
  90. [html`
  91. <div x-data="{ count: 1 }" id="a">
  92. <button @click="$refs.template.remove()">Remove</button>
  93. <template x-teleport="#b" @click="count++" x-ref="template">
  94. <h1 x-text="count"></h1>
  95. </template>
  96. </div>
  97. <div id="b"></div>
  98. `],
  99. ({ get }) => {
  100. get('#b h1').should(exist())
  101. get('button').click()
  102. get('#b h1').should(notExist())
  103. },
  104. )
  105. test('$refs inside teleport can be accessed outside',
  106. [html`
  107. <div x-data="{ count: 1 }" id="a">
  108. <button @click="$refs.count.remove()">Remove</button>
  109. <template x-teleport="#b">
  110. <h1 x-text="count" x-ref="count"></h1>
  111. </template>
  112. </div>
  113. <div id="b"></div>
  114. `],
  115. ({ get }) => {
  116. get('#b h1').should(exist())
  117. get('button').click()
  118. get('#b h1').should(notExist())
  119. },
  120. )
  121. test('$root is accessed outside teleport',
  122. [html`
  123. <div x-data="{ count: 1 }" id="a">
  124. <template x-teleport="#b">
  125. <h1 x-text="$root.id"></h1>
  126. </template>
  127. </div>
  128. <div id="b"></div>
  129. `],
  130. ({ get }) => {
  131. get('#b h1').should(exist())
  132. get('#b h1').should(haveText('a'))
  133. },
  134. )
  135. test('$id honors x-id outside teleport',
  136. [html`
  137. <div x-data="{ count: 1 }" id="a" x-id="['foo']">
  138. <h1 x-text="$id('foo')"></h1>
  139. <template x-teleport="#b">
  140. <h1 x-text="$id('foo')"></h1>
  141. </template>
  142. </div>
  143. <div id="b"></div>
  144. `],
  145. ({ get }) => {
  146. get('#b h1').should(haveText('foo-1'))
  147. },
  148. )