|
1 | 1 | import { renderHook, waitFor, render, screen } from '@testing-library/react'; |
2 | 2 | import { useElementSize } from './use-element-size'; |
3 | | -import type { ElementSizeStateStatus, ElementSizeState } from './defs'; |
4 | 3 |
|
5 | 4 | describe('Element size can be detected when: ', () => { |
6 | | - describe('tracks and', () => { |
7 | | - const originalObserver = global.ResizeObserver; |
8 | | - let disconnectSpy: jest.Mock; |
9 | | - let observeSpy: jest.Mock; |
10 | | - |
11 | | - const HEIGHT = 600; |
12 | | - const WIDTH = 800; |
13 | | - |
14 | | - beforeEach(() => { |
15 | | - disconnectSpy = jest.fn(); |
16 | | - observeSpy = jest.fn(); |
17 | | - |
18 | | - global.ResizeObserver = class MockedResizeObserver { |
19 | | - constructor(cb: ResizeObserverCallback) { |
20 | | - setTimeout(() => { |
21 | | - cb( |
22 | | - [ |
23 | | - { |
24 | | - contentRect: { |
25 | | - height: HEIGHT, |
26 | | - width: WIDTH, |
27 | | - }, |
28 | | - }, |
29 | | - ] as ResizeObserverEntry[], |
30 | | - this |
31 | | - ); |
32 | | - }, 150); |
33 | | - } |
34 | | - |
35 | | - // eslint-disable-next-line @typescript-eslint/no-empty-function |
36 | | - observe = observeSpy; |
37 | | - |
38 | | - // eslint-disable-next-line @typescript-eslint/no-empty-function |
39 | | - unobserve = () => {}; |
| 5 | + const height = 600; |
| 6 | + const width = 800; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + global.ResizeObserver = class MockedResizeObserver { |
| 10 | + observe = jest.fn(); |
| 11 | + unobserve = jest.fn(); |
| 12 | + disconnect = jest.fn(); |
| 13 | + }; |
| 14 | + }); |
40 | 15 |
|
41 | | - // eslint-disable-next-line @typescript-eslint/no-empty-function |
42 | | - disconnect = disconnectSpy; |
43 | | - }; |
44 | | - }); |
| 16 | + const ComponentFixture = () => { |
| 17 | + const [state, ref] = useElementSize<HTMLDivElement>(); |
| 18 | + return ( |
| 19 | + <div ref={ref}> |
| 20 | + {state.status === 'detected' |
| 21 | + ? `detected: width: ${state.width}, height: ${state.height}` |
| 22 | + : state.status} |
| 23 | + </div> |
| 24 | + ); |
| 25 | + }; |
| 26 | + |
| 27 | + it('calculates size for typical HTML node', () => { |
| 28 | + render(<ComponentFixture />); |
| 29 | + screen.getByText(`detected: width: 0, height: 0`); |
| 30 | + }); |
45 | 31 |
|
46 | | - afterEach(() => { |
47 | | - global.ResizeObserver = originalObserver; |
| 32 | + it('listens for resize of element', async () => { |
| 33 | + const observeSpy = jest.fn(); |
| 34 | + |
| 35 | + global.ResizeObserver = class MockedResizeObserver { |
| 36 | + constructor(cb: ResizeObserverCallback) { |
| 37 | + setTimeout(() => { |
| 38 | + cb( |
| 39 | + [ |
| 40 | + { |
| 41 | + contentRect: { |
| 42 | + height, |
| 43 | + width, |
| 44 | + }, |
| 45 | + }, |
| 46 | + ] as ResizeObserverEntry[], |
| 47 | + this |
| 48 | + ); |
| 49 | + }, 150); |
| 50 | + } |
| 51 | + |
| 52 | + observe = observeSpy; |
| 53 | + unobserve = jest.fn(); |
| 54 | + disconnect = jest.fn(); |
| 55 | + }; |
| 56 | + |
| 57 | + render(<ComponentFixture />); |
| 58 | + |
| 59 | + await waitFor(() => { |
| 60 | + screen.getByText(`detected: width: ${width}, height: ${height}`); |
48 | 61 | }); |
| 62 | + }); |
49 | 63 |
|
50 | | - it('updates state if listening body', async () => { |
51 | | - const { result } = renderHook(() => useElementSize()); |
52 | | - |
53 | | - expect(observeSpy).toHaveBeenCalledTimes(1); |
54 | | - expect(observeSpy).toHaveBeenCalledWith(document.body); |
55 | | - expect(result.current.state).toEqual({ |
56 | | - status: 'undetected', |
57 | | - } as ElementSizeState); |
58 | | - |
59 | | - await waitFor(() => { |
60 | | - expect(result.current.state).toEqual({ |
61 | | - status: 'detected', |
62 | | - height: HEIGHT, |
63 | | - width: WIDTH, |
64 | | - } as ElementSizeState); |
| 64 | + it('listens for resize for body', async () => { |
| 65 | + const observeSpy = jest.fn(); |
| 66 | + |
| 67 | + global.ResizeObserver = class MockedResizeObserver { |
| 68 | + constructor(cb: ResizeObserverCallback) { |
| 69 | + setTimeout(() => { |
| 70 | + cb( |
| 71 | + [ |
| 72 | + { |
| 73 | + contentRect: { |
| 74 | + height, |
| 75 | + width, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ] as ResizeObserverEntry[], |
| 79 | + this |
| 80 | + ); |
| 81 | + }, 150); |
| 82 | + } |
| 83 | + |
| 84 | + observe = observeSpy; |
| 85 | + unobserve = jest.fn(); |
| 86 | + disconnect = jest.fn(); |
| 87 | + }; |
| 88 | + |
| 89 | + const { result } = renderHook(() => useElementSize()); |
| 90 | + |
| 91 | + await waitFor(() => { |
| 92 | + expect(result.current[0]).toEqual({ |
| 93 | + status: 'detected', |
| 94 | + height, |
| 95 | + width, |
65 | 96 | }); |
66 | 97 | }); |
| 98 | + }); |
67 | 99 |
|
68 | | - it('updates state if listening native HTML element', async () => { |
69 | | - const ComponentFixture = () => { |
70 | | - const { ref, state } = useElementSize<HTMLDivElement>(); |
71 | | - return ( |
72 | | - <div ref={ref}> |
73 | | - {state.status === 'detected' |
74 | | - ? `detected: width: ${state.width}, height: ${state.height}` |
75 | | - : state.status} |
76 | | - </div> |
77 | | - ); |
78 | | - }; |
79 | | - |
80 | | - render(<ComponentFixture />); |
81 | | - |
82 | | - const element = document.createElement('div'); |
83 | | - element.innerHTML = 'undetected' as ElementSizeStateStatus; |
| 100 | + it('disconnects after unmount', () => { |
| 101 | + const disconnectSpy = jest.fn(); |
| 102 | + global.ResizeObserver = class MockedResizeObserver { |
| 103 | + observe = jest.fn(); |
| 104 | + unobserve = jest.fn(); |
| 105 | + disconnect = disconnectSpy; |
| 106 | + }; |
84 | 107 |
|
85 | | - expect(observeSpy).toHaveBeenCalledTimes(1); |
86 | | - expect(observeSpy).toHaveBeenCalledWith(element); |
| 108 | + const { unmount } = renderHook(() => useElementSize()); |
87 | 109 |
|
88 | | - screen.getByText('undetected' as ElementSizeStateStatus); |
| 110 | + unmount(); |
89 | 111 |
|
90 | | - await waitFor(() => { |
91 | | - screen.getByText(`detected: width: ${WIDTH}, height: ${HEIGHT}`); |
92 | | - }); |
93 | | - }); |
| 112 | + expect(disconnectSpy).toHaveBeenCalledTimes(1); |
| 113 | + }); |
94 | 114 |
|
95 | | - it('disconnects after unmount', () => { |
96 | | - const { unmount } = renderHook(() => useElementSize()); |
| 115 | + it('calculates size for body when mounted', () => { |
| 116 | + jest |
| 117 | + .spyOn(document.body, 'getBoundingClientRect') |
| 118 | + .mockReturnValue({ height, width } as DOMRect); |
97 | 119 |
|
98 | | - unmount(); |
| 120 | + const { result } = renderHook(() => useElementSize()); |
99 | 121 |
|
100 | | - expect(disconnectSpy).toHaveBeenCalledTimes(1); |
| 122 | + expect(result.current[0]).toEqual({ |
| 123 | + status: 'detected', |
| 124 | + height, |
| 125 | + width, |
101 | 126 | }); |
102 | 127 | }); |
103 | 128 | }); |
0 commit comments