Skip to content
Open
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
69 changes: 25 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@rollup/plugin-typescript": "^11.1.6",
"@shopify/jest-dom-mocks": "^5.2.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/react": "^16.3.1",
Copy link
Owner

Choose a reason for hiding this comment

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

I recently updated the testing library packages, so this is now obsolete.

"@testing-library/user-event": "^14.5.2",
"@types/jest": "^29.5.12",
"@types/node": "^14.18.34",
Expand Down
22 changes: 21 additions & 1 deletion src/timeout/useThrottle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('useThrottle', () => {
})
})

it('properly cleans up timeout after unmount', async () => {
it('cleans up timeout after unmount', async () => {
const timeoutHandler = jest.fn()

const { unmount } = renderHook(() => {
Expand All @@ -232,6 +232,26 @@ describe('useThrottle', () => {
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)
expect(timeoutHandler).toHaveBeenCalledTimes(1)
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)

See my other comment at L252

})

it('cleans up and sets up again in strict mode', async () => {
const timeoutHandler = jest.fn()

renderHook(
() => {
const func = useThrottle(timeoutHandler, 500, { leading: false })
useEffect(() => {
func()
}, [func])
},
{ reactStrictMode: true }
)

await act(() => {
jest.runAllTimers()
})

expect(timeoutHandler).toHaveBeenCalledTimes(1)
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
expect(timeoutHandler).toHaveBeenCalledTimes(1)
expect(timeoutHandler).toHaveBeenCalledTimes(1)
expect(clearTimeoutSpy).toHaveBeenCalledTimes(1)

Copy link
Owner

Choose a reason for hiding this comment

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

I suggest that we test both the execution and cleanup call in both new and old tests.

})

it('returns the timeout id for manual clearance', async () => {
const timeoutHandler = jest.fn()

Expand Down
1 change: 1 addition & 0 deletions src/timeout/useThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function useThrottle<T extends (...args: never[]) => unknown>(
return () => {
if (timeoutId.current) {
clearTimeout(timeoutId.current)
timeoutId.current = null
}
}
}, [])
Expand Down
Loading