Barazo default frontend barazo.forum
at main 83 lines 2.7 kB view raw
1import { describe, it, expect } from 'vitest' 2import { getCategories, getCategoryBySlug, getTopics, ApiError } from './client' 3import { http, HttpResponse } from 'msw' 4import { server } from '@/mocks/server' 5import { mockCategories } from '@/mocks/data' 6 7const API_URL = '' 8 9describe('API client', () => { 10 describe('getCategories', () => { 11 it('returns category tree', async () => { 12 const result = await getCategories() 13 expect(result.categories).toHaveLength(mockCategories.length) 14 expect(result.categories[0]!.name).toBe('General Discussion') 15 }) 16 17 it('includes nested children', async () => { 18 const result = await getCategories() 19 const dev = result.categories.find((c) => c.slug === 'development') 20 expect(dev?.children).toHaveLength(2) 21 expect(dev?.children[0]!.name).toBe('Frontend') 22 }) 23 }) 24 25 describe('getCategoryBySlug', () => { 26 it('returns a single category with topic count', async () => { 27 const result = await getCategoryBySlug('general') 28 expect(result.name).toBe('General Discussion') 29 expect(result.topicCount).toBeGreaterThan(0) 30 }) 31 32 it('throws ApiError for unknown slug', async () => { 33 await expect(getCategoryBySlug('nonexistent')).rejects.toThrow(ApiError) 34 }) 35 }) 36 37 describe('getTopics', () => { 38 it('returns paginated topics', async () => { 39 const result = await getTopics() 40 expect(result.topics.length).toBeGreaterThan(0) 41 expect(result.topics[0]).toHaveProperty('uri') 42 expect(result.topics[0]).toHaveProperty('title') 43 }) 44 45 it('filters by category', async () => { 46 const result = await getTopics({ category: 'general' }) 47 for (const topic of result.topics) { 48 expect(topic.category).toBe('general') 49 } 50 }) 51 52 it('respects limit parameter', async () => { 53 const result = await getTopics({ limit: 2 }) 54 expect(result.topics.length).toBeLessThanOrEqual(2) 55 }) 56 }) 57 58 describe('error handling', () => { 59 it('throws ApiError on server error', async () => { 60 server.use( 61 http.get(`${API_URL}/api/categories`, () => { 62 return HttpResponse.json({ error: 'Internal server error' }, { status: 500 }) 63 }) 64 ) 65 await expect(getCategories()).rejects.toThrow(ApiError) 66 }) 67 68 it('includes status code in ApiError', async () => { 69 server.use( 70 http.get(`${API_URL}/api/categories`, () => { 71 return HttpResponse.json({ error: 'Not found' }, { status: 404 }) 72 }) 73 ) 74 try { 75 await getCategories() 76 expect.fail('Should have thrown') 77 } catch (error) { 78 expect(error).toBeInstanceOf(ApiError) 79 expect((error as ApiError).status).toBe(404) 80 } 81 }) 82 }) 83})