portal.spec.js 3.8 KB

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