|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx'; |
| 4 | + |
| 5 | +import { useThrottledCallback } from './useThrottledCallback.ts'; |
| 6 | + |
| 7 | +describe('useThrottledCallback', () => { |
| 8 | + beforeEach(() => { |
| 9 | + vi.useFakeTimers(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('is safe on server side rendering', () => { |
| 13 | + const onChange = vi.fn(); |
| 14 | + renderHookSSR.serverOnly(() => useThrottledCallback({ onChange, timeThreshold: 100 })); |
| 15 | + |
| 16 | + expect(onChange).not.toHaveBeenCalled(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should throttle the callback with the specified time threshold', () => { |
| 20 | + const onChange = vi.fn(); |
| 21 | + const { result } = renderHookSSR(() => useThrottledCallback({ onChange, timeThreshold: 100 })); |
| 22 | + |
| 23 | + result.current(true); |
| 24 | + expect(onChange).toBeCalledTimes(1); |
| 25 | + expect(onChange).toBeCalledWith(true); |
| 26 | + |
| 27 | + result.current(true); |
| 28 | + vi.advanceTimersByTime(50); |
| 29 | + expect(onChange).toBeCalledTimes(1); |
| 30 | + |
| 31 | + vi.advanceTimersByTime(50); |
| 32 | + expect(onChange).toBeCalledTimes(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should call on leading edge by default', () => { |
| 36 | + const onChange = vi.fn(); |
| 37 | + const { result } = renderHookSSR(() => useThrottledCallback({ onChange, timeThreshold: 100 })); |
| 38 | + |
| 39 | + result.current(true); |
| 40 | + expect(onChange).toBeCalledTimes(1); |
| 41 | + expect(onChange).toBeCalledWith(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should handle trailing edge', () => { |
| 45 | + const onChange = vi.fn(); |
| 46 | + const { result } = renderHookSSR(() => useThrottledCallback({ onChange, timeThreshold: 100, edges: ['trailing'] })); |
| 47 | + |
| 48 | + result.current(true); |
| 49 | + expect(onChange).not.toBeCalled(); |
| 50 | + |
| 51 | + vi.advanceTimersByTime(100); |
| 52 | + expect(onChange).toBeCalledTimes(1); |
| 53 | + expect(onChange).toBeCalledWith(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not trigger callback if value has not changed', () => { |
| 57 | + const onChange = vi.fn(); |
| 58 | + const { result } = renderHookSSR(() => useThrottledCallback({ onChange, timeThreshold: 100 })); |
| 59 | + |
| 60 | + result.current(true); |
| 61 | + vi.advanceTimersByTime(100); |
| 62 | + expect(onChange).toBeCalledTimes(1); |
| 63 | + |
| 64 | + result.current(true); |
| 65 | + vi.advanceTimersByTime(100); |
| 66 | + expect(onChange).toBeCalledTimes(1); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should cleanup on unmount', async () => { |
| 70 | + const onChange = vi.fn(); |
| 71 | + const { result, unmount } = await renderHookSSR(() => |
| 72 | + useThrottledCallback({ onChange, timeThreshold: 100, edges: ['trailing'] }) |
| 73 | + ); |
| 74 | + |
| 75 | + result.current(true); |
| 76 | + unmount(); |
| 77 | + vi.advanceTimersByTime(100); |
| 78 | + |
| 79 | + expect(onChange).not.toBeCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle value toggling', () => { |
| 83 | + const onChange = vi.fn(); |
| 84 | + const { result } = renderHookSSR(() => useThrottledCallback({ onChange, timeThreshold: 100 })); |
| 85 | + |
| 86 | + result.current(true); |
| 87 | + expect(onChange).toBeCalledTimes(1); |
| 88 | + expect(onChange).toBeCalledWith(true); |
| 89 | + |
| 90 | + vi.advanceTimersByTime(100); |
| 91 | + |
| 92 | + result.current(false); |
| 93 | + expect(onChange).toBeCalledTimes(2); |
| 94 | + expect(onChange).toBeCalledWith(false); |
| 95 | + }); |
| 96 | +}); |
0 commit comments