Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@types/react": "^19.2.5",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^4.7.0",
"@vitest/coverage-v8": "^2.1.9",
"@vitest/coverage-v8": "^3.2.4",
"bumpp": "^9.11.1",
"conventional-changelog-cli": "^5.0.0",
"cypress": "13.15.2",
Expand All @@ -52,7 +52,7 @@
"react-dom": "^19.2.0",
"typescript": "^5.9.3",
"vite": "^6.4.1",
"vitest": "^2.1.9"
"vitest": "^3.2.4"
},
"prettier": {
"semi": false,
Expand Down
18 changes: 3 additions & 15 deletions packages/react-use/src/use-raf-fn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('useRafFn', () => {

act(() => {
result.current()
vi.advanceTimersByTime(16) // Simulate the passage of time for the next frame
vi.advanceTimersToNextFrame()
})

expect(callback).toHaveBeenCalled()
Expand All @@ -32,23 +32,11 @@ describe('useRafFn', () => {
result.current()
unmount()
})

setTimeout(() => {
expect(callback).toHaveBeenCalled()
})
})

it('should not call the callback if unmounted before the next frame', () => {
const { result, unmount } = renderHook(() => useRafFn(callback))

act(() => {
result.current()
unmount()
vi.advanceTimersToNextFrame()
})

setTimeout(() => {
expect(callback).toHaveBeenCalledTimes(1) // Should only be called once
})
expect(callback).not.toHaveBeenCalled()
})

it('should fallback to pure callback if requestAnimationFrame is not available', () => {
Expand Down
22 changes: 9 additions & 13 deletions packages/react-use/src/use-raf-loop/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('useRafLoop', () => {
renderHook(() => useRafLoop(callback, { immediate: true }))

act(() => {
vi.advanceTimersByTime(1000 / 60) // Simulate frame
vi.advanceTimersToNextFrame()
})

expect(callback).toHaveBeenCalled()
Expand All @@ -28,7 +28,7 @@ describe('useRafLoop', () => {
renderHook(() => useRafLoop(callback, { immediate: false }))

act(() => {
vi.advanceTimersByTime(1000 / 60) // Simulate frame
vi.advanceTimersToNextFrame()
})

expect(callback).not.toHaveBeenCalled()
Expand All @@ -41,43 +41,39 @@ describe('useRafLoop', () => {
act(() => {
vi.advanceTimersByTime(1_000) // Simulate 1 second
})

setTimeout(() => {
expect(count.value).toBeLessThan(30)
expect(count.value).toBeGreaterThan(0)
act(() => {
vi.advanceTimersToNextFrame()
})

expect(count.value).toSatisfy((v: number) => v > 0 && v <= 30, 'within (0, 30] range')
})

it('should pause and resume correctly', () => {
const { result } = renderHook(() => useRafLoop(callback))

act(() => {
result.current.pause()
vi.advanceTimersByTime(1000 / 60) // Simulate frame
vi.advanceTimersToNextFrame()
})

expect(callback).not.toHaveBeenCalled()

act(() => {
result.current.resume()
vi.advanceTimersByTime(1000 / 60) // Simulate frame
vi.advanceTimersToNextFrame()
})

expect(callback).toHaveBeenCalled()
})

it('should call immediateCallback before the first frame', () => {
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name 'should call immediateCallback before the first frame' is misleading because immediateCallback: true is a configuration option that tells the hook to call the main callback immediately, not a separate callback. Consider renaming this test to something like 'should call callback immediately when immediateCallback option is true' to better reflect what is being tested.

Suggested change
it('should call immediateCallback before the first frame', () => {
it('should call callback immediately when immediateCallback option is true', () => {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if maintainers find the name better, it can be renamed

const immediateCallback = vi.fn()

renderHook(() =>
useRafLoop(callback, {
immediate: true,
immediateCallback: true,
}),
)

setTimeout(() => {
expect(immediateCallback).toHaveBeenCalled()
})
expect(callback).toHaveBeenCalled()
})
})
24 changes: 15 additions & 9 deletions packages/react-use/src/use-raf-state/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@ describe('useRafState', () => {
act(() => {
result.current[1](1)
})
expect(result.current[0]).toBe(initialState)

setTimeout(() => expect(result.current[0]).toBe(1))
})

it('should update state in the next animation frame', () => {
const { result } = renderHook(() => useRafState(initialState))
act(() => {
result.current[1](1)
vi.advanceTimersToNextFrame()
})
setTimeout(() => expect(result.current[0]).toBe(1))
expect(result.current[0]).toBe(1)
})

it('should handle multiple updates correctly', () => {
const { result } = renderHook(() => useRafState(initialState))

act(() => {
result.current[1](2)
result.current[1](3)
})
setTimeout(() => expect(result.current[0]).toBe(3))
act(() => {
vi.advanceTimersToNextFrame()
})

expect(result.current[0]).toBe(3)
})

it('should work with undefined initial state', () => {
Expand All @@ -52,9 +53,14 @@ describe('useRafState', () => {

it('should update state with a function', () => {
const { result } = renderHook(() => useRafState(initialState))

act(() => {
result.current[1]((prev) => prev + 1)
})
setTimeout(() => expect(result.current[0]).toBe(1))
act(() => {
vi.advanceTimersToNextFrame()
})

expect(result.current[0]).toBe(1)
})
})
Loading