bigbuffet-rw/app/soapbox/hooks/__tests__/useDimensions.test.ts

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-06-22 06:25:46 -07:00
import { renderHook, act } from '@testing-library/react-hooks';
2022-09-07 07:30:11 -07:00
import { listener, mockDisconnect } from '../__mocks__/resize-observer';
2022-06-22 06:25:46 -07:00
import { useDimensions } from '../useDimensions';
describe('useDimensions()', () => {
2022-09-07 07:30:11 -07:00
beforeEach(() => {
mockDisconnect.mockClear();
});
2022-06-22 06:25:46 -07:00
it('defaults to 0', () => {
const { result } = renderHook(() => useDimensions());
act(() => {
const div = document.createElement('div');
2022-07-18 10:36:56 -07:00
(result.current[1] as any)(div);
2022-06-22 06:25:46 -07:00
});
2022-07-18 10:36:56 -07:00
expect(result.current[2]).toMatchObject({
2022-06-22 06:25:46 -07:00
width: 0,
height: 0,
});
});
it('measures the dimensions of a DOM element', () => {
const { result } = renderHook(() => useDimensions());
act(() => {
const div = document.createElement('div');
2022-07-18 10:36:56 -07:00
(result.current[1] as any)(div);
2022-06-22 06:25:46 -07:00
});
act(() => {
listener!([
{
contentRect: {
width: 200,
height: 200,
},
},
]);
});
2022-07-18 10:36:56 -07:00
expect(result.current[2]).toMatchObject({
2022-06-22 06:25:46 -07:00
width: 200,
height: 200,
});
});
it('disconnects on unmount', () => {
const { result, unmount } = renderHook(() => useDimensions());
act(() => {
const div = document.createElement('div');
2022-07-18 10:36:56 -07:00
(result.current[1] as any)(div);
2022-06-22 06:25:46 -07:00
});
2022-09-07 07:30:11 -07:00
expect(mockDisconnect).toHaveBeenCalledTimes(0);
2022-06-22 06:25:46 -07:00
unmount();
2022-09-07 07:30:11 -07:00
expect(mockDisconnect).toHaveBeenCalledTimes(1);
2022-06-22 06:25:46 -07:00
});
});