pingPostgrest.test.ts 1.4 KB

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