|
| 1 | +import { renderHook } from "@testing-library/react"; |
| 2 | +import { useDebouncedFunction, useDebouncedValue } from "./debounce"; |
| 3 | + |
| 4 | +beforeAll(() => { |
| 5 | + jest.useFakeTimers(); |
| 6 | + jest.spyOn(global, "setTimeout"); |
| 7 | +}); |
| 8 | + |
| 9 | +afterAll(() => { |
| 10 | + jest.useRealTimers(); |
| 11 | + jest.clearAllMocks(); |
| 12 | +}); |
| 13 | + |
| 14 | +// Most UI tests should be structure from the user's experience, but just |
| 15 | +// because these are more abstract, general-purpose hooks, it seemed harder to |
| 16 | +// do that. Had to bring in some mocks |
| 17 | +function renderDebouncedValue<T = unknown>(value: T, time: number) { |
| 18 | + return renderHook( |
| 19 | + ({ value, time }: { value: T; time: number }) => { |
| 20 | + return useDebouncedValue(value, time); |
| 21 | + }, |
| 22 | + { |
| 23 | + initialProps: { value, time }, |
| 24 | + }, |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function renderDebouncedFunction<Args extends unknown[]>( |
| 29 | + callbackArg: (...args: Args) => void | Promise<void>, |
| 30 | + time: number, |
| 31 | +) { |
| 32 | + return renderHook( |
| 33 | + ({ callback, time }: { callback: typeof callbackArg; time: number }) => { |
| 34 | + return useDebouncedFunction<Args>(callback, time); |
| 35 | + }, |
| 36 | + { |
| 37 | + initialProps: { callback: callbackArg, time }, |
| 38 | + }, |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +describe(`${useDebouncedValue.name}`, () => { |
| 43 | + it("Should immediately return out the exact same value (by reference) on mount", () => { |
| 44 | + const value = {}; |
| 45 | + const { result } = renderDebouncedValue(value, 2000); |
| 46 | + |
| 47 | + expect(result.current).toBe(value); |
| 48 | + expect.hasAssertions(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Should not immediately resync state as the hook re-renders with new value argument", async () => { |
| 52 | + let value = 0; |
| 53 | + const time = 5000; |
| 54 | + |
| 55 | + const { result, rerender } = renderDebouncedValue(value, time); |
| 56 | + expect(result.current).toEqual(0); |
| 57 | + |
| 58 | + for (let i = 1; i <= 5; i++) { |
| 59 | + setTimeout(() => { |
| 60 | + value++; |
| 61 | + rerender({ value, time }); |
| 62 | + }, i * 100); |
| 63 | + } |
| 64 | + |
| 65 | + await jest.advanceTimersByTimeAsync(time - 100); |
| 66 | + expect(result.current).toEqual(0); |
| 67 | + expect.hasAssertions(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("Should resync after specified milliseconds pass with no change to arguments", async () => { |
| 71 | + const initialValue = false; |
| 72 | + const time = 5000; |
| 73 | + |
| 74 | + const { result, rerender } = renderDebouncedValue(initialValue, time); |
| 75 | + expect(result.current).toEqual(false); |
| 76 | + |
| 77 | + rerender({ value: !initialValue, time }); |
| 78 | + await jest.runAllTimersAsync(); |
| 79 | + |
| 80 | + expect(result.current).toEqual(true); |
| 81 | + expect.hasAssertions(); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe(`${useDebouncedFunction.name}`, () => { |
| 86 | + describe("hook", () => { |
| 87 | + it("Should provide stable function references across re-renders", () => { |
| 88 | + const time = 5000; |
| 89 | + const { result, rerender } = renderDebouncedFunction(jest.fn(), time); |
| 90 | + |
| 91 | + const { debounced: oldDebounced, cancelDebounce: oldCancel } = |
| 92 | + result.current; |
| 93 | + |
| 94 | + rerender({ callback: jest.fn(), time }); |
| 95 | + const { debounced: newDebounced, cancelDebounce: newCancel } = |
| 96 | + result.current; |
| 97 | + |
| 98 | + expect(oldDebounced).toBe(newDebounced); |
| 99 | + expect(oldCancel).toBe(newCancel); |
| 100 | + expect.hasAssertions(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("Resets any pending debounces if the timer argument changes", async () => { |
| 104 | + const time = 5000; |
| 105 | + let count = 0; |
| 106 | + const incrementCount = () => { |
| 107 | + count++; |
| 108 | + }; |
| 109 | + |
| 110 | + const { result, rerender } = renderDebouncedFunction( |
| 111 | + incrementCount, |
| 112 | + time, |
| 113 | + ); |
| 114 | + |
| 115 | + result.current.debounced(); |
| 116 | + rerender({ callback: incrementCount, time: time + 1 }); |
| 117 | + |
| 118 | + await jest.runAllTimersAsync(); |
| 119 | + expect(count).toEqual(0); |
| 120 | + expect.hasAssertions(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("debounced function", () => { |
| 125 | + it("Resolve the debounce after specified milliseconds pass with no other calls", async () => { |
| 126 | + let value = false; |
| 127 | + const { result } = renderDebouncedFunction(() => { |
| 128 | + value = !value; |
| 129 | + }, 100); |
| 130 | + |
| 131 | + result.current.debounced(); |
| 132 | + |
| 133 | + await jest.runOnlyPendingTimersAsync(); |
| 134 | + expect(value).toBe(true); |
| 135 | + expect.hasAssertions(); |
| 136 | + }); |
| 137 | + |
| 138 | + it("Always uses the most recent callback argument passed in (even if it switches while a debounce is queued)", async () => { |
| 139 | + let count = 0; |
| 140 | + const time = 500; |
| 141 | + |
| 142 | + const { result, rerender } = renderDebouncedFunction(() => { |
| 143 | + count = 1; |
| 144 | + }, time); |
| 145 | + |
| 146 | + result.current.debounced(); |
| 147 | + rerender({ |
| 148 | + callback: () => { |
| 149 | + count = 9999; |
| 150 | + }, |
| 151 | + time, |
| 152 | + }); |
| 153 | + |
| 154 | + await jest.runAllTimersAsync(); |
| 155 | + expect(count).toEqual(9999); |
| 156 | + expect.hasAssertions(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("Should reset the debounce timer with repeated calls to the method", async () => { |
| 160 | + let count = 0; |
| 161 | + const { result } = renderDebouncedFunction(() => { |
| 162 | + count++; |
| 163 | + }, 2000); |
| 164 | + |
| 165 | + for (let i = 0; i < 10; i++) { |
| 166 | + setTimeout(() => { |
| 167 | + result.current.debounced(); |
| 168 | + }, i * 100); |
| 169 | + } |
| 170 | + |
| 171 | + await jest.runAllTimersAsync(); |
| 172 | + expect(count).toBe(1); |
| 173 | + expect.hasAssertions(); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe("cancelDebounce function", () => { |
| 178 | + it("Should be able to cancel a pending debounce", async () => { |
| 179 | + let count = 0; |
| 180 | + const { result } = renderDebouncedFunction(() => { |
| 181 | + count++; |
| 182 | + }, 2000); |
| 183 | + |
| 184 | + const { debounced, cancelDebounce } = result.current; |
| 185 | + debounced(); |
| 186 | + cancelDebounce(); |
| 187 | + |
| 188 | + await jest.runAllTimersAsync(); |
| 189 | + expect(count).toEqual(0); |
| 190 | + expect.hasAssertions(); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments