1
0

x-teleport.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { beEqualTo, beVisible, haveText, html, notBeVisible, 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 teleport multiple',
  19. [html`
  20. <div x-data="{ count: 1 }" id="a">
  21. <button @click="count++">Inc</button>
  22. <template x-teleport="#b">
  23. <h1 x-text="count"></h1>
  24. </template>
  25. <template x-teleport="#b">
  26. <h2 x-text="count + 1"></h2>
  27. </template>
  28. </div>
  29. <div id="b"></div>
  30. `],
  31. ({ get }) => {
  32. get('#b h1').should(haveText('1'))
  33. get('#b h2').should(haveText('2'))
  34. get('button').click()
  35. get('#b h1').should(haveText('2'))
  36. get('#b h2').should(haveText('3'))
  37. },
  38. )
  39. test('teleported targets forward events to teleport source if listeners are attached',
  40. [html`
  41. <div x-data="{ count: 1 }" id="a">
  42. <button @click="count++">Inc</button>
  43. <template x-teleport="#b" @click="count++">
  44. <h1 x-text="count"></h1>
  45. </template>
  46. </div>
  47. <div id="b"></div>
  48. `],
  49. ({ get }) => {
  50. get('#b h1').should(haveText('1'))
  51. get('button').click()
  52. get('#b h1').should(haveText('2'))
  53. get('h1').click()
  54. get('#b h1').should(haveText('3'))
  55. },
  56. )
  57. test('removing teleport source removes teleported target',
  58. [html`
  59. <div x-data="{ count: 1 }" id="a">
  60. <button @click="$refs.template.remove()">Remove</button>
  61. <template x-teleport="#b" @click="count++" x-ref="template">
  62. <h1 x-text="count"></h1>
  63. </template>
  64. </div>
  65. <div id="b"></div>
  66. `],
  67. ({ get }) => {
  68. get('#b h1').should(beVisible())
  69. get('button').click()
  70. get('#b h1').should(notBeVisible())
  71. },
  72. )
  73. test('$refs inside teleport can be accessed outside',
  74. [html`
  75. <div x-data="{ count: 1 }" id="a">
  76. <button @click="$refs.count.remove()">Remove</button>
  77. <template x-teleport="#b">
  78. <h1 x-text="count" x-ref="count"></h1>
  79. </template>
  80. </div>
  81. <div id="b"></div>
  82. `],
  83. ({ get }) => {
  84. get('#b h1').should(beVisible())
  85. get('button').click()
  86. get('#b h1').should(notBeVisible())
  87. },
  88. )
  89. test('$root is accessed outside teleport',
  90. [html`
  91. <div x-data="{ count: 1 }" id="a">
  92. <template x-teleport="#b">
  93. <h1 x-text="$root.id"></h1>
  94. </template>
  95. </div>
  96. <div id="b"></div>
  97. `],
  98. ({ get }) => {
  99. get('#b h1').should(beVisible())
  100. get('#b h1').should(haveText('a'))
  101. },
  102. )
  103. test('$id honors x-id outside teleport',
  104. [html`
  105. <div x-data="{ count: 1 }" id="a" x-id="['foo']">
  106. <h1 x-text="$id('foo')"></h1>
  107. <template x-teleport="#b">
  108. <h1 x-text="$id('foo')"></h1>
  109. </template>
  110. </div>
  111. <div id="b"></div>
  112. `],
  113. ({ get }) => {
  114. get('#b h1').should(haveText('foo-1'))
  115. },
  116. )