collapse.spec.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { haveAttribute, haveComputedStyle, html, notHaveAttribute, test } from '../../utils'
  2. test('can collapse and expand element',
  3. [html`
  4. <div x-data="{ expanded: false }">
  5. <button @click="expanded = ! expanded">toggle</button>
  6. <h1 x-show="expanded" x-collapse>contents <a href="#">focusable content</a></h1>
  7. </div>
  8. `],
  9. ({ get }, reload) => {
  10. get('h1').should(haveComputedStyle('height', '0px'))
  11. get('h1').should(haveAttribute('style', 'display: none; height: 0px; overflow: hidden;'))
  12. get('h1').should(haveAttribute('hidden', 'hidden'))
  13. get('button').click()
  14. get('h1').should(haveAttribute('style', 'height: auto;'))
  15. get('h1').should(notHaveAttribute('hidden'))
  16. get('button').click()
  17. get('h1').should(haveComputedStyle('height', '0px'))
  18. get('h1').should(haveAttribute('style', 'height: 0px; overflow: hidden; display: none;'))
  19. get('h1').should(haveAttribute('hidden', 'hidden'))
  20. },
  21. )
  22. test('can collapse and expand with a minimum height instead of "display: none"',
  23. [html`
  24. <div x-data="{ expanded: false }">
  25. <button @click="expanded = ! expanded">toggle</button>
  26. <h1 x-show="expanded" x-collapse.min.25px>contents <a href="#">focusable content</a></h1>
  27. </div>
  28. `],
  29. ({ get }) => {
  30. get('h1').should(haveComputedStyle('height', '25px'))
  31. get('h1').should(haveAttribute('style', 'height: 25px; overflow: hidden;'))
  32. get('h1').should(notHaveAttribute('hidden', 'hidden'))
  33. get('button').click()
  34. get('h1').should(haveAttribute('style', 'height: auto;'))
  35. get('button').click()
  36. get('h1').should(haveComputedStyle('height', '25px'))
  37. get('h1').should(haveAttribute('style', 'height: 25px; overflow: hidden;'))
  38. },
  39. )
  40. test('@click.away with x-collapse (prevent race condition)',
  41. html`
  42. <div x-data="{ show: false }">
  43. <button @click="show = true">Show</button>
  44. <h1 x-show="show" @click.away="show = false" x-collapse>h1</h1>
  45. </div>
  46. `,
  47. ({ get }) => {
  48. get('h1').should(haveComputedStyle('height', '0px'))
  49. get('button').click()
  50. get('h1').should(haveAttribute('style', 'height: auto;'))
  51. }
  52. )
  53. test('@click.away with x-collapse and borders (prevent race condition)',
  54. html`
  55. <div x-data="{ show: false }">
  56. <button @click="show = true">Show</button>
  57. <h1 style="border: 1x solid" x-show="show" @click.away="show = false" x-collapse>h1</h1>
  58. </div>
  59. `,
  60. ({ get }) => {
  61. get('h1').should(haveComputedStyle('height', '0px'))
  62. get('button').click()
  63. get('h1').should(haveAttribute('style', 'height: auto;'))
  64. }
  65. )
  66. // https://github.com/alpinejs/alpine/issues/2335
  67. test('double-click on x-collapse does not mix styles up',
  68. [html`
  69. <div x-data="{ expanded: false }">
  70. <button @click="expanded = ! expanded">toggle</button>
  71. <h1 x-show="expanded" x-collapse>contents</h1>
  72. </div>
  73. `],
  74. ({ get }, reload) => {
  75. get('h1').should(haveComputedStyle('height', '0px'))
  76. get('h1').should(haveAttribute('style', 'display: none; height: 0px; overflow: hidden;'))
  77. get('button').click()
  78. get('button').click()
  79. get('h1').should(haveAttribute('style', 'height: 0px; overflow: hidden; display: none;'))
  80. get('button').click()
  81. get('h1').should(haveAttribute('style', 'height: auto;'))
  82. get('button').click()
  83. get('button').click()
  84. get('h1').should(haveAttribute('style', 'height: auto;'))
  85. },
  86. )