feat(recipes): load recipe list from API in page server
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
21
frontend/src/routes/(app)/recipes/+page.server.ts
Normal file
21
frontend/src/routes/(app)/recipes/+page.server.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
import { apiClient } from '$lib/server/api';
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ fetch }) => {
|
||||||
|
const api = apiClient(fetch);
|
||||||
|
const { data, error } = await api.GET('/v1/recipes', {});
|
||||||
|
|
||||||
|
if (error || !data?.data) {
|
||||||
|
return { recipes: [] };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
recipes: data.data.map((r) => ({
|
||||||
|
id: r.id!,
|
||||||
|
name: r.name!,
|
||||||
|
cookTimeMin: r.cookTimeMin,
|
||||||
|
effort: r.effort,
|
||||||
|
heroImageUrl: r.heroImageUrl
|
||||||
|
}))
|
||||||
|
};
|
||||||
|
};
|
||||||
45
frontend/src/routes/(app)/recipes/page.server.test.ts
Normal file
45
frontend/src/routes/(app)/recipes/page.server.test.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user