use-expand-collapse.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import PropTypes from 'prop-types'
  2. import { expect } from 'chai'
  3. import { render } from '@testing-library/react'
  4. import useExpandCollapse from '../../../../frontend/js/shared/hooks/use-expand-collapse'
  5. const sampleContent = (
  6. <div>
  7. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  8. <p>Maecenas maximus ultrices sollicitudin.</p>
  9. <p>Praesent mollis arcu eget molestie viverra.</p>
  10. <p>Pellentesque eget molestie nisl, non hendrerit lectus.</p>
  11. </div>
  12. )
  13. const originalScrollHeight = Object.getOwnPropertyDescriptor(
  14. HTMLElement.prototype,
  15. 'offsetHeight'
  16. )
  17. const originalScrollWidth = Object.getOwnPropertyDescriptor(
  18. HTMLElement.prototype,
  19. 'offsetWidth'
  20. )
  21. function ExpandCollapseTestUI({ expandCollapseArgs }) {
  22. const { expandableProps } = useExpandCollapse(expandCollapseArgs)
  23. return (
  24. <>
  25. <div {...expandableProps}>{sampleContent}</div>
  26. </>
  27. )
  28. }
  29. ExpandCollapseTestUI.propTypes = {
  30. expandCollapseArgs: PropTypes.object,
  31. }
  32. describe('useExpandCollapse', function () {
  33. // JSDom doesn't compute layout/sizing, so we need to simulate sizing for the elements
  34. // Here we are simulating that the content is bigger than the `collapsedSize`, so
  35. // the expand-collapse widget is used
  36. beforeEach(function () {
  37. Object.defineProperty(HTMLElement.prototype, 'scrollHeight', {
  38. configurable: true,
  39. value: 500,
  40. })
  41. Object.defineProperty(HTMLElement.prototype, 'scrollWidth', {
  42. configurable: true,
  43. value: 500,
  44. })
  45. })
  46. afterEach(function () {
  47. Object.defineProperty(
  48. HTMLElement.prototype,
  49. 'scrollHeight',
  50. originalScrollHeight
  51. )
  52. Object.defineProperty(
  53. HTMLElement.prototype,
  54. 'scrollWidth',
  55. originalScrollWidth
  56. )
  57. })
  58. describe('custom CSS classes', function () {
  59. it('supports a custom CSS class', function () {
  60. const testArgs = {
  61. classes: {
  62. container: 'my-custom-class',
  63. },
  64. }
  65. const { container } = render(
  66. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  67. )
  68. const elWithCustomCSSClass = container.querySelector('div')
  69. expect(elWithCustomCSSClass).to.exist
  70. })
  71. it('supports an extra custom CSS class for the collapsed state', function () {
  72. const testArgs = {
  73. classes: {
  74. containerCollapsed: 'my-custom-collapsed-class',
  75. },
  76. }
  77. const { container } = render(
  78. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  79. )
  80. const elWithCustomCollapsedCSSClass = container.querySelector(
  81. '.my-custom-collapsed-class'
  82. )
  83. expect(elWithCustomCollapsedCSSClass).to.exist
  84. })
  85. it('ignores the collapsed CSS class when expanded', function () {
  86. const testArgs = {
  87. initiallyExpanded: true,
  88. classes: {
  89. containerCollapsed: 'my-custom-collapsed-class',
  90. },
  91. }
  92. const { container } = render(
  93. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  94. )
  95. const elWithCustomCollapsedCSSClass = container.querySelector(
  96. '.my-custom-collapsed-class'
  97. )
  98. expect(elWithCustomCollapsedCSSClass).to.not.exist
  99. })
  100. })
  101. describe('height and width support via dimension argument', function () {
  102. it('defaults to height', function () {
  103. const { container } = render(<ExpandCollapseTestUI />)
  104. const expandCollapseEl = container.firstChild
  105. expect(expandCollapseEl.style.height).to.not.be.empty
  106. expect(expandCollapseEl.style.width).to.be.empty
  107. })
  108. it('supports width', function () {
  109. const testArgs = {
  110. dimension: 'width',
  111. }
  112. const { container } = render(
  113. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  114. )
  115. const expandCollapseEl = container.firstChild
  116. expect(expandCollapseEl.style.height).to.be.empty
  117. expect(expandCollapseEl.style.width).to.not.be.empty
  118. })
  119. })
  120. describe('collapsed size support via collapsedSize argument', function () {
  121. it('defaults to 0px', function () {
  122. const { container } = render(<ExpandCollapseTestUI />)
  123. const expandCollapseEl = container.firstChild
  124. expect(expandCollapseEl.style.height).to.equal('0px')
  125. })
  126. it('supports a custom collapsed size', function () {
  127. const testArgs = {
  128. collapsedSize: 200,
  129. }
  130. const { container } = render(
  131. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  132. )
  133. const expandCollapseEl = container.firstChild
  134. expect(expandCollapseEl.style.height).to.equal('200px')
  135. })
  136. it('supports a custom collapsed size for width', function () {
  137. const testArgs = {
  138. collapsedSize: 200,
  139. dimension: 'width',
  140. }
  141. const { container } = render(
  142. <ExpandCollapseTestUI expandCollapseArgs={testArgs} />
  143. )
  144. const expandCollapseEl = container.firstChild
  145. expect(expandCollapseEl.style.height).to.be.empty
  146. expect(expandCollapseEl.style.width).to.equal('200px')
  147. })
  148. })
  149. })