|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 3 | + |
| 4 | +import { useDebounceCallback } from './index'; |
| 5 | + |
| 6 | +describe('useDebounceCallback', () => { |
| 7 | + const callback = vi.fn(); |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + vi.useFakeTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + callback.mockRestore(); |
| 15 | + vi.useRealTimers(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('core logic', () => { |
| 19 | + it('should correctly delay the function call', async () => { |
| 20 | + const { result } = renderHook(() => |
| 21 | + useDebounceCallback({ callback, delay: 300 }) |
| 22 | + ); |
| 23 | + |
| 24 | + const [debouncedCallback] = result.current; |
| 25 | + |
| 26 | + debouncedCallback(); |
| 27 | + expect(callback).not.toBeCalled(); |
| 28 | + |
| 29 | + vi.advanceTimersByTime(250); |
| 30 | + |
| 31 | + debouncedCallback(); |
| 32 | + expect(callback).not.toBeCalled(); |
| 33 | + |
| 34 | + vi.runAllTimers(); |
| 35 | + expect(callback).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should cancel the function call when cancel is invoked', async () => { |
| 39 | + const { result } = renderHook(() => |
| 40 | + useDebounceCallback({ callback, delay: 300 }) |
| 41 | + ); |
| 42 | + |
| 43 | + const [debouncedCallback, cancel] = result.current; |
| 44 | + |
| 45 | + debouncedCallback(); |
| 46 | + cancel(); |
| 47 | + |
| 48 | + vi.runAllTimers(); |
| 49 | + |
| 50 | + expect(callback).toHaveBeenCalledTimes(0); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should cancel the function call on unmount', async () => { |
| 54 | + const { result, unmount } = renderHook(() => |
| 55 | + useDebounceCallback({ callback, delay: 300 }) |
| 56 | + ); |
| 57 | + |
| 58 | + const [debouncedCallback] = result.current; |
| 59 | + |
| 60 | + debouncedCallback(); |
| 61 | + unmount(); |
| 62 | + |
| 63 | + vi.runAllTimers(); |
| 64 | + |
| 65 | + expect(callback).toHaveBeenCalledTimes(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should reset the timeout when callback changes', async () => { |
| 69 | + const newCallback = vi.fn(); |
| 70 | + |
| 71 | + const { result, rerender } = renderHook( |
| 72 | + (cb: (args?: unknown) => void, delay = 300) => |
| 73 | + useDebounceCallback({ callback: cb || callback, delay }) |
| 74 | + ); |
| 75 | + |
| 76 | + const [debouncedCallbackOne] = result.current; |
| 77 | + debouncedCallbackOne(); |
| 78 | + expect(callback).not.toBeCalled(); |
| 79 | + |
| 80 | + rerender(newCallback); |
| 81 | + const [debouncedCallbackTwo] = result.current; |
| 82 | + debouncedCallbackTwo(); |
| 83 | + |
| 84 | + vi.runAllTimers(); |
| 85 | + |
| 86 | + expect(callback).toHaveBeenCalledTimes(0); |
| 87 | + expect(newCallback).toHaveBeenCalledTimes(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should use default delay if not provided', async () => { |
| 91 | + const { result } = renderHook(() => useDebounceCallback({ callback })); |
| 92 | + const [debouncedCallback] = result.current; |
| 93 | + |
| 94 | + debouncedCallback(); |
| 95 | + |
| 96 | + vi.runAllTimers(); |
| 97 | + |
| 98 | + expect(callback).toHaveBeenCalledTimes(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should correctly pass arguments to the callback', async () => { |
| 102 | + let mutableValue = false; |
| 103 | + |
| 104 | + const callback = vi.fn((arg: boolean) => { |
| 105 | + mutableValue = arg; |
| 106 | + |
| 107 | + return mutableValue; |
| 108 | + }); |
| 109 | + |
| 110 | + const { result } = renderHook(() => useDebounceCallback({ callback })); |
| 111 | + |
| 112 | + const [debouncedCallback] = result.current; |
| 113 | + |
| 114 | + debouncedCallback(true); |
| 115 | + |
| 116 | + vi.runAllTimers(); |
| 117 | + |
| 118 | + expect(callback).toHaveBeenCalledTimes(1); |
| 119 | + expect(mutableValue).toBe(true); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('check options prop', () => { |
| 124 | + it('should call the first callback immediately when firstCallWithoutDelay is true', () => { |
| 125 | + const { result } = renderHook(() => |
| 126 | + useDebounceCallback({ |
| 127 | + callback, |
| 128 | + delay: 300, |
| 129 | + options: { firstCallWithoutDelay: true }, |
| 130 | + }) |
| 131 | + ); |
| 132 | + |
| 133 | + const [debouncedCallback] = result.current; |
| 134 | + |
| 135 | + debouncedCallback(); |
| 136 | + expect(callback).toHaveBeenCalledTimes(1); |
| 137 | + |
| 138 | + vi.runAllTimers(); |
| 139 | + |
| 140 | + debouncedCallback(); |
| 141 | + expect(callback).toHaveBeenCalledTimes(2); |
| 142 | + |
| 143 | + debouncedCallback(); |
| 144 | + expect(callback).toHaveBeenCalledTimes(2); |
| 145 | + |
| 146 | + vi.runAllTimers(); |
| 147 | + |
| 148 | + expect(callback).toHaveBeenCalledTimes(3); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments