|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { POST } from '@/app/api/reviews/route'; |
| 3 | +import { GET } from '@/app/api/reviews/[userId]/route'; |
| 4 | +import { sql } from '@/lib/db'; |
| 5 | + |
| 6 | +// Mock the db module |
| 7 | +vi.mock('@/lib/db', () => { |
| 8 | + return { |
| 9 | + sql: vi.fn(), |
| 10 | + }; |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Reviews API', () => { |
| 14 | + beforeEach(() => { |
| 15 | + vi.clearAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('POST /api/reviews', () => { |
| 19 | + it('returns 400 for invalid rating', async () => { |
| 20 | + const req = new Request('http://localhost/api/reviews', { |
| 21 | + method: 'POST', |
| 22 | + body: JSON.stringify({ |
| 23 | + contractId: 1, |
| 24 | + reviewerId: 2, |
| 25 | + freelancerId: 3, |
| 26 | + rating: 6, // Invalid rating |
| 27 | + }), |
| 28 | + }); |
| 29 | + |
| 30 | + const response = await POST(req); |
| 31 | + expect(response.status).toBe(400); |
| 32 | + const data = await response.json(); |
| 33 | + expect(data.error).toBe('Invalid input data'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns 404 if contract not found', async () => { |
| 37 | + // Mock contract not found |
| 38 | + (sql as any).mockResolvedValueOnce([]); |
| 39 | + |
| 40 | + const req = new Request('http://localhost/api/reviews', { |
| 41 | + method: 'POST', |
| 42 | + body: JSON.stringify({ |
| 43 | + contractId: 99, |
| 44 | + reviewerId: 2, |
| 45 | + freelancerId: 3, |
| 46 | + rating: 5, |
| 47 | + }), |
| 48 | + }); |
| 49 | + |
| 50 | + const response = await POST(req); |
| 51 | + expect(response.status).toBe(404); |
| 52 | + const data = await response.json(); |
| 53 | + expect(data.error).toBe('Contract not found'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('successfully creates a verified review if contract is completed', async () => { |
| 57 | + // Mock contract status query (completed) |
| 58 | + (sql as any).mockResolvedValueOnce([{ status: 'completed', client_id: 2 }]); |
| 59 | + // Mock insert query |
| 60 | + (sql as any).mockResolvedValueOnce([{ id: 1, contract_id: 1, verified: true }]); |
| 61 | + |
| 62 | + const req = new Request('http://localhost/api/reviews', { |
| 63 | + method: 'POST', |
| 64 | + body: JSON.stringify({ |
| 65 | + contractId: 1, |
| 66 | + reviewerId: 2, |
| 67 | + freelancerId: 3, |
| 68 | + rating: 5, |
| 69 | + comment: 'Great work!', |
| 70 | + }), |
| 71 | + }); |
| 72 | + |
| 73 | + const response = await POST(req); |
| 74 | + expect(response.status).toBe(201); |
| 75 | + const data = await response.json(); |
| 76 | + expect(data.verified).toBe(true); |
| 77 | + expect(sql).toHaveBeenCalledTimes(2); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns 409 on unique constraint violation', async () => { |
| 81 | + // Mock contract status query |
| 82 | + (sql as any).mockResolvedValueOnce([{ status: 'completed', client_id: 2 }]); |
| 83 | + // Mock insert query throwing unique violation |
| 84 | + const error: any = new Error('Unique constraint'); |
| 85 | + error.code = '23505'; |
| 86 | + (sql as any).mockRejectedValueOnce(error); |
| 87 | + |
| 88 | + const req = new Request('http://localhost/api/reviews', { |
| 89 | + method: 'POST', |
| 90 | + body: JSON.stringify({ |
| 91 | + contractId: 1, |
| 92 | + reviewerId: 2, |
| 93 | + freelancerId: 3, |
| 94 | + rating: 5, |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + const response = await POST(req); |
| 99 | + expect(response.status).toBe(409); |
| 100 | + const data = await response.json(); |
| 101 | + expect(data.error).toBe('One review allowed per contract.'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('GET /api/reviews/[userId]', () => { |
| 106 | + it('returns 400 for invalid userId', async () => { |
| 107 | + const req = new Request('http://localhost/api/reviews/invalid'); |
| 108 | + const response = await GET(req, { params: Promise.resolve({ userId: 'invalid' }) }); |
| 109 | + expect(response.status).toBe(400); |
| 110 | + }); |
| 111 | + |
| 112 | + it('returns paginated reviews', async () => { |
| 113 | + // Mock fetching reviews |
| 114 | + const mockRows = [ |
| 115 | + { id: 2, contract_id: 2, rating: 4, total_count: '2' }, |
| 116 | + { id: 1, contract_id: 1, rating: 5, total_count: '2' }, |
| 117 | + ]; |
| 118 | + (sql as any).mockResolvedValueOnce(mockRows); |
| 119 | + |
| 120 | + const req = new Request('http://localhost/api/reviews/3?page=1&limit=2'); |
| 121 | + const response = await GET(req, { params: Promise.resolve({ userId: '3' }) }); |
| 122 | + expect(response.status).toBe(200); |
| 123 | + |
| 124 | + const data = await response.json(); |
| 125 | + expect(data.data).toHaveLength(2); |
| 126 | + expect(data.data[0]).not.toHaveProperty('total_count'); // ensures mapping worked |
| 127 | + expect(data.meta.totalCount).toBe(2); |
| 128 | + expect(data.meta.page).toBe(1); |
| 129 | + expect(data.meta.totalPages).toBe(1); |
| 130 | + expect(sql).toHaveBeenCalledTimes(1); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments