-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72b4d62
commit 6b2136b
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getServerSession } from 'next-auth/next'; | ||
import { authOptions } from '@/lib/auth'; | ||
import { handleError } from '@/lib/errorHandler'; | ||
import { dfdaGET, dfdaPOST } from '@/lib/dfda'; | ||
import { GET, POST } from 'apps/nextjs/app/api/dfda/[dfdaPath]/route'; | ||
|
||
jest.mock('next-auth/next'); | ||
jest.mock('@/lib/dfda'); | ||
|
||
describe('GET /api/dfda/[dfdaPath]', () => { | ||
it('should return 200 with data on successful request', async () => { | ||
// Mock dependencies | ||
(getServerSession as jest.Mock).mockResolvedValueOnce({ user: { id: 'user-id' } }); | ||
(dfdaGET as jest.Mock).mockResolvedValueOnce({ data: { key: 'value' } }); | ||
|
||
// Call GET and check response | ||
const response = await GET(new Request('https://example.com/api/dfda/test'), { params: { dfdaPath: 'test' } }); | ||
expect(response.status).toBe(200); | ||
expect(response.headers.get('Content-Type')).toBe('application/json'); | ||
expect(await response.json()).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should return error response on failed request', async () => { | ||
// Mock error | ||
(getServerSession as jest.Mock).mockResolvedValueOnce({ user: { id: 'user-id' } }); | ||
(dfdaGET as jest.Mock).mockRejectedValueOnce(new Error('Request failed')); | ||
|
||
// Call GET and check error response | ||
const response = await GET(new Request('https://example.com/api/dfda/test'), { params: { dfdaPath: 'test' } }); | ||
expect(response.status).toBe(500); | ||
}); | ||
}); | ||
|
||
describe('POST /api/dfda/[dfdaPath]', () => { | ||
it('should return 200 with data on successful request', async () => { | ||
// Mock dependencies | ||
(getServerSession as jest.Mock).mockResolvedValueOnce({ user: { id: 'user-id' } }); | ||
(dfdaPOST as jest.Mock).mockResolvedValueOnce({ data: { key: 'value' }, status: 200 }); | ||
|
||
// Call POST and check response | ||
const response = await POST(new Request('https://example.com/api/dfda/test'), { params: { dfdaPath: 'test' } }); | ||
expect(response.status).toBe(200); | ||
expect(response.headers.get('Content-Type')).toBe('application/json'); | ||
expect(await response.json()).toEqual({ key: 'value' }); | ||
}); | ||
|
||
it('should return error response on failed request', async () => { | ||
// Mock error | ||
(getServerSession as jest.Mock).mockResolvedValueOnce({ user: { id: 'user-id' } }); | ||
(dfdaPOST as jest.Mock).mockRejectedValueOnce(new Error('Request failed')); | ||
|
||
// Call POST and check error response | ||
const response = await POST(new Request('https://example.com/api/dfda/test'), { params: { dfdaPath: 'test' } }); | ||
expect(response.status).toBe(500); | ||
}); | ||
}); |