|
| 1 | +import { renderHook, act } from "@testing-library/react"; |
| 2 | +import { useDebounceValue } from "../../src/utils/useDebounce"; |
| 3 | +import { |
| 4 | + jest, |
| 5 | + afterEach, |
| 6 | + beforeEach, |
| 7 | + describe, |
| 8 | + expect, |
| 9 | + test, |
| 10 | +} from "@jest/globals"; |
| 11 | + |
| 12 | +describe("useDebounceValue", () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.useFakeTimers(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + test("should return the initial value immediately", () => { |
| 22 | + const initialValue = "initial"; |
| 23 | + const { result } = renderHook(() => useDebounceValue(initialValue, 500)); |
| 24 | + |
| 25 | + expect(result.current).toBe(initialValue); |
| 26 | + }); |
| 27 | + |
| 28 | + test("should delay updating the value until after the specified interval", () => { |
| 29 | + const initialValue = "initial"; |
| 30 | + const { result, rerender } = renderHook( |
| 31 | + ({ value, interval }) => useDebounceValue(value, interval), |
| 32 | + { initialProps: { value: initialValue, interval: 500 } }, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(result.current).toBe(initialValue); |
| 36 | + |
| 37 | + // Change the value |
| 38 | + const newValue = "updated"; |
| 39 | + rerender({ value: newValue, interval: 500 }); |
| 40 | + |
| 41 | + // Value should not have changed yet |
| 42 | + expect(result.current).toBe(initialValue); |
| 43 | + |
| 44 | + // Fast-forward time by 499ms (just before the debounce interval) |
| 45 | + act(() => { |
| 46 | + jest.advanceTimersByTime(499); |
| 47 | + }); |
| 48 | + |
| 49 | + // Value should still be the initial value |
| 50 | + expect(result.current).toBe(initialValue); |
| 51 | + |
| 52 | + // Fast-forward the remaining 1ms to reach the debounce interval |
| 53 | + act(() => { |
| 54 | + jest.advanceTimersByTime(1); |
| 55 | + }); |
| 56 | + |
| 57 | + // Value should now be updated |
| 58 | + expect(result.current).toBe(newValue); |
| 59 | + }); |
| 60 | + |
| 61 | + test("should reset the debounce timer when the value changes again", () => { |
| 62 | + const initialValue = "initial"; |
| 63 | + const { result, rerender } = renderHook( |
| 64 | + ({ value, interval }) => useDebounceValue(value, interval), |
| 65 | + { initialProps: { value: initialValue, interval: 500 } }, |
| 66 | + ); |
| 67 | + |
| 68 | + // Change the value once |
| 69 | + rerender({ value: "intermediate", interval: 500 }); |
| 70 | + |
| 71 | + // Fast-forward time by 250ms (halfway through debounce interval) |
| 72 | + act(() => { |
| 73 | + jest.advanceTimersByTime(250); |
| 74 | + }); |
| 75 | + |
| 76 | + // Value should still be initial |
| 77 | + expect(result.current).toBe(initialValue); |
| 78 | + |
| 79 | + // Change the value again |
| 80 | + rerender({ value: "final", interval: 500 }); |
| 81 | + |
| 82 | + // Fast-forward another 250ms (would reach the first interval, but timer was reset) |
| 83 | + act(() => { |
| 84 | + jest.advanceTimersByTime(250); |
| 85 | + }); |
| 86 | + |
| 87 | + // Value should still be initial because timer was reset |
| 88 | + expect(result.current).toBe(initialValue); |
| 89 | + |
| 90 | + // Fast-forward to reach the new timer completion |
| 91 | + act(() => { |
| 92 | + jest.advanceTimersByTime(250); |
| 93 | + }); |
| 94 | + |
| 95 | + // Value should now be the final value |
| 96 | + expect(result.current).toBe("final"); |
| 97 | + }); |
| 98 | + |
| 99 | + test("should respect the new interval when interval changes", () => { |
| 100 | + const initialValue = "initial"; |
| 101 | + const { result, rerender } = renderHook( |
| 102 | + ({ value, interval }) => useDebounceValue(value, interval), |
| 103 | + { initialProps: { value: initialValue, interval: 500 } }, |
| 104 | + ); |
| 105 | + |
| 106 | + // Change value and interval |
| 107 | + rerender({ value: "updated", interval: 1000 }); |
| 108 | + |
| 109 | + // Fast-forward by 500ms (the original interval) |
| 110 | + act(() => { |
| 111 | + jest.advanceTimersByTime(500); |
| 112 | + }); |
| 113 | + |
| 114 | + // Value should still be initial because new interval is 1000ms |
| 115 | + expect(result.current).toBe(initialValue); |
| 116 | + |
| 117 | + // Fast-forward by another 500ms to reach new 1000ms interval |
| 118 | + act(() => { |
| 119 | + jest.advanceTimersByTime(500); |
| 120 | + }); |
| 121 | + |
| 122 | + // Value should now be updated |
| 123 | + expect(result.current).toBe("updated"); |
| 124 | + }); |
| 125 | + |
| 126 | + test("should work with different data types", () => { |
| 127 | + // Test with object |
| 128 | + const initialObject = { name: "John" }; |
| 129 | + const { result: objectResult, rerender: objectRerender } = renderHook( |
| 130 | + ({ value, interval }) => useDebounceValue(value, interval), |
| 131 | + { initialProps: { value: initialObject, interval: 200 } }, |
| 132 | + ); |
| 133 | + |
| 134 | + const newObject = { name: "Jane" }; |
| 135 | + objectRerender({ value: newObject, interval: 200 }); |
| 136 | + |
| 137 | + act(() => { |
| 138 | + jest.advanceTimersByTime(200); |
| 139 | + }); |
| 140 | + |
| 141 | + expect(objectResult.current).toEqual(newObject); |
| 142 | + |
| 143 | + // Test with number |
| 144 | + const { result: numberResult, rerender: numberRerender } = renderHook( |
| 145 | + ({ value, interval }) => useDebounceValue(value, interval), |
| 146 | + { initialProps: { value: 1, interval: 200 } }, |
| 147 | + ); |
| 148 | + |
| 149 | + numberRerender({ value: 2, interval: 200 }); |
| 150 | + |
| 151 | + act(() => { |
| 152 | + jest.advanceTimersByTime(200); |
| 153 | + }); |
| 154 | + |
| 155 | + expect(numberResult.current).toBe(2); |
| 156 | + }); |
| 157 | + |
| 158 | + test("should clean up timeout on unmount", () => { |
| 159 | + const clearTimeoutSpy = jest.spyOn(window, "clearTimeout"); |
| 160 | + const { unmount } = renderHook(() => useDebounceValue("test", 500)); |
| 161 | + |
| 162 | + unmount(); |
| 163 | + |
| 164 | + expect(clearTimeoutSpy).toHaveBeenCalled(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments