|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx'; |
| 4 | + |
| 5 | +import { useAsyncLock } from './useAsyncLock.ts'; |
| 6 | + |
| 7 | +describe('useAsyncLock', () => { |
| 8 | + it('is safe on server side rendering', () => { |
| 9 | + const result = renderHookSSR.serverOnly(() => useAsyncLock()); |
| 10 | + |
| 11 | + expect(result.current.isLocked()).toBe(false); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should execute a callback when the lock is available', async () => { |
| 15 | + const callback = vi.fn(() => 'done'); |
| 16 | + const { result } = await renderHookSSR(() => useAsyncLock()); |
| 17 | + |
| 18 | + await expect(result.current.runWithLock(callback)).resolves.toEqual({ |
| 19 | + status: 'executed', |
| 20 | + data: 'done', |
| 21 | + }); |
| 22 | + expect(callback).toHaveBeenCalledTimes(1); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should block overlapping executions', async () => { |
| 26 | + let resolvePending: (value: string) => void = () => {}; |
| 27 | + const pending = new Promise<string>(resolve => { |
| 28 | + resolvePending = resolve; |
| 29 | + }); |
| 30 | + const firstCallback = vi.fn(() => pending); |
| 31 | + const secondCallback = vi.fn(() => 'blocked'); |
| 32 | + const { result } = renderHookSSR(() => useAsyncLock()); |
| 33 | + |
| 34 | + const firstResult = result.current.runWithLock(firstCallback); |
| 35 | + |
| 36 | + expect(result.current.isLocked()).toBe(true); |
| 37 | + await expect(result.current.runWithLock(secondCallback)).resolves.toEqual({ |
| 38 | + status: 'blocked', |
| 39 | + }); |
| 40 | + expect(secondCallback).not.toHaveBeenCalled(); |
| 41 | + |
| 42 | + resolvePending('done'); |
| 43 | + await expect(firstResult).resolves.toEqual({ |
| 44 | + status: 'executed', |
| 45 | + data: 'done', |
| 46 | + }); |
| 47 | + expect(result.current.isLocked()).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should release the lock after a callback rejects', async () => { |
| 51 | + const error = new Error('failed'); |
| 52 | + const rejectedCallback = vi.fn(async () => { |
| 53 | + throw error; |
| 54 | + }); |
| 55 | + const nextCallback = vi.fn(() => 'next'); |
| 56 | + const { result } = renderHookSSR(() => useAsyncLock()); |
| 57 | + |
| 58 | + await expect(result.current.runWithLock(rejectedCallback)).rejects.toThrow(error); |
| 59 | + expect(result.current.isLocked()).toBe(false); |
| 60 | + |
| 61 | + await expect(result.current.runWithLock(nextCallback)).resolves.toEqual({ |
| 62 | + status: 'executed', |
| 63 | + data: 'next', |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should keep returned functions stable across rerenders', async () => { |
| 68 | + const { result, rerender } = renderHookSSR(() => useAsyncLock()); |
| 69 | + const initialValue = result.current; |
| 70 | + |
| 71 | + rerender(); |
| 72 | + |
| 73 | + expect(result.current).toBe(initialValue); |
| 74 | + expect(result.current.runWithLock).toBe(initialValue.runWithLock); |
| 75 | + expect(result.current.isLocked).toBe(initialValue.isLocked); |
| 76 | + }); |
| 77 | +}); |
0 commit comments