-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from Ceylar37/test-useIdle
test: add tests for useIdle hook
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
import { useIdle } from './useIdle'; | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
it('Should use idle', () => { | ||
const { result } = renderHook(useIdle); | ||
|
||
expect(result.current.idle).toBe(false); | ||
expect(result.current.lastActive).toBeLessThanOrEqual(Date.now()); | ||
}); | ||
|
||
it('Should be true after 60e3', () => { | ||
const { result } = renderHook(() => useIdle(60e3)); | ||
|
||
expect(result.current.idle).toBe(false); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(60e3); | ||
}); | ||
expect(result.current.idle).toBe(true); | ||
}); | ||
|
||
it('Should be equal to initially passed state', () => { | ||
const { result } = renderHook(() => useIdle(60e3, { initialState: true })); | ||
|
||
expect(result.current.idle).toBe(true); | ||
}); | ||
|
||
it('Should be false after interaction', () => { | ||
const { result } = renderHook(() => useIdle(60e3, { initialState: true })); | ||
|
||
expect(result.current.idle).toBe(true); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('mousemove')); | ||
}); | ||
expect(result.current.idle).toBe(false); | ||
expect(result.current.lastActive).toBeLessThanOrEqual(Date.now()); | ||
}); | ||
|
||
it('Should be false after visibilitychange', () => { | ||
const { result } = renderHook(() => useIdle(60e3, { initialState: true })); | ||
|
||
act(() => { | ||
document.dispatchEvent(new Event('visibilitychange')); | ||
}); | ||
expect(result.current.idle).toBe(false); | ||
expect(result.current.lastActive).toBeLessThanOrEqual(Date.now()); | ||
}); | ||
|
||
it('Should not react to unexpected events', () => { | ||
const { result } = renderHook(() => useIdle(60e3, { initialState: true, events: ['click'] })); | ||
|
||
act(() => { | ||
window.dispatchEvent(new Event('mousemove')); | ||
}); | ||
expect(result.current.idle).toBe(true); | ||
}); |