|
| 1 | +// intent-api.test.ts |
| 2 | +import { describe, it, expect, jest } from '@jest/globals'; |
| 3 | +import { IntentApiImpl, type IntentSubmissionParams } from './intent-api'; // adjust if needed |
| 4 | +import type { FetchFunction } from './types'; // adjust if needed |
| 5 | + |
| 6 | +describe('IntentApiImpl', () => { |
| 7 | + const baseUrl = 'https://example.com/api'; |
| 8 | + |
| 9 | + const makeParams = (): IntentSubmissionParams => ({ |
| 10 | + srcChainId: '1', |
| 11 | + quoteId: 'quote-123', |
| 12 | + signature: '0xsig', |
| 13 | + order: { some: 'payload' }, |
| 14 | + userAddress: '0xabc', |
| 15 | + aggregatorId: 'agg-1', |
| 16 | + }); |
| 17 | + |
| 18 | + // Key part: strongly type the mock as FetchFunction (returns Promise<unknown>) |
| 19 | + const makeFetchMock = () => |
| 20 | + jest.fn<ReturnType<FetchFunction>, Parameters<FetchFunction>>(); |
| 21 | + |
| 22 | + it('submitIntent calls POST /submitOrder with JSON body and returns response', async () => { |
| 23 | + const fetchFn = makeFetchMock().mockResolvedValue({ ok: true, id: 'resp' }); |
| 24 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 25 | + |
| 26 | + const params = makeParams(); |
| 27 | + const result = await api.submitIntent(params); |
| 28 | + |
| 29 | + expect(result).toEqual({ ok: true, id: 'resp' }); |
| 30 | + expect(fetchFn).toHaveBeenCalledTimes(1); |
| 31 | + expect(fetchFn).toHaveBeenCalledWith(`${baseUrl}/submitOrder`, { |
| 32 | + method: 'POST', |
| 33 | + headers: { 'Content-Type': 'application/json' }, |
| 34 | + body: JSON.stringify(params), |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('submitIntent rethrows Errors with a prefixed message', async () => { |
| 39 | + const fetchFn = makeFetchMock().mockRejectedValue(new Error('boom')); |
| 40 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 41 | + |
| 42 | + await expect(api.submitIntent(makeParams())).rejects.toThrow( |
| 43 | + 'Failed to submit intent: boom', |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('submitIntent returns null when rejection is not an Error', async () => { |
| 48 | + const fetchFn = makeFetchMock().mockRejectedValue('boom'); |
| 49 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 50 | + |
| 51 | + await expect(api.submitIntent(makeParams())).resolves.toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('getOrderStatus calls GET /getOrderStatus with encoded query params and returns response', async () => { |
| 55 | + const fetchFn = makeFetchMock().mockResolvedValue({ status: 'filled' }); |
| 56 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 57 | + |
| 58 | + const orderId = 'order-1'; |
| 59 | + const aggregatorId = 'My Agg/With Spaces'; |
| 60 | + const srcChainId = '10'; |
| 61 | + |
| 62 | + const result = await api.getOrderStatus(orderId, aggregatorId, srcChainId); |
| 63 | + |
| 64 | + expect(result).toEqual({ status: 'filled' }); |
| 65 | + expect(fetchFn).toHaveBeenCalledTimes(1); |
| 66 | + |
| 67 | + const expectedEndpoint = |
| 68 | + `${baseUrl}/getOrderStatus` + |
| 69 | + `?orderId=${orderId}` + |
| 70 | + `&aggregatorId=${encodeURIComponent(aggregatorId)}` + |
| 71 | + `&srcChainId=${srcChainId}`; |
| 72 | + |
| 73 | + expect(fetchFn).toHaveBeenCalledWith(expectedEndpoint, { method: 'GET' }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('getOrderStatus rethrows Errors with a prefixed message', async () => { |
| 77 | + const fetchFn = makeFetchMock().mockRejectedValue(new Error('nope')); |
| 78 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 79 | + |
| 80 | + await expect(api.getOrderStatus('o', 'a', '1')).rejects.toThrow( |
| 81 | + 'Failed to get order status: nope', |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('getOrderStatus returns null when rejection is not an Error', async () => { |
| 86 | + const fetchFn = makeFetchMock().mockRejectedValue({ message: 'nope' }); |
| 87 | + const api = new IntentApiImpl(baseUrl, fetchFn); |
| 88 | + |
| 89 | + await expect(api.getOrderStatus('o', 'a', '1')).resolves.toBeNull(); |
| 90 | + }); |
| 91 | +}); |
0 commit comments