Skip to content

Commit da2971c

Browse files
committed
main 🧊 rework use page leave test
1 parent 575aae2 commit da2971c

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

‎src/hooks/useIdle/useIdle.test.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,53 @@ it('Should use idle', () => {
1414
});
1515

1616
it('Should be true after 60e3', () => {
17-
const { result } = renderHook(() => useIdle(60e3));
18-
17+
const { result } = renderHook(useIdle);
1918
expect(result.current.idle).toBe(false);
2019

21-
act(() => {
22-
vi.advanceTimersByTime(60e3);
23-
});
20+
act(() => vi.advanceTimersByTime(60e3));
2421
expect(result.current.idle).toBe(true);
2522
});
2623

27-
it('Should be equal to initially passed state', () => {
28-
const { result } = renderHook(() => useIdle(60e3, { initialState: true }));
29-
24+
it('Should set initial state', () => {
25+
const { result } = renderHook(() => useIdle(60e3, { initialValue: true }));
3026
expect(result.current.idle).toBe(true);
3127
});
3228

3329
it('Should be false after interaction', () => {
34-
const { result } = renderHook(() => useIdle(60e3, { initialState: true }));
30+
const { result } = renderHook(() => useIdle(60e3));
3531

32+
act(() => vi.advanceTimersByTime(60e3));
3633
expect(result.current.idle).toBe(true);
3734

38-
act(() => {
39-
window.dispatchEvent(new Event('mousemove'));
40-
});
35+
act(() => window.dispatchEvent(new Event('mousemove')));
36+
4137
expect(result.current.idle).toBe(false);
4238
expect(result.current.lastActive).toBeLessThanOrEqual(Date.now());
4339
});
4440

45-
it('Should be false after visibilitychange', () => {
46-
const { result } = renderHook(() => useIdle(60e3, { initialState: true }));
41+
it('Should be false after visibilitychange event', () => {
42+
const { result } = renderHook(() => useIdle(60e3));
4743

48-
act(() => {
49-
document.dispatchEvent(new Event('visibilitychange'));
50-
});
44+
act(() => vi.advanceTimersByTime(60e3));
45+
expect(result.current.idle).toBe(true);
46+
47+
act(() => document.dispatchEvent(new Event('visibilitychange')));
5148
expect(result.current.idle).toBe(false);
5249
expect(result.current.lastActive).toBeLessThanOrEqual(Date.now());
5350
});
5451

55-
it('Should not react to unexpected events', () => {
56-
const { result } = renderHook(() => useIdle(60e3, { initialState: true, events: ['click'] }));
52+
it('Should work with custom events', () => {
53+
const { result } = renderHook(() => useIdle(60e3, { events: ['mousedown'] }));
54+
55+
act(() => vi.advanceTimersByTime(60e3));
56+
expect(result.current.idle).toBe(true);
57+
58+
act(() => window.dispatchEvent(new Event('mousedown')));
59+
expect(result.current.idle).toBe(false);
60+
61+
act(() => vi.advanceTimersByTime(60e3));
62+
expect(result.current.idle).toBe(true);
5763

58-
act(() => {
59-
window.dispatchEvent(new Event('mousemove'));
60-
});
64+
act(() => document.dispatchEvent(new Event('mousemove')));
6165
expect(result.current.idle).toBe(true);
6266
});

‎src/hooks/useIdle/useIdle.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import React from 'react';
33
import { throttle } from '@/utils/helpers';
44

55
interface UseIdleOptions {
6-
initialState?: boolean;
7-
events?: Array<keyof WindowEventMap>;
6+
initialValue?: boolean;
7+
events?: Array<keyof DocumentEventMap>;
88
}
99

1010
const IDLE_EVENTS = [
@@ -14,7 +14,7 @@ const IDLE_EVENTS = [
1414
'touchstart',
1515
'wheel',
1616
'resize'
17-
] satisfies Array<keyof WindowEventMap>;
17+
] satisfies Array<keyof DocumentEventMap>;
1818
const ONE_MINUTE = 60e3;
1919

2020
//* The use idle return type */
@@ -39,9 +39,9 @@ export interface UseIdleReturn {
3939
*/
4040
export const useIdle = (
4141
milliseconds = ONE_MINUTE,
42-
{ initialState = false, events = IDLE_EVENTS }: UseIdleOptions = {}
42+
{ initialValue = false, events = IDLE_EVENTS }: UseIdleOptions = {}
4343
): UseIdleReturn => {
44-
const [idle, setIdle] = React.useState(initialState);
44+
const [idle, setIdle] = React.useState(initialValue);
4545
const [lastActive, setLastActive] = React.useState(Date.now());
4646

4747
React.useEffect(() => {
@@ -51,7 +51,7 @@ export const useIdle = (
5151
const onEvent = throttle(() => {
5252
setIdle(false);
5353
setLastActive(Date.now());
54-
window.clearTimeout(timeoutId);
54+
clearTimeout(timeoutId);
5555
timeoutId = setTimeout(onTimeout, milliseconds);
5656
}, 500);
5757

@@ -67,7 +67,7 @@ export const useIdle = (
6767
return () => {
6868
events.forEach((event) => window.addEventListener(event, onEvent));
6969
document.removeEventListener('visibilitychange', onVisibilitychange);
70-
window.clearTimeout(timeoutId);
70+
clearTimeout(timeoutId);
7171
};
7272
}, [milliseconds, events]);
7373

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { act, fireEvent, renderHook } from '@testing-library/react';
1+
import { act, renderHook } from '@testing-library/react';
22

33
import { usePageLeave } from './usePageLeave';
44

55
it('Should use page leave', () => {
66
const callback = vi.fn();
77
renderHook(() => usePageLeave(callback));
88

9-
act(() => fireEvent.mouseLeave(document));
9+
act(() => document.dispatchEvent(new Event('mouseleave')));
1010
expect(callback).toBeCalledTimes(1);
1111

12-
act(() => fireEvent.mouseEnter(document));
12+
act(() => document.dispatchEvent(new Event('mouseenter')));
1313

14-
act(() => fireEvent.mouseLeave(document));
14+
act(() => document.dispatchEvent(new Event('mouseleave')));
1515
expect(callback).toBeCalledTimes(2);
1616
});

0 commit comments

Comments
 (0)