Files
supabase/apps/studio/lib/pingPostgrest.test.ts
Joshen Lim dbb413beeb Chore/deprecate lib common fetch 03 (#36529)
* Deprecate use of getWithTimeout, refactor BuildingState and RestoringState to use RQ

* Refactor profile-create-mutation to use data/fetchers, and edge-function-status-query to use fetch

* Shift post from lib/common/fetch, refactor bucket-object-download-mutation

* Address feedback

* Minor fix

* Refactor post calls from lib/common/fetch in auth pages to data/fetchers

* Add missing POST users endpoint + small fix when deleting user via context menu

* Remove all use of any imports from lib/common/fetch

* Clean up remaining usage of lib/common/fetch

* Fix fetchHeadWithTimeout

* simplify handleFetchError

* allow handleFetchError to accept unknown

* non-breaking change

* small fixes

* fix query path

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
2025-07-21 16:59:17 +08:00

41 lines
1.4 KiB
TypeScript

import * as fetchModule from 'data/fetchers'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import pingPostgrest from './pingPostgrest'
vi.mock('./constants', () => ({ API_URL: 'https://api.example.com' }))
describe('pingPostgrest', () => {
beforeEach(() => {
vi.restoreAllMocks()
})
it('returns true if fetchHeadWithTimeout returns no error', async () => {
vi.spyOn(fetchModule, 'fetchHeadWithTimeout').mockResolvedValue({ error: undefined })
const result = await pingPostgrest('my-project')
expect(result).toBe(true)
})
it('returns false if fetchHeadWithTimeout returns an error', async () => {
vi.spyOn(fetchModule, 'fetchHeadWithTimeout').mockResolvedValue({ error: { message: 'fail' } })
const result = await pingPostgrest('my-project')
expect(result).toBe(false)
})
it('returns false if projectRef is undefined', async () => {
const result = await pingPostgrest(undefined as any)
expect(result).toBe(false)
})
it('passes timeout option to fetchHeadWithTimeout', async () => {
const spy = vi
.spyOn(fetchModule, 'fetchHeadWithTimeout')
.mockResolvedValue({ error: undefined })
await pingPostgrest('my-project', { timeout: 1234 })
expect(spy).toHaveBeenCalledWith(
'https://api.example.com/projects/my-project/api/rest',
[],
expect.objectContaining({ timeout: 1234 })
)
})
})