Skip to content

Commit

Permalink
feat: Updated 1 files
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Apr 28, 2024
1 parent 72b4d62 commit 6b2136b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions apps/nextjs/app/api/dfda/[dfdaPath]/route.test.ts
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);
});
});

0 comments on commit 6b2136b

Please sign in to comment.