Files
mealprep/frontend/src/routes/(app)/recipes/page.server.test.ts
2026-04-03 09:45:43 +02:00

46 lines
1.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('$env/dynamic/private', () => ({
env: { BACKEND_URL: 'http://localhost:8080' }
}));
const mockGet = vi.fn();
vi.mock('$lib/server/api', () => ({
apiClient: () => ({ GET: mockGet })
}));
describe('recipe library page — load', () => {
let load: any;
beforeEach(async () => {
mockGet.mockReset();
vi.resetModules();
const mod = await import('./+page.server');
load = mod.load;
});
const mockRecipes = [
{ id: 'r1', name: 'Spaghetti', cookTimeMin: 30, effort: 'Easy' },
{ id: 'r2', name: 'Curry', cookTimeMin: 45, effort: 'Medium' }
];
it('fetches recipes from GET /v1/recipes', async () => {
mockGet.mockResolvedValue({ data: { data: mockRecipes }, error: undefined });
await load({ fetch: vi.fn() } as any);
expect(mockGet).toHaveBeenCalledWith('/v1/recipes', expect.any(Object));
});
it('returns recipes in data', async () => {
mockGet.mockResolvedValue({ data: { data: mockRecipes }, error: undefined });
const result = await load({ fetch: vi.fn() } as any);
expect(result.recipes).toHaveLength(2);
expect(result.recipes[0].name).toBe('Spaghetti');
});
it('returns empty array when API fails', async () => {
mockGet.mockResolvedValue({ data: undefined, error: { status: 500 } });
const result = await load({ fetch: vi.fn() } as any);
expect(result.recipes).toEqual([]);
});
});