use-resize.spec.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import '../../helpers/bootstrap-3'
  2. import {
  3. usePersistedResize,
  4. useResize,
  5. } from '../../../../frontend/js/shared/hooks/use-resize'
  6. function Template({
  7. mousePos,
  8. getTargetProps,
  9. getHandleProps,
  10. }: ReturnType<typeof useResize>) {
  11. return (
  12. <div
  13. style={{
  14. height: '100vh',
  15. display: 'flex',
  16. alignItems: 'center',
  17. }}
  18. >
  19. <div style={{ position: 'relative' }}>
  20. <div
  21. id="target"
  22. {...getTargetProps({
  23. style: {
  24. width: '200px',
  25. height: '200px',
  26. border: '2px solid black',
  27. ...(mousePos?.x && { width: `${mousePos.x}px` }),
  28. },
  29. })}
  30. >
  31. Demo content demo content demo content demo content demo content demo
  32. content
  33. </div>
  34. <div
  35. id="handle"
  36. {...getHandleProps({
  37. style: {
  38. position: 'absolute',
  39. top: 0,
  40. right: 0,
  41. height: '100%',
  42. width: '4px',
  43. backgroundColor: 'red',
  44. },
  45. })}
  46. />
  47. </div>
  48. </div>
  49. )
  50. }
  51. function PersistedResizeTest() {
  52. const props = usePersistedResize({ name: 'test' })
  53. return <Template {...props} />
  54. }
  55. function ResizeTest() {
  56. const props = useResize()
  57. return <Template {...props} />
  58. }
  59. describe('useResize', function () {
  60. it('should apply provided styles to the target', function () {
  61. cy.mount(<ResizeTest />)
  62. // test a css prop being applied
  63. cy.get('#target').should('have.css', 'width', '200px')
  64. })
  65. it('should apply provided styles to the handle', function () {
  66. cy.mount(<ResizeTest />)
  67. // test a css prop being applied
  68. cy.get('#handle').should('have.css', 'width', '4px')
  69. })
  70. it('should apply default styles to the handle', function () {
  71. cy.mount(<ResizeTest />)
  72. cy.get('#handle')
  73. .should('have.css', 'cursor', 'col-resize')
  74. .and('have.css', 'user-select', 'none')
  75. })
  76. it('should resize the target horizontally on mousedown and mousemove', function () {
  77. const xPos = 400
  78. cy.mount(<ResizeTest />)
  79. cy.get('#handle').trigger('mousedown', { button: 0 })
  80. cy.get('#handle').trigger('mousemove', { clientX: xPos })
  81. cy.get('#handle').trigger('mouseup')
  82. cy.get('#target').should('have.css', 'width', `${xPos}px`)
  83. })
  84. it('should persist the resize data', function () {
  85. const xPos = 400
  86. cy.mount(<PersistedResizeTest />)
  87. cy.get('#handle').trigger('mousedown', { button: 0 })
  88. cy.get('#handle').trigger('mousemove', { clientX: xPos })
  89. cy.get('#handle').trigger('mouseup')
  90. cy.window()
  91. .its('localStorage.resizeable-test')
  92. .should('eq', `{"x":${xPos}}`)
  93. // render the component again
  94. cy.mount(<PersistedResizeTest />)
  95. cy.get('#target').should('have.css', 'width', `${xPos}px`)
  96. })
  97. })