|
| 1 | +import React from 'react'; |
| 2 | +import { describe, test, expect, mock } from 'bun:test'; |
| 3 | +import { render } from 'ink-testing-library'; |
| 4 | +import { Text } from 'ink'; |
| 5 | +import { useApi } from '../../src/hooks/use-api.js'; |
| 6 | + |
| 7 | +/** Tiny component that exposes useApi state as text for assertion */ |
| 8 | +function TestHarness({ apiFn, immediate = false }: { apiFn: (...args: any[]) => Promise<any>; immediate?: boolean }) { |
| 9 | + const { data, loading, error, queryTime, execute, retry } = useApi(apiFn, immediate); |
| 10 | + // Stash execute/retry on a global so tests can call them |
| 11 | + (globalThis as any).__testExecute = execute; |
| 12 | + (globalThis as any).__testRetry = retry; |
| 13 | + return React.createElement( |
| 14 | + Text, |
| 15 | + null, |
| 16 | + JSON.stringify({ data, loading, error, queryTime: queryTime !== null ? 'set' : null }), |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +function parseState(frame: string | undefined) { |
| 21 | + return JSON.parse(frame ?? '{}'); |
| 22 | +} |
| 23 | + |
| 24 | +const tick = (ms = 50) => new Promise((r) => setTimeout(r, ms)); |
| 25 | + |
| 26 | +describe('useApi', () => { |
| 27 | + test('initial state is idle', () => { |
| 28 | + const apiFn = mock(() => Promise.resolve('ok')); |
| 29 | + const { lastFrame } = render( |
| 30 | + React.createElement(TestHarness, { apiFn }), |
| 31 | + ); |
| 32 | + const state = parseState(lastFrame()); |
| 33 | + expect(state.data).toBeNull(); |
| 34 | + expect(state.loading).toBe(false); |
| 35 | + expect(state.error).toBeNull(); |
| 36 | + }); |
| 37 | + |
| 38 | + test('immediate=true triggers execute on mount', async () => { |
| 39 | + const apiFn = mock(() => Promise.resolve({ hello: 'world' })); |
| 40 | + const { lastFrame } = render( |
| 41 | + React.createElement(TestHarness, { apiFn, immediate: true }), |
| 42 | + ); |
| 43 | + await tick(); |
| 44 | + const state = parseState(lastFrame()); |
| 45 | + expect(state.data).toEqual({ hello: 'world' }); |
| 46 | + expect(state.loading).toBe(false); |
| 47 | + expect(state.queryTime).toBe('set'); |
| 48 | + expect(apiFn).toHaveBeenCalledTimes(1); |
| 49 | + }); |
| 50 | + |
| 51 | + test('execute resolves with data', async () => { |
| 52 | + const apiFn = mock(() => Promise.resolve('result')); |
| 53 | + const { lastFrame } = render( |
| 54 | + React.createElement(TestHarness, { apiFn }), |
| 55 | + ); |
| 56 | + |
| 57 | + (globalThis as any).__testExecute('arg1'); |
| 58 | + await tick(); |
| 59 | + |
| 60 | + const state = parseState(lastFrame()); |
| 61 | + expect(state.data).toBe('result'); |
| 62 | + expect(state.loading).toBe(false); |
| 63 | + expect(state.error).toBeNull(); |
| 64 | + }); |
| 65 | + |
| 66 | + test('execute sets error on rejection', async () => { |
| 67 | + const apiFn = mock(() => Promise.reject(new Error('boom'))); |
| 68 | + const { lastFrame } = render( |
| 69 | + React.createElement(TestHarness, { apiFn }), |
| 70 | + ); |
| 71 | + |
| 72 | + (globalThis as any).__testExecute(); |
| 73 | + await tick(); |
| 74 | + |
| 75 | + const state = parseState(lastFrame()); |
| 76 | + expect(state.error).toBe('boom'); |
| 77 | + expect(state.loading).toBe(false); |
| 78 | + expect(state.queryTime).toBe('set'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('retry re-executes with last args', async () => { |
| 82 | + let callCount = 0; |
| 83 | + const apiFn = mock((...args: any[]) => { |
| 84 | + callCount++; |
| 85 | + if (callCount === 1) return Promise.reject(new Error('fail')); |
| 86 | + return Promise.resolve(`ok-${args[0]}`); |
| 87 | + }); |
| 88 | + const { lastFrame } = render( |
| 89 | + React.createElement(TestHarness, { apiFn }), |
| 90 | + ); |
| 91 | + |
| 92 | + // First call fails |
| 93 | + (globalThis as any).__testExecute('myarg'); |
| 94 | + await tick(); |
| 95 | + expect(parseState(lastFrame()).error).toBe('fail'); |
| 96 | + |
| 97 | + // Retry should reuse 'myarg' |
| 98 | + (globalThis as any).__testRetry(); |
| 99 | + await tick(); |
| 100 | + const state = parseState(lastFrame()); |
| 101 | + expect(state.data).toBe('ok-myarg'); |
| 102 | + expect(state.error).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + test('handles non-Error rejection', async () => { |
| 106 | + const apiFn = mock(() => Promise.reject('string error')); |
| 107 | + const { lastFrame } = render( |
| 108 | + React.createElement(TestHarness, { apiFn }), |
| 109 | + ); |
| 110 | + |
| 111 | + (globalThis as any).__testExecute(); |
| 112 | + await tick(); |
| 113 | + |
| 114 | + const state = parseState(lastFrame()); |
| 115 | + expect(state.error).toBe('string error'); |
| 116 | + }); |
| 117 | +}); |
0 commit comments