|
| 1 | +// Integration tests: renderHook exercises the full React lifecycle including |
| 2 | +// useAlive (cleanup on unmount) and retry-race-condition logic. |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { renderHook, act } from '@testing-library/react'; |
| 5 | +import { useAsyncCallback, AsyncStatus } from './useAsyncCallback'; |
| 6 | + |
| 7 | +describe('useAsyncCallback', () => { |
| 8 | + it('starts in Idle state', () => { |
| 9 | + const { result } = renderHook(() => useAsyncCallback(async () => 'value')); |
| 10 | + const [state] = result.current; |
| 11 | + expect(state.status).toBe(AsyncStatus.Idle); |
| 12 | + }); |
| 13 | + |
| 14 | + it('transitions to Success with returned data', async () => { |
| 15 | + const { result } = renderHook(() => useAsyncCallback(async () => 42)); |
| 16 | + |
| 17 | + await act(async () => { |
| 18 | + await result.current[1](); |
| 19 | + }); |
| 20 | + |
| 21 | + expect(result.current[0]).toEqual({ status: AsyncStatus.Success, data: 42 }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('transitions to Error when the async function throws', async () => { |
| 25 | + const boom = new Error('boom'); |
| 26 | + const { result } = renderHook(() => |
| 27 | + useAsyncCallback(async () => { |
| 28 | + throw boom; |
| 29 | + }) |
| 30 | + ); |
| 31 | + |
| 32 | + await act(async () => { |
| 33 | + await result.current[1]().catch(() => {}); |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result.current[0]).toEqual({ status: AsyncStatus.Error, error: boom }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('ignores the result of a stale (superseded) request', async () => { |
| 40 | + // Two calls are made. The first resolves AFTER the second — its result should |
| 41 | + // be discarded so the final state reflects only the second call. |
| 42 | + let resolveFirst!: (v: string) => void; |
| 43 | + let resolveSecond!: (v: string) => void; |
| 44 | + let callCount = 0; |
| 45 | + |
| 46 | + const { result } = renderHook(() => |
| 47 | + useAsyncCallback(async () => { |
| 48 | + callCount += 1; |
| 49 | + if (callCount === 1) { |
| 50 | + return new Promise<string>((res) => { |
| 51 | + resolveFirst = res; |
| 52 | + }); |
| 53 | + } |
| 54 | + return new Promise<string>((res) => { |
| 55 | + resolveSecond = res; |
| 56 | + }); |
| 57 | + }) |
| 58 | + ); |
| 59 | + |
| 60 | + // Fire both requests before either resolves |
| 61 | + act(() => { |
| 62 | + result.current[1](); |
| 63 | + }); |
| 64 | + act(() => { |
| 65 | + result.current[1](); |
| 66 | + }); |
| 67 | + |
| 68 | + // Resolve the stale first request — its result should be ignored |
| 69 | + await act(async () => { |
| 70 | + resolveFirst('stale'); |
| 71 | + await Promise.resolve(); |
| 72 | + }); |
| 73 | + |
| 74 | + // Resolve the fresh second request — this should be the final state |
| 75 | + await act(async () => { |
| 76 | + resolveSecond('fresh'); |
| 77 | + await Promise.resolve(); |
| 78 | + }); |
| 79 | + |
| 80 | + const successStates = result.current[0]; |
| 81 | + expect(successStates.status).toBe(AsyncStatus.Success); |
| 82 | + if (successStates.status === AsyncStatus.Success) { |
| 83 | + expect(successStates.data).toBe('fresh'); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + it('does not call setState after the component unmounts', async () => { |
| 88 | + let resolveAfterUnmount!: (v: string) => void; |
| 89 | + const stateChanges: string[] = []; |
| 90 | + |
| 91 | + const { result, unmount } = renderHook(() => |
| 92 | + useAsyncCallback( |
| 93 | + async () => |
| 94 | + new Promise<string>((res) => { |
| 95 | + resolveAfterUnmount = res; |
| 96 | + }) |
| 97 | + ) |
| 98 | + ); |
| 99 | + |
| 100 | + // Track state changes via the third returned setter |
| 101 | + const [, callback, setState] = result.current; |
| 102 | + const originalSetState = setState; |
| 103 | + // Patch setState to record calls |
| 104 | + result.current[2] = (s) => { |
| 105 | + stateChanges.push(typeof s === 'function' ? 'fn' : s.status); |
| 106 | + originalSetState(s); |
| 107 | + }; |
| 108 | + |
| 109 | + act(() => { |
| 110 | + callback(); |
| 111 | + }); |
| 112 | + |
| 113 | + unmount(); |
| 114 | + |
| 115 | + // Resolve after unmount — alive() returns false, so state should NOT be updated |
| 116 | + await act(async () => { |
| 117 | + resolveAfterUnmount('late'); |
| 118 | + await Promise.resolve(); |
| 119 | + }); |
| 120 | + |
| 121 | + // Only the Loading state (queued before unmount) may have been emitted; |
| 122 | + // Success must not appear after unmount. |
| 123 | + const successCalls = stateChanges.filter((s) => s === AsyncStatus.Success); |
| 124 | + expect(successCalls).toHaveLength(0); |
| 125 | + }); |
| 126 | +}); |
0 commit comments