1
0

x-show.spec.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { beHidden, beVisible, haveAttribute, html, test } from '../../utils'
  2. test('x-show toggles display: none; with no other style attributes',
  3. html`
  4. <div x-data="{ show: true }">
  5. <span x-show="show">thing</span>
  6. <button x-on:click="show = false"></button>
  7. </div>
  8. `,
  9. ({ get }) => {
  10. get('span').should(beVisible())
  11. get('button').click()
  12. get('span').should(beHidden())
  13. }
  14. )
  15. test('x-show (with true default) toggles display: none; even if it exists with the page load',
  16. html`
  17. <div x-data="{ show: true }">
  18. <span x-show="show" style="display: none;">thing</span>
  19. <button x-on:click="show = false"></button>
  20. </div>
  21. `,
  22. ({ get }) => {
  23. get('span').should(beVisible())
  24. get('button').click()
  25. get('span').should(beHidden())
  26. }
  27. )
  28. test('x-show (with false default) toggles display: none; even if it exists with the page load',
  29. html`
  30. <div x-data="{ show: false }">
  31. <span x-show="show" style="display: none;">thing</span>
  32. <button x-on:click="show = true"></button>
  33. </div>
  34. `,
  35. ({ get }) => {
  36. get('span').should(beHidden())
  37. get('button').click()
  38. get('span').should(beVisible())
  39. }
  40. )
  41. test('x-show toggles display: none; with other style attributes',
  42. html`
  43. <div x-data="{ show: true }">
  44. <span x-show="show" style="color: blue;">thing</span>
  45. <button x-on:click="show = false"></button>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('span').should(beVisible())
  50. get('span').should(haveAttribute('style', 'color: blue;'))
  51. get('button').click()
  52. get('span').should(beHidden())
  53. get('span').should(haveAttribute('style', 'color: blue; display: none;'))
  54. }
  55. )
  56. test('x-show waits for transitions within it to finish before hiding an elements',
  57. html`
  58. <style>
  59. .transition { transition-property: background-color, border-color, color, fill, stroke; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); transition-duration: 150ms; }
  60. .duration-100 { transition-duration: 100ms; }
  61. </style>
  62. <div x-data="{ show: true }">
  63. <span x-show="show">
  64. <h1 x-show="show" x-transition:leave="transition duration-100">thing</h1>
  65. </span>
  66. <button x-on:click="show = false"></button>
  67. </div>
  68. `,
  69. ({ get }) => {
  70. get('span').should(beVisible())
  71. get('button').click()
  72. get('span').should(beVisible())
  73. get('h1').should(beHidden())
  74. get('span').should(beHidden())
  75. }
  76. )
  77. test('x-show does NOT wait for transitions to finish if .immediate is present',
  78. html`
  79. <style>
  80. .transition { transition-property: background-color, border-color, color, fill, stroke; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); transition-duration: 150ms; }
  81. .duration-100 { transition-duration: 100ms; }
  82. </style>
  83. <div x-data="{ show: true }">
  84. <span x-show.immediate="show">
  85. <h1 x-show="show" x-transition:leave="transition duration-100">thing</h1>
  86. </span>
  87. <button x-on:click="show = false"></button>
  88. </div>
  89. `,
  90. ({ get }) => {
  91. get('span').should(beVisible())
  92. get('button').click()
  93. get('span').should(beHidden())
  94. }
  95. )
  96. test('x-show with x-bind:style inside x-for works correctly',
  97. html`
  98. <div x-data="{items: [{ cleared: false }, { cleared: false }]}">
  99. <template x-for="(item, index) in items" :key="index">
  100. <button x-show="! item.cleared"
  101. x-bind:style="'background: #999'"
  102. @click="item.cleared = true"
  103. >
  104. </button>
  105. </template>
  106. </div>
  107. `,
  108. ({ get }) => {
  109. get('button:nth-of-type(1)').should(beVisible())
  110. get('button:nth-of-type(1)').should(haveAttribute('style', 'background: #999'))
  111. get('button:nth-of-type(2)').should(beVisible())
  112. get('button:nth-of-type(2)').should(haveAttribute('style', 'background: #999'))
  113. get('button:nth-of-type(1)').click()
  114. get('button:nth-of-type(1)').should(beHidden())
  115. get('button:nth-of-type(1)').should(haveAttribute('style', 'background: rgb(153, 153, 153); display: none;'))
  116. get('button:nth-of-type(2)').should(beVisible())
  117. get('button:nth-of-type(2)').should(haveAttribute('style', 'background: #999'))
  118. }
  119. )
  120. test('x-show takes precedence over style bindings for display property',
  121. html`
  122. <div x-data="{ show: false }">
  123. <span x-show="show" :style="'color: red;'">thing</span>
  124. <span :style="'color: red;'" x-show="show">thing</span>
  125. </div>
  126. `,
  127. ({ get }) => {
  128. get('span:nth-of-type(1)').should(haveAttribute('style', 'color: red; display: none;'))
  129. get('span:nth-of-type(2)').should(haveAttribute('style', 'color: red; display: none;'))
  130. }
  131. )