43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { RevolutionaryTypedFetch } from '../src/core/typed-fetch.js'
|
|
|
|
const jsonResponse = (body: any) =>
|
|
new Response(JSON.stringify(body), {
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
|
|
describe('RevolutionaryTypedFetch request polish', () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('applies a JSON content-type automatically for plain object bodies', async () => {
|
|
const fetchSpy = vi.fn(async (_url, init) => {
|
|
const headers = new Headers(init?.headers as HeadersInit)
|
|
expect(headers.get('content-type')).toBe('application/json')
|
|
return jsonResponse({ ok: true })
|
|
})
|
|
vi.stubGlobal('fetch', fetchSpy)
|
|
|
|
const client = new RevolutionaryTypedFetch({ request: { baseURL: 'https://api.test' } })
|
|
await client.post('/items', { name: 'test' })
|
|
|
|
expect(fetchSpy).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('does not dedupe concurrent requests when bodies differ', async () => {
|
|
const fetchSpy = vi.fn(async (_url, init) => {
|
|
return jsonResponse({ echoed: init?.body })
|
|
})
|
|
vi.stubGlobal('fetch', fetchSpy)
|
|
|
|
const client = new RevolutionaryTypedFetch({ request: { baseURL: 'https://api.test' } })
|
|
await Promise.all([
|
|
client.post('/items', { name: 'one' }),
|
|
client.post('/items', { name: 'two' })
|
|
])
|
|
|
|
expect(fetchSpy).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|