Some checks failed
Dev Deployment (Zero Downtime) / deploy-dev (push) Failing after 10m16s
70 lines
1.5 KiB
TypeScript
70 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { GET } from '@/app/api/hobbies/route';
|
|
import { getHobbies } from '@/lib/directus';
|
|
|
|
jest.mock('@/lib/directus', () => ({
|
|
getHobbies: jest.fn(),
|
|
}));
|
|
|
|
jest.mock('next/server', () => ({
|
|
NextRequest: jest.fn((url) => ({
|
|
url,
|
|
})),
|
|
NextResponse: {
|
|
json: jest.fn((data, options) => ({
|
|
json: async () => data,
|
|
status: options?.status || 200,
|
|
})),
|
|
},
|
|
}));
|
|
|
|
describe('GET /api/hobbies', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should return hobbies from Directus', async () => {
|
|
const mockHobbies = [
|
|
{
|
|
id: '1',
|
|
key: 'coding',
|
|
icon: 'Code',
|
|
title: 'Coding',
|
|
description: 'I love coding',
|
|
},
|
|
];
|
|
|
|
(getHobbies as jest.Mock).mockResolvedValue(mockHobbies);
|
|
|
|
const request = {
|
|
url: 'http://localhost/api/hobbies?locale=en',
|
|
} as any;
|
|
|
|
await GET(request);
|
|
|
|
expect(NextResponse.json).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
hobbies: mockHobbies,
|
|
source: 'directus',
|
|
})
|
|
);
|
|
});
|
|
|
|
it('should return fallback when no hobbies found', async () => {
|
|
(getHobbies as jest.Mock).mockResolvedValue(null);
|
|
|
|
const request = {
|
|
url: 'http://localhost/api/hobbies?locale=en',
|
|
} as any;
|
|
|
|
await GET(request);
|
|
|
|
expect(NextResponse.json).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
hobbies: null,
|
|
source: 'fallback',
|
|
})
|
|
);
|
|
});
|
|
});
|