-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix(web-react): Use Resize Observer hook with better handling of wind…
…ow object refs #DS-1465
- Loading branch information
Showing
4 changed files
with
32 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 8 additions & 23 deletions
31
packages/web-react/src/components/Collapse/__tests__/useResizeHeight.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,34 @@ | ||
/* eslint-disable jest/no-commented-out-tests */ | ||
/** | ||
* Disabled because we are unable to simulate and subsequently test the correct behavior of the useResizeObserver hook. | ||
* But we do not want to throw away the know-how about testing that hook in code yet. | ||
* | ||
* More information https://jira.lmc.cz/browse/DS-549 | ||
* | ||
* @todo either we make this test work in https://jira.lmc.cz/browse/DS-549 or replaced it with visual test | ||
*/ | ||
|
||
/* | ||
import '@testing-library/jest-dom'; | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { ResizeObserver } from '@juggle/resize-observer'; | ||
import React, { useRef } from 'react'; | ||
import { useResizeHeight } from '../useResizeHeight'; | ||
|
||
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => cb()); | ||
jest.mock('@juggle/resize-observer'); | ||
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(() => { | ||
return 0; | ||
}); | ||
|
||
const TestComponent = () => { | ||
const ref = React.useRef(null); | ||
const ref = useRef(null); | ||
const height = useResizeHeight(ref); | ||
|
||
return <div ref={ref}>{height}</div>; | ||
}; | ||
|
||
const resize = (height: number) => { | ||
ResizeObserver.mockImplementation((cb) => { | ||
global.ResizeObserver = jest.fn().mockImplementation((cb) => { | ||
cb([{ contentRect: { height } }]); | ||
|
||
return { observe: jest.fn, disconnect: jest.fn, unobserve: jest.fn }; | ||
return { observe: jest.fn(), disconnect: jest.fn(), unobserve: jest.fn() }; | ||
}); | ||
|
||
const { container } = render(<TestComponent />); | ||
|
||
return container.textContent; | ||
}; | ||
|
||
describe.skip('useResizeHeight', () => { | ||
describe('useResizeHeight', () => { | ||
it('should return simulated height', () => { | ||
expect(resize(100)).toBe('100px'); | ||
expect(resize(200)).toBe('200px'); | ||
}); | ||
}); | ||
*/ | ||
|
||
// eslint-disable-next-line jest/no-disabled-tests, jest/expect-expect, @typescript-eslint/no-empty-function | ||
it.skip('skip', () => {}); |
14 changes: 10 additions & 4 deletions
14
packages/web-react/src/components/Collapse/useResizeHeight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import useResizeObserver from '@react-hook/resize-observer'; | ||
import { RefObject, useState } from 'react'; | ||
import { useResizeObserver } from 'usehooks-ts'; | ||
|
||
type Size = { | ||
height: number | undefined; | ||
}; | ||
|
||
export const useResizeHeight = (ref: RefObject<HTMLElement>): string => { | ||
const [height, setHeight] = useState<string>('0px'); | ||
|
||
useResizeObserver(ref, (entry: ResizeObserverEntry) => { | ||
const currentHeight = entry.contentRect.height; | ||
const onResize = (size: Size) => { | ||
const currentHeight = size.height; | ||
setHeight(`${currentHeight}px`); | ||
}); | ||
}; | ||
|
||
useResizeObserver({ ref, onResize }); | ||
|
||
return height; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters