pingPostgrest.test.ts41 lines · main
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2
3import pingPostgrest from './pingPostgrest'
4import * as fetchModule from '@/data/fetchers'
5
6vi.mock('./constants', () => ({ API_URL: 'https://api.example.com' }))
7
8describe('pingPostgrest', () => {
9 beforeEach(() => {
10 vi.restoreAllMocks()
11 })
12
13 it('returns true if fetchHeadWithTimeout returns no error', async () => {
14 vi.spyOn(fetchModule, 'fetchHeadWithTimeout').mockResolvedValue({ error: undefined })
15 const result = await pingPostgrest('my-project')
16 expect(result).toBe(true)
17 })
18
19 it('returns false if fetchHeadWithTimeout returns an error', async () => {
20 vi.spyOn(fetchModule, 'fetchHeadWithTimeout').mockResolvedValue({ error: { message: 'fail' } })
21 const result = await pingPostgrest('my-project')
22 expect(result).toBe(false)
23 })
24
25 it('returns false if projectRef is undefined', async () => {
26 const result = await pingPostgrest(undefined as any)
27 expect(result).toBe(false)
28 })
29
30 it('passes timeout option to fetchHeadWithTimeout', async () => {
31 const spy = vi
32 .spyOn(fetchModule, 'fetchHeadWithTimeout')
33 .mockResolvedValue({ error: undefined })
34 await pingPostgrest('my-project', { timeout: 1234 })
35 expect(spy).toHaveBeenCalledWith(
36 'https://api.example.com/projects/my-project/api/rest',
37 [],
38 expect.objectContaining({ timeout: 1234 })
39 )
40 })
41})