use-resize.spec.tsx 2.8 KB

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