use-expand-collapse.test.js 4.9 KB

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