Skip to content

Commit

Permalink
useContentRect tests (deephaven#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 11, 2024
1 parent f2be02b commit c7ecd14
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
15 changes: 15 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ Object.defineProperty(window, 'ResizeObserver', {
},
});

Object.defineProperty(window, 'DOMRect', {
value: function (x: number = 0, y: number = 0, width = 0, height = 0) {
return TestUtils.createMockProxy<DOMRect>({
x,
y,
width,
height,
top: y,
bottom: y + height,
left: x,
right: x + width,
});
},
});

Object.defineProperty(window, 'TextDecoder', {
value: TextDecoder,
});
Expand Down
70 changes: 70 additions & 0 deletions packages/react-hooks/src/useContentRect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { TestUtils } from '@deephaven/utils';
import { useContentRect } from './useContentRect';
import useResizeObserver from './useResizeObserver';

jest.mock('./useResizeObserver');

const { asMock, createMockProxy } = TestUtils;

beforeEach(() => {
jest.clearAllMocks();
expect.hasAssertions();
asMock(useResizeObserver).mockName('useResizeObserver');
});

describe.each([true, false])('useContentRect - explicitMap:%s', explicitMap => {
const mock = {
refValue: document.createElement('div'),
mappedValue: document.createElement('span'),
resizeEntry: createMockProxy<ResizeObserverEntry>({
contentRect: new DOMRect(0, 0, 100, 100),
}),
observer: createMockProxy<ResizeObserver>(),
};

const mockMap = explicitMap ? jest.fn(() => mock.mappedValue) : undefined;

it('should initially return zero size contentRect', () => {
const { result } = renderHook(() => useContentRect(mockMap));
expect(useResizeObserver).toHaveBeenCalledWith(null, expect.any(Function));
expect(result.current.contentRect).toEqual(new DOMRect());
});

it('should pass expected value to resize observer based on presence of map function', () => {
const { result, rerender } = renderHook(() => useContentRect(mockMap));

result.current.ref(mock.refValue);
rerender();

if (mockMap != null) {
expect(mockMap).toHaveBeenCalledWith(mock.refValue);
}
expect(useResizeObserver).toHaveBeenCalledWith(
mockMap == null ? mock.refValue : mock.mappedValue,
expect.any(Function)
);
expect(result.current.contentRect).toEqual(new DOMRect());
});

it.each([
[[], new DOMRect()],
[[mock.resizeEntry], mock.resizeEntry.contentRect],
])(
'should update contentRect when resize observer triggers: %s',
(entries, expected) => {
const { result, rerender } = renderHook(() => useContentRect(mockMap));

result.current.ref(mock.refValue);
rerender();

const handleResize = asMock(useResizeObserver).mock.calls.at(-1)?.[1];

act(() => {
handleResize?.(entries, mock.observer);
});

expect(result.current.contentRect).toEqual(expected);
}
);
});

0 comments on commit c7ecd14

Please sign in to comment.