11import { act , renderHook } from '@testing-library/react' ;
22
3+ import { renderHookServer } from '@/tests' ;
4+
35import { useDeviceMotion } from './useDeviceMotion' ;
46
57const DEVICE_MOTION_EVENT_INIT : DeviceMotionEventInit = {
@@ -41,26 +43,46 @@ globalThis.DeviceMotionEvent = MockDeviceMotionEvent as any;
4143it ( 'Should use device motion' , ( ) => {
4244 const { result } = renderHook ( useDeviceMotion ) ;
4345
44- expect ( result . current . interval ) . toBe ( 0 ) ;
45- expect ( result . current . rotationRate ) . toEqual ( {
46+ expect ( result . current . watch ) . toBeTypeOf ( 'function' ) ;
47+ expect ( result . current . snapshot . interval ) . toBe ( 0 ) ;
48+ expect ( result . current . snapshot . rotationRate ) . toEqual ( {
4649 alpha : null ,
4750 beta : null ,
4851 gamma : null
4952 } ) ;
50- expect ( result . current . acceleration ) . toEqual ( { x : null , y : null , z : null } ) ;
51- expect ( result . current . accelerationIncludingGravity ) . toEqual ( {
53+ expect ( result . current . snapshot . acceleration ) . toEqual ( { x : null , y : null , z : null } ) ;
54+ expect ( result . current . snapshot . accelerationIncludingGravity ) . toEqual ( {
5255 x : null ,
5356 y : null ,
5457 z : null
5558 } ) ;
5659} ) ;
5760
58- it ( 'Should handle device motion event' , ( ) => {
61+ it ( 'Should use device motion on server side' , ( ) => {
62+ const { result } = renderHookServer ( useDeviceMotion ) ;
63+
64+ expect ( result . current . watch ) . toBeTypeOf ( 'function' ) ;
65+ expect ( result . current . snapshot . interval ) . toBe ( 0 ) ;
66+ expect ( result . current . snapshot . rotationRate ) . toEqual ( {
67+ alpha : null ,
68+ beta : null ,
69+ gamma : null
70+ } ) ;
71+ expect ( result . current . snapshot . acceleration ) . toEqual ( { x : null , y : null , z : null } ) ;
72+ expect ( result . current . snapshot . accelerationIncludingGravity ) . toEqual ( {
73+ x : null ,
74+ y : null ,
75+ z : null
76+ } ) ;
77+ } ) ;
78+
79+ it ( 'Should return reactive value on watch' , ( ) => {
5980 const { result } = renderHook ( useDeviceMotion ) ;
6081
82+ act ( ( ) => result . current . watch ( ) ) ;
6183 act ( ( ) => window . dispatchEvent ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ) ;
6284
63- expect ( result . current ) . toEqual ( {
85+ expect ( result . current . snapshot ) . toEqual ( {
6486 interval : DEVICE_MOTION_EVENT_INIT . interval ,
6587 rotationRate : DEVICE_MOTION_EVENT_INIT . rotationRate ,
6688 acceleration : DEVICE_MOTION_EVENT_INIT . acceleration ,
@@ -71,10 +93,11 @@ it('Should handle device motion event', () => {
7193it ( 'Should call callback when motion detected' , ( ) => {
7294 const onChange = vi . fn ( ) ;
7395 renderHook ( ( ) => useDeviceMotion ( { onChange } ) ) ;
96+ const event = new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ;
7497
75- act ( ( ) => window . dispatchEvent ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ) ;
98+ act ( ( ) => window . dispatchEvent ( event ) ) ;
7699
77- expect ( onChange ) . toBeCalledWith ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ;
100+ expect ( onChange ) . toHaveBeenCalledWith ( event ) ;
78101} ) ;
79102
80103it ( 'Should be listen enabled param' , ( ) => {
@@ -84,45 +107,21 @@ it('Should be listen enabled param', () => {
84107 initialProps : { enabled : false }
85108 } ) ;
86109
87- expect ( addEventListenerSpy ) . not . toBeCalledWith ( 'devicemotion' , expect . any ( Function ) ) ;
110+ expect ( addEventListenerSpy ) . not . toHaveBeenCalledWith ( 'devicemotion' , expect . any ( Function ) ) ;
88111
89112 rerender ( { enabled : true } ) ;
90113
91- expect ( addEventListenerSpy ) . toBeCalledWith ( 'devicemotion' , expect . any ( Function ) ) ;
114+ expect ( addEventListenerSpy ) . toHaveBeenCalledWith ( 'devicemotion' , expect . any ( Function ) ) ;
92115} ) ;
93116
94- it ( 'Should throttle events by delay' , ( ) => {
95- vi . useFakeTimers ( ) ;
117+ it ( 'Should handle events without throttling' , ( ) => {
96118 const onChange = vi . fn ( ) ;
97119 renderHook ( ( ) => useDeviceMotion ( { onChange } ) ) ;
98120
99121 act ( ( ) => window . dispatchEvent ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ) ;
100-
101- expect ( onChange ) . toHaveBeenCalledOnce ( ) ;
102-
103122 act ( ( ) => window . dispatchEvent ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ) ;
104123
105- act ( ( ) => vi . advanceTimersByTime ( 1000 ) ) ;
106-
107124 expect ( onChange ) . toBeCalledTimes ( 2 ) ;
108-
109- vi . useRealTimers ( ) ;
110- } ) ;
111-
112- it ( 'Should update with new delay' , ( ) => {
113- const addEventListenerSpy = vi . spyOn ( window , 'addEventListener' ) ;
114- const removeEventListenerSpy = vi . spyOn ( window , 'removeEventListener' ) ;
115-
116- const { rerender } = renderHook ( ( delay : number ) => useDeviceMotion ( { delay } ) , {
117- initialProps : 1000
118- } ) ;
119-
120- expect ( addEventListenerSpy ) . toHaveBeenCalledOnce ( ) ;
121-
122- rerender ( 500 ) ;
123-
124- expect ( removeEventListenerSpy ) . toHaveBeenCalledOnce ( ) ;
125- expect ( addEventListenerSpy ) . toBeCalledTimes ( 2 ) ;
126125} ) ;
127126
128127it ( 'Should handle enabled changes' , ( ) => {
@@ -146,9 +145,9 @@ it('Should handle enabled changes', () => {
146145 expect ( removeEventListenerSpy ) . toHaveBeenCalledOnce ( ) ;
147146} ) ;
148147
149- it ( 'Should handle delay and callback' , ( ) => {
148+ it ( 'Should handle callback' , ( ) => {
150149 const callback = vi . fn ( ) ;
151- renderHook ( ( ) => useDeviceMotion ( callback , 1000 ) ) ;
150+ renderHook ( ( ) => useDeviceMotion ( callback ) ) ;
152151
153152 act ( ( ) => window . dispatchEvent ( new DeviceMotionEvent ( 'devicemotion' , DEVICE_MOTION_EVENT_INIT ) ) ) ;
154153
0 commit comments