|
| 1 | +import { resolveFetch } from '../src/lib/helpers' |
| 2 | + |
| 3 | +describe('resolveFetch', () => { |
| 4 | + const TEST_URL = 'https://example.com' |
| 5 | + const TEST_OPTIONS = { method: 'GET' } |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Reset any mocks between tests |
| 9 | + jest.resetModules() |
| 10 | + jest.clearAllMocks() |
| 11 | + }) |
| 12 | + |
| 13 | + it('should use custom fetch if provided', async () => { |
| 14 | + const customFetch = jest.fn() |
| 15 | + const resolvedFetch = resolveFetch(customFetch) |
| 16 | + |
| 17 | + await resolvedFetch(TEST_URL, TEST_OPTIONS) |
| 18 | + |
| 19 | + expect(customFetch).toHaveBeenCalledTimes(1) |
| 20 | + expect(customFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should use global fetch if no custom fetch is provided', async () => { |
| 24 | + const globalFetch = jest.fn() |
| 25 | + global.fetch = globalFetch |
| 26 | + const resolvedFetch = resolveFetch() |
| 27 | + |
| 28 | + await resolvedFetch(TEST_URL, TEST_OPTIONS) |
| 29 | + |
| 30 | + expect(globalFetch).toHaveBeenCalledTimes(1) |
| 31 | + expect(globalFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) |
| 32 | + }) |
| 33 | + |
| 34 | + it('should use node-fetch if global fetch is not available', async () => { |
| 35 | + const nodeFetch = jest.fn() |
| 36 | + jest.mock('@supabase/node-fetch', () => nodeFetch) |
| 37 | + |
| 38 | + global.fetch = undefined as any |
| 39 | + const resolvedFetch = resolveFetch() |
| 40 | + |
| 41 | + await resolvedFetch(TEST_URL, TEST_OPTIONS) |
| 42 | + |
| 43 | + expect(nodeFetch).toHaveBeenCalledTimes(1) |
| 44 | + expect(nodeFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) |
| 45 | + }) |
| 46 | +}) |
0 commit comments