31 lines
968 B
JavaScript
31 lines
968 B
JavaScript
|
|
import { describe, it, expect } from 'vitest'
|
||
|
|
import { server, http, HttpResponse } from './test-setup'
|
||
|
|
import { getPersonsInHome, searchPersons } from '@/api/sdk'
|
||
|
|
|
||
|
|
// Persons API tests
|
||
|
|
|
||
|
|
describe('persons api (typed client)', () => {
|
||
|
|
it('lists persons in home', async () => {
|
||
|
|
server.use(
|
||
|
|
http.get('*/api/v1/persons', () => {
|
||
|
|
return HttpResponse.json([{ id: 1, name: 'Ada Lovelace' }])
|
||
|
|
})
|
||
|
|
)
|
||
|
|
const page = await getPersonsInHome()
|
||
|
|
expect(Array.isArray(page.items)).toBe(true)
|
||
|
|
expect(page.items[0].name).toBe('Ada Lovelace')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('searches persons by name', async () => {
|
||
|
|
server.use(
|
||
|
|
http.get('*/api/v1/persons', ({ request }) => {
|
||
|
|
const url = new URL(request.url)
|
||
|
|
const q = url.searchParams.get('q')
|
||
|
|
return HttpResponse.json(q ? [{ id: 2, name: 'Alan Turing' }] : [])
|
||
|
|
})
|
||
|
|
)
|
||
|
|
const page = await searchPersons('alan')
|
||
|
|
expect(page.items[0].name.toLowerCase()).toContain('alan')
|
||
|
|
})
|
||
|
|
})
|