66 * Side Public License, v 1.
77 */
88
9- import React , { useRef } from 'react' ;
9+ import React , { type MutableRefObject , useRef } from 'react' ;
1010import { act } from '@testing-library/react' ;
1111
12- import { render } from '../../test/rtl' ;
12+ import { render , renderHook , renderHookAct } from '../../test/rtl' ;
1313
1414import { useIsPointerDown } from './useIsPointerDown' ;
1515
@@ -26,39 +26,36 @@ global.PointerEvent = MockPointerEvent;
2626describe ( 'useIsPointerDown' , ( ) => {
2727 describe ( 'without container' , ( ) => {
2828 it ( 'returns true when pointer is down and false when pointer is up' , ( ) => {
29- let isPointerDown : boolean ;
29+ const {
30+ result : { current : ref } ,
31+ } = renderHook ( useIsPointerDown ) ;
3032
31- const TestComponent = ( ) => {
32- isPointerDown = useIsPointerDown ( ) ;
33- return null ;
34- } ;
35-
36- render ( < TestComponent /> ) ;
37- expect ( isPointerDown ! ) . toBe ( false ) ;
33+ expect ( ref . current ) . toBe ( false ) ;
3834
39- act ( ( ) => {
35+ renderHookAct ( ( ) => {
4036 document . dispatchEvent (
4137 new PointerEvent ( 'pointerdown' , { bubbles : true } )
4238 ) ;
4339 } ) ;
44- expect ( isPointerDown ! ) . toBe ( true ) ;
4540
46- act ( ( ) => {
41+ expect ( ref . current ) . toBe ( true ) ;
42+
43+ renderHookAct ( ( ) => {
4744 document . dispatchEvent (
4845 new PointerEvent ( 'pointerup' , { bubbles : true } )
4946 ) ;
5047 } ) ;
51- expect ( isPointerDown ! ) . toBe ( false ) ;
48+ expect ( ref . current ) . toBe ( false ) ;
5249 } ) ;
5350 } ) ;
5451
5552 describe ( 'with container' , ( ) => {
5653 it ( 'returns true when pointer is down inside the container' , ( ) => {
57- let isPointerDown : boolean ;
54+ let isPointerDownRef : MutableRefObject < boolean > = { current : false } ;
5855
5956 const TestComponent = ( ) => {
6057 const containerRef = useRef < HTMLDivElement | null > ( null ) ;
61- isPointerDown = useIsPointerDown ( containerRef ) ;
58+ isPointerDownRef = useIsPointerDown ( containerRef ) ;
6259 return (
6360 < div >
6461 < div data-test-subj = "outside" />
@@ -68,30 +65,30 @@ describe('useIsPointerDown', () => {
6865 } ;
6966
7067 const { getByTestSubject } = render ( < TestComponent /> ) ;
71- expect ( isPointerDown ! ) . toBe ( false ) ;
68+ expect ( isPointerDownRef . current ) . toBe ( false ) ;
7269
7370 act ( ( ) => {
7471 const container = getByTestSubject ( 'container' ) ;
7572 container . dispatchEvent (
7673 new PointerEvent ( 'pointerdown' , { bubbles : true } )
7774 ) ;
7875 } ) ;
79- expect ( isPointerDown ! ) . toBe ( true ) ;
76+ expect ( isPointerDownRef . current ) . toBe ( true ) ;
8077
8178 act ( ( ) => {
8279 document . dispatchEvent (
8380 new PointerEvent ( 'pointerup' , { bubbles : true } )
8481 ) ;
8582 } ) ;
86- expect ( isPointerDown ! ) . toBe ( false ) ;
83+ expect ( isPointerDownRef . current ) . toBe ( false ) ;
8784 } ) ;
8885
8986 it ( 'returns false when pointer is down outside the container' , ( ) => {
90- let isPointerDown : boolean ;
87+ let isPointerDownRef : MutableRefObject < boolean > = { current : false } ;
9188
9289 const TestComponent = ( ) => {
9390 const containerRef = useRef < HTMLDivElement | null > ( null ) ;
94- isPointerDown = useIsPointerDown ( containerRef ) ;
91+ isPointerDownRef = useIsPointerDown ( containerRef ) ;
9592 return (
9693 < div >
9794 < div data-test-subj = "outside" />
@@ -101,69 +98,59 @@ describe('useIsPointerDown', () => {
10198 } ;
10299
103100 const { getByTestSubject } = render ( < TestComponent /> ) ;
104- expect ( isPointerDown ! ) . toBe ( false ) ;
101+ expect ( isPointerDownRef . current ) . toBe ( false ) ;
105102
106103 act ( ( ) => {
107104 const outside = getByTestSubject ( 'outside' ) ;
108105 outside . dispatchEvent (
109106 new PointerEvent ( 'pointerdown' , { bubbles : true } )
110107 ) ;
111108 } ) ;
112- expect ( isPointerDown ! ) . toBe ( false ) ;
109+ expect ( isPointerDownRef . current ) . toBe ( false ) ;
113110 } ) ;
114111 } ) ;
115112
116113 describe ( 'pointercancel and visibilitychange events' , ( ) => {
117114 it ( 'resets to false on pointercancel' , ( ) => {
118- let isPointerDown : boolean ;
119-
120- const TestComponent = ( ) => {
121- isPointerDown = useIsPointerDown ( ) ;
122- return null ;
123- } ;
115+ const {
116+ result : { current : ref } ,
117+ } = renderHook ( useIsPointerDown ) ;
124118
125- render ( < TestComponent /> ) ;
126-
127- act ( ( ) => {
119+ renderHookAct ( ( ) => {
128120 document . dispatchEvent (
129121 new PointerEvent ( 'pointerdown' , { bubbles : true } )
130122 ) ;
131123 } ) ;
132- expect ( isPointerDown ! ) . toBe ( true ) ;
124+ expect ( ref . current ) . toBe ( true ) ;
133125
134- act ( ( ) => {
126+ renderHookAct ( ( ) => {
135127 document . dispatchEvent (
136128 new PointerEvent ( 'pointercancel' , { bubbles : true } )
137129 ) ;
138130 } ) ;
139- expect ( isPointerDown ! ) . toBe ( false ) ;
131+ expect ( ref . current ) . toBe ( false ) ;
140132 } ) ;
141133
142134 it ( 'resets to false when document becomes hidden' , ( ) => {
143- let isPointerDown : boolean ;
135+ const {
136+ result : { current : ref } ,
137+ } = renderHook ( useIsPointerDown ) ;
144138
145- const TestComponent = ( ) => {
146- isPointerDown = useIsPointerDown ( ) ;
147- return null ;
148- } ;
149-
150- render ( < TestComponent /> ) ;
151-
152- act ( ( ) => {
139+ renderHookAct ( ( ) => {
153140 document . dispatchEvent (
154141 new PointerEvent ( 'pointerdown' , { bubbles : true } )
155142 ) ;
156143 } ) ;
157- expect ( isPointerDown ! ) . toBe ( true ) ;
144+ expect ( ref . current ) . toBe ( true ) ;
158145
159- act ( ( ) => {
146+ renderHookAct ( ( ) => {
160147 Object . defineProperty ( document , 'visibilityState' , {
161148 value : 'hidden' ,
162149 configurable : true ,
163150 } ) ;
164151 document . dispatchEvent ( new Event ( 'visibilitychange' ) ) ;
165152 } ) ;
166- expect ( isPointerDown ! ) . toBe ( false ) ;
153+ expect ( ref . current ) . toBe ( false ) ;
167154
168155 // reset
169156 Object . defineProperty ( document , 'visibilityState' , {
0 commit comments