|
| 1 | +// Jest globals are available without import in this environment |
| 2 | +import fetch from 'cross-fetch' |
| 3 | +import { createTestDataContext } from '../helper' |
| 4 | +import { UtilDataSource } from '../../../src/sources/UtilDataSource' |
| 5 | +import { DataContext } from '../../../src' |
| 6 | +import { strictAgent } from '@packages/network' |
| 7 | + |
| 8 | +// Mock cross-fetch |
| 9 | +jest.mock('cross-fetch') |
| 10 | +const mockedFetch = jest.mocked(fetch) |
| 11 | + |
| 12 | +describe('UtilDataSource', () => { |
| 13 | + let ctx: DataContext |
| 14 | + let utilDataSource: UtilDataSource |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + ctx = createTestDataContext('open') |
| 18 | + utilDataSource = new UtilDataSource(ctx) |
| 19 | + |
| 20 | + // Reset all mocks |
| 21 | + jest.clearAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + ctx.destroy() |
| 26 | + }) |
| 27 | + |
| 28 | + describe('#fetch', () => { |
| 29 | + it('calls fetch with strictAgent and provided options', async () => { |
| 30 | + const mockResponse = { |
| 31 | + ok: true, |
| 32 | + status: 200, |
| 33 | + json: jest.fn().mockResolvedValue({ data: 'test' }), |
| 34 | + } as any |
| 35 | + |
| 36 | + mockedFetch.mockResolvedValue(mockResponse) |
| 37 | + |
| 38 | + const url = 'https://example.com/api' |
| 39 | + const init = { method: 'POST', body: 'test' } |
| 40 | + |
| 41 | + const result = await utilDataSource.fetch(url, init) |
| 42 | + |
| 43 | + expect(mockedFetch).toHaveBeenCalledWith(url, { |
| 44 | + agent: strictAgent, |
| 45 | + method: 'POST', |
| 46 | + body: 'test', |
| 47 | + }) |
| 48 | + |
| 49 | + expect(result).toBe(mockResponse) |
| 50 | + }) |
| 51 | + |
| 52 | + it('calls fetch with only strictAgent when no init options provided', async () => { |
| 53 | + const mockResponse = { |
| 54 | + ok: true, |
| 55 | + status: 200, |
| 56 | + json: jest.fn().mockResolvedValue({ data: 'test' }), |
| 57 | + } as any |
| 58 | + |
| 59 | + mockedFetch.mockResolvedValue(mockResponse) |
| 60 | + |
| 61 | + const url = 'https://example.com/api' |
| 62 | + |
| 63 | + const result = await utilDataSource.fetch(url) |
| 64 | + |
| 65 | + expect(mockedFetch).toHaveBeenCalledWith(url, { |
| 66 | + agent: expect.any(Object), // strictAgent |
| 67 | + }) |
| 68 | + |
| 69 | + expect(result).toBe(mockResponse) |
| 70 | + }) |
| 71 | + |
| 72 | + it('merges init options with agent configuration', async () => { |
| 73 | + const mockResponse = { |
| 74 | + ok: true, |
| 75 | + status: 200, |
| 76 | + } as any |
| 77 | + |
| 78 | + mockedFetch.mockResolvedValue(mockResponse) |
| 79 | + |
| 80 | + const url = 'https://example.com/api' |
| 81 | + const init = { |
| 82 | + method: 'PUT', |
| 83 | + headers: { 'Content-Type': 'application/json' }, |
| 84 | + body: JSON.stringify({ test: 'data' }), |
| 85 | + } |
| 86 | + |
| 87 | + await utilDataSource.fetch(url, init) |
| 88 | + |
| 89 | + expect(mockedFetch).toHaveBeenCalledWith(url, { |
| 90 | + agent: strictAgent, |
| 91 | + method: 'PUT', |
| 92 | + headers: { 'Content-Type': 'application/json' }, |
| 93 | + body: JSON.stringify({ test: 'data' }), |
| 94 | + }) |
| 95 | + }) |
| 96 | + }) |
| 97 | +}) |
0 commit comments