1
- import React , { useRef } from 'react' ;
2
- import { renderHook , act , render , screen } from '@testing-library/react' ;
3
- import useSize from '../index' ;
1
+ import React , { useRef , act } from "react" ;
2
+ import { renderHook , render , screen } from "@testing-library/react" ;
3
+ import useSize from "../index" ;
4
+ import { sleep } from "../../utils/testingHelpers" ;
4
5
5
6
let callback ;
6
- jest . mock ( ' resize-observer-polyfill' , ( ) => {
7
+ jest . mock ( " resize-observer-polyfill" , ( ) => {
7
8
return jest . fn ( ) . mockImplementation ( ( cb ) => {
8
9
callback = cb ;
9
10
return {
@@ -14,15 +15,15 @@ jest.mock('resize-observer-polyfill', () => {
14
15
} ) ;
15
16
16
17
// test about Resize Observer see https://github.com/que-etc/resize-observer-polyfill/tree/master/tests
17
- describe ( ' useSize' , ( ) => {
18
- it ( ' should work when target is a mounted DOM' , ( ) => {
18
+ describe ( " useSize" , ( ) => {
19
+ it ( " should work when target is a mounted DOM" , ( ) => {
19
20
const hook = renderHook ( ( ) => useSize ( document . body ) ) ;
20
21
expect ( hook . result . current ) . toEqual ( { height : 0 , width : 0 } ) ;
21
22
} ) ;
22
23
23
- it ( ' should work when target is a `MutableRefObject`' , async ( ) => {
24
+ it ( " should work when target is a `MutableRefObject`" , async ( ) => {
24
25
const mockRaf = jest
25
- . spyOn ( window , ' requestAnimationFrame' )
26
+ . spyOn ( window , " requestAnimationFrame" )
26
27
. mockImplementation ( ( cb : FrameRequestCallback ) => {
27
28
cb ( 0 ) ;
28
29
return 0 ;
@@ -41,29 +42,33 @@ describe('useSize', () => {
41
42
}
42
43
43
44
render ( < Setup /> ) ;
44
- expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( 'width: undefined' ) ;
45
- expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( 'height: undefined' ) ;
45
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent (
46
+ "width: undefined"
47
+ ) ;
48
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent (
49
+ "height: undefined"
50
+ ) ;
46
51
47
52
act ( ( ) => callback ( [ { target : { clientWidth : 10 , clientHeight : 10 } } ] ) ) ;
48
- expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( ' width: 10' ) ;
49
- expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( ' height: 10' ) ;
53
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( " width: 10" ) ;
54
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( " height: 10" ) ;
50
55
mockRaf . mockRestore ( ) ;
51
56
} ) ;
52
57
53
- it ( ' should not work when target is null' , ( ) => {
58
+ it ( " should not work when target is null" , ( ) => {
54
59
expect ( ( ) => {
55
60
renderHook ( ( ) => useSize ( null ) ) ;
56
61
} ) . not . toThrowError ( ) ;
57
62
} ) ;
58
63
59
- it ( ' should work' , ( ) => {
64
+ it ( " should work" , ( ) => {
60
65
const mockRaf = jest
61
- . spyOn ( window , ' requestAnimationFrame' )
66
+ . spyOn ( window , " requestAnimationFrame" )
62
67
. mockImplementation ( ( cb : FrameRequestCallback ) => {
63
68
cb ( 0 ) ;
64
69
return 0 ;
65
70
} ) ;
66
- const targetEl = document . createElement ( ' div' ) ;
71
+ const targetEl = document . createElement ( " div" ) ;
67
72
const { result } = renderHook ( ( ) => useSize ( targetEl ) ) ;
68
73
69
74
act ( ( ) => {
@@ -84,4 +89,82 @@ describe('useSize', () => {
84
89
85
90
mockRaf . mockRestore ( ) ;
86
91
} ) ;
92
+
93
+ it ( "debounceOptions should work" , async ( ) => {
94
+ let count = 0 ;
95
+
96
+ function Setup ( ) {
97
+ const ref = useRef ( null ) ;
98
+ const size = useSize ( ref , { debounceOptions : { wait : 200 } } ) ;
99
+ count += 1 ;
100
+
101
+ return (
102
+ < div ref = { ref } >
103
+ < div > width: { String ( size ?. width ) } </ div >
104
+ < div > height: { String ( size ?. height ) } </ div >
105
+ </ div >
106
+ ) ;
107
+ }
108
+
109
+ render ( < Setup /> ) ;
110
+
111
+ act ( ( ) => callback ( [ { target : { clientWidth : 10 , clientHeight : 10 } } ] ) ) ;
112
+ act ( ( ) => callback ( [ { target : { clientWidth : 20 , clientHeight : 20 } } ] ) ) ;
113
+ act ( ( ) => callback ( [ { target : { clientWidth : 30 , clientHeight : 30 } } ] ) ) ;
114
+
115
+ expect ( count ) . toBe ( 1 ) ;
116
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent (
117
+ "width: undefined"
118
+ ) ;
119
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent (
120
+ "height: undefined"
121
+ ) ;
122
+
123
+ await sleep ( 300 ) ;
124
+
125
+ expect ( count ) . toBe ( 2 ) ;
126
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( "width: 30" ) ;
127
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( "height: 30" ) ;
128
+ } ) ;
129
+
130
+ it ( "throttleOptions should work" , async ( ) => {
131
+ let count = 0 ;
132
+
133
+ function Setup ( ) {
134
+ const ref = useRef ( null ) ;
135
+ const size = useSize ( ref , { throttleOptions : { wait : 500 } } ) ;
136
+ count += 1 ;
137
+
138
+ return (
139
+ < div ref = { ref } >
140
+ < div > width: { String ( size ?. width ) } </ div >
141
+ < div > height: { String ( size ?. height ) } </ div >
142
+ </ div >
143
+ ) ;
144
+ }
145
+
146
+ render ( < Setup /> ) ;
147
+
148
+ act ( ( ) => callback ( [ { target : { clientWidth : 10 , clientHeight : 10 } } ] ) ) ;
149
+ act ( ( ) => callback ( [ { target : { clientWidth : 20 , clientHeight : 20 } } ] ) ) ;
150
+ act ( ( ) => callback ( [ { target : { clientWidth : 30 , clientHeight : 30 } } ] ) ) ;
151
+
152
+ expect ( count ) . toBe ( 1 ) ;
153
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent (
154
+ "width: undefined"
155
+ ) ;
156
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent (
157
+ "height: undefined"
158
+ ) ;
159
+
160
+ await sleep ( 450 ) ;
161
+ expect ( count ) . toBe ( 2 ) ;
162
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( "width: 10" ) ;
163
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( "height: 10" ) ;
164
+
165
+ await sleep ( 200 ) ;
166
+ expect ( count ) . toBe ( 3 ) ;
167
+ expect ( await screen . findByText ( / ^ w i d t h / ) ) . toHaveTextContent ( "width: 30" ) ;
168
+ expect ( await screen . findByText ( / ^ h e i g h t / ) ) . toHaveTextContent ( "height: 30" ) ;
169
+ } ) ;
87
170
} ) ;
0 commit comments