Fix tests

This commit is contained in:
Justin 2022-09-07 10:30:11 -04:00
parent 07cbd02737
commit 9aebaa96a8
2 changed files with 32 additions and 25 deletions

View file

@ -0,0 +1,25 @@
let listener: ((rect: any) => void) | undefined = undefined;
const mockDisconnect = jest.fn();
class ResizeObserver {
constructor(ls: any) {
listener = ls;
}
observe() {
// do nothing
}
unobserve() {
// do nothing
}
disconnect() {
mockDisconnect();
}
}
// eslint-disable-next-line compat/compat
(window as any).ResizeObserver = ResizeObserver;
export { ResizeObserver as default, listener, mockDisconnect };

View file

@ -1,21 +1,13 @@
import { renderHook, act } from '@testing-library/react-hooks'; import { renderHook, act } from '@testing-library/react-hooks';
import { listener, mockDisconnect } from '../__mocks__/resize-observer';
import { useDimensions } from '../useDimensions'; import { useDimensions } from '../useDimensions';
let listener: ((rect: any) => void) | undefined = undefined;
(window as any).ResizeObserver = class ResizeObserver {
constructor(ls: any) {
listener = ls;
}
observe() {}
disconnect() {}
};
describe('useDimensions()', () => { describe('useDimensions()', () => {
beforeEach(() => {
mockDisconnect.mockClear();
});
it('defaults to 0', () => { it('defaults to 0', () => {
const { result } = renderHook(() => useDimensions()); const { result } = renderHook(() => useDimensions());
@ -56,16 +48,6 @@ describe('useDimensions()', () => {
}); });
it('disconnects on unmount', () => { it('disconnects on unmount', () => {
const disconnect = jest.fn();
(window as any).ResizeObserver = class ResizeObserver {
observe() {}
disconnect() {
disconnect();
}
};
const { result, unmount } = renderHook(() => useDimensions()); const { result, unmount } = renderHook(() => useDimensions());
act(() => { act(() => {
@ -73,8 +55,8 @@ describe('useDimensions()', () => {
(result.current[1] as any)(div); (result.current[1] as any)(div);
}); });
expect(disconnect).toHaveBeenCalledTimes(0); expect(mockDisconnect).toHaveBeenCalledTimes(0);
unmount(); unmount();
expect(disconnect).toHaveBeenCalledTimes(1); expect(mockDisconnect).toHaveBeenCalledTimes(1);
}); });
}); });