|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import { useRef, RefObject } from 'react'; |
| 3 | +import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock'; |
| 4 | + |
| 5 | +import { useContentBoxSize } from './useContentBoxSize'; |
| 6 | + |
| 7 | +withResizeObserverMock(); |
| 8 | + |
| 9 | +beforeAll(() => { |
| 10 | + jest.useFakeTimers(); |
| 11 | +}); |
| 12 | + |
| 13 | +let element: HTMLElement; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + element = document.createElement('div'); |
| 17 | + element.style.width = '40px'; |
| 18 | + element.style.height = '30px'; |
| 19 | + element.style.padding = '5px'; |
| 20 | + document.body.append(element); |
| 21 | +}); |
| 22 | + |
| 23 | +afterEach(() => { |
| 24 | + element.remove(); |
| 25 | +}); |
| 26 | + |
| 27 | +const wrapRef = (ref: RefObject<HTMLElement>) => { |
| 28 | + Object.assign(ref, { current: element }); |
| 29 | + return ref; |
| 30 | +}; |
| 31 | + |
| 32 | +it('immediately returns size', async () => { |
| 33 | + const { result } = renderHook(() => useContentBoxSize(wrapRef(useRef()))); |
| 34 | + |
| 35 | + expect(result.current.inlineSize).toStrictEqual(40); |
| 36 | + expect(result.current.blockSize).toStrictEqual(30); |
| 37 | +}); |
| 38 | + |
| 39 | +it('gets the observed element size after resize', async () => { |
| 40 | + const { result } = renderHook(() => useContentBoxSize(wrapRef(useRef()))); |
| 41 | + |
| 42 | + // triggers MutationObserver |
| 43 | + await act(async () => { |
| 44 | + element.style.width = '30px'; |
| 45 | + element.style.height = '40px'; |
| 46 | + element.style.padding = '15px'; |
| 47 | + }); |
| 48 | + |
| 49 | + // waits for debounced state mutation |
| 50 | + await act(async () => { |
| 51 | + jest.advanceTimersByTime(0); |
| 52 | + }); |
| 53 | + |
| 54 | + expect(result.current.inlineSize).toStrictEqual(30); |
| 55 | + expect(result.current.blockSize).toStrictEqual(40); |
| 56 | + |
| 57 | + // triggers MutationObserver |
| 58 | + await act(async () => { |
| 59 | + element.style.boxSizing = 'border-box'; |
| 60 | + }); |
| 61 | + |
| 62 | + // waits for debounced state mutation |
| 63 | + await act(async () => { |
| 64 | + jest.advanceTimersByTime(0); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(result.current.inlineSize).toStrictEqual(0); |
| 68 | + expect(result.current.blockSize).toStrictEqual(10); |
| 69 | +}); |
| 70 | + |
| 71 | +it('debounces the observed element size', async () => { |
| 72 | + const halfDelay = 50; |
| 73 | + const delay = 2 * halfDelay; |
| 74 | + |
| 75 | + const { result } = renderHook(() => |
| 76 | + useContentBoxSize(wrapRef(useRef()), { debounceDelay: delay }) |
| 77 | + ); |
| 78 | + |
| 79 | + // triggers MutationObserver |
| 80 | + await act(async () => { |
| 81 | + element.style.width = '30px'; |
| 82 | + element.style.height = '40px'; |
| 83 | + element.style.padding = '15px'; |
| 84 | + }); |
| 85 | + |
| 86 | + // waits for debounced state mutation |
| 87 | + await act(async () => { |
| 88 | + jest.advanceTimersByTime(halfDelay); |
| 89 | + }); |
| 90 | + |
| 91 | + expect(result.current.inlineSize).toStrictEqual(40); |
| 92 | + expect(result.current.blockSize).toStrictEqual(30); |
| 93 | + |
| 94 | + // wait the callback trigger from ResizeObserver |
| 95 | + await act(async () => { |
| 96 | + jest.advanceTimersByTime(halfDelay); |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result.current.inlineSize).toStrictEqual(30); |
| 100 | + expect(result.current.blockSize).toStrictEqual(40); |
| 101 | +}); |
0 commit comments