Skip to content

Commit d72bfd2

Browse files
committed
fix(test): ensure raf hooks test assertions
1 parent 08699c0 commit d72bfd2

7 files changed

Lines changed: 656 additions & 304 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@types/react": "^19.2.5",
3939
"@types/react-dom": "^19.2.3",
4040
"@vitejs/plugin-react": "^4.7.0",
41-
"@vitest/coverage-v8": "^2.1.9",
41+
"@vitest/coverage-v8": "^3.2.4",
4242
"bumpp": "^9.11.1",
4343
"conventional-changelog-cli": "^5.0.0",
4444
"cypress": "13.15.2",
@@ -52,7 +52,7 @@
5252
"react-dom": "^19.2.0",
5353
"typescript": "^5.9.3",
5454
"vite": "^6.4.1",
55-
"vitest": "^2.1.9"
55+
"vitest": "^3.2.4"
5656
},
5757
"prettier": {
5858
"semi": false,

packages/react-use/src/use-raf-fn/index.test.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,23 @@ describe('useRafFn', () => {
1919

2020
act(() => {
2121
result.current()
22-
vi.advanceTimersByTime(16) // Simulate the passage of time for the next frame
22+
vi.advanceTimersToNextFrame()
2323
})
2424

2525
expect(callback).toHaveBeenCalled()
2626
})
2727

2828
it('should clean up the animation frame on unmount', () => {
29-
const { result, unmount } = renderHook(() => useRafFn(callback))
29+
const { unmount } = renderHook(() => useRafFn(callback))
3030

3131
act(() => {
32-
result.current()
3332
unmount()
3433
})
35-
36-
setTimeout(() => {
37-
expect(callback).toHaveBeenCalled()
38-
})
39-
})
40-
41-
it('should not call the callback if unmounted before the next frame', () => {
42-
const { result, unmount } = renderHook(() => useRafFn(callback))
43-
4434
act(() => {
45-
result.current()
46-
unmount()
35+
vi.advanceTimersToNextFrame()
4736
})
4837

49-
setTimeout(() => {
50-
expect(callback).toHaveBeenCalledTimes(1) // Should only be called once
51-
})
38+
expect(callback).not.toHaveBeenCalled()
5239
})
5340

5441
it('should fallback to pure callback if requestAnimationFrame is not available', () => {

packages/react-use/src/use-raf-loop/index.test.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('useRafLoop', () => {
1818
renderHook(() => useRafLoop(callback, { immediate: true }))
1919

2020
act(() => {
21-
vi.advanceTimersByTime(1000 / 60) // Simulate frame
21+
vi.advanceTimersToNextFrame()
2222
})
2323

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

3030
act(() => {
31-
vi.advanceTimersByTime(1000 / 60) // Simulate frame
31+
vi.advanceTimersToNextFrame()
3232
})
3333

3434
expect(callback).not.toHaveBeenCalled()
@@ -41,43 +41,39 @@ describe('useRafLoop', () => {
4141
act(() => {
4242
vi.advanceTimersByTime(1_000) // Simulate 1 second
4343
})
44-
45-
setTimeout(() => {
46-
expect(count.value).toBeLessThan(30)
47-
expect(count.value).toBeGreaterThan(0)
44+
act(() => {
45+
vi.advanceTimersToNextFrame()
4846
})
47+
48+
expect(count.value).toSatisfy((v: number) => v > 0 && v <= 30, 'within (0, 30] range')
4949
})
5050

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

5454
act(() => {
5555
result.current.pause()
56-
vi.advanceTimersByTime(1000 / 60) // Simulate frame
56+
vi.advanceTimersToNextFrame()
5757
})
5858

5959
expect(callback).not.toHaveBeenCalled()
6060

6161
act(() => {
6262
result.current.resume()
63-
vi.advanceTimersByTime(1000 / 60) // Simulate frame
63+
vi.advanceTimersToNextFrame()
6464
})
6565

6666
expect(callback).toHaveBeenCalled()
6767
})
6868

6969
it('should call immediateCallback before the first frame', () => {
70-
const immediateCallback = vi.fn()
71-
7270
renderHook(() =>
7371
useRafLoop(callback, {
7472
immediate: true,
7573
immediateCallback: true,
7674
}),
7775
)
7876

79-
setTimeout(() => {
80-
expect(immediateCallback).toHaveBeenCalled()
81-
})
77+
expect(callback).toHaveBeenCalled()
8278
})
8379
})

packages/react-use/src/use-raf-state/index.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,38 @@ describe('useRafState', () => {
2424
act(() => {
2525
result.current[1](1)
2626
})
27+
act(() => {
28+
vi.advanceTimersToNextFrame()
29+
})
2730

28-
setTimeout(() => expect(result.current[0]).toBe(1))
31+
expect(result.current[0]).toBe(1)
2932
})
3033

3134
it('should update state in the next animation frame', () => {
3235
const { result } = renderHook(() => useRafState(initialState))
36+
3337
act(() => {
3438
result.current[1](1)
3539
})
36-
setTimeout(() => expect(result.current[0]).toBe(1))
40+
act(() => {
41+
vi.advanceTimersToNextFrame()
42+
})
43+
44+
expect(result.current[0]).toBe(1)
3745
})
3846

3947
it('should handle multiple updates correctly', () => {
4048
const { result } = renderHook(() => useRafState(initialState))
49+
4150
act(() => {
4251
result.current[1](2)
4352
result.current[1](3)
4453
})
45-
setTimeout(() => expect(result.current[0]).toBe(3))
54+
act(() => {
55+
vi.advanceTimersToNextFrame()
56+
})
57+
58+
expect(result.current[0]).toBe(3)
4659
})
4760

4861
it('should work with undefined initial state', () => {
@@ -52,9 +65,14 @@ describe('useRafState', () => {
5265

5366
it('should update state with a function', () => {
5467
const { result } = renderHook(() => useRafState(initialState))
68+
5569
act(() => {
5670
result.current[1]((prev) => prev + 1)
5771
})
58-
setTimeout(() => expect(result.current[0]).toBe(1))
72+
act(() => {
73+
vi.advanceTimersToNextFrame()
74+
})
75+
76+
expect(result.current[0]).toBe(1)
5977
})
6078
})

0 commit comments

Comments
 (0)