test(#123): add Vitest integration tests for SvelteKit load functions
Adds server-project spec files for the four priority routes: - routes/+page.server (home/search) — happy path, 401 redirect, network error fallback - routes/documents/[id]/+page.server — happy path, comments fetch failure, 401/403/404 - routes/persons/[id]/+page.server — happy path, partial API failure, 403/404 - routes/admin/+page.server — ADMIN permission gate (none/read-only/undefined/no groups) All tests run in Node environment with vi.mock() for createApiClient and $env/dynamic/private. No real network calls; total suite runs in < 1 second. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
83
frontend/src/routes/persons/[id]/page.server.spec.ts
Normal file
83
frontend/src/routes/persons/[id]/page.server.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
||||
import { load } from './+page.server';
|
||||
|
||||
vi.mock('$lib/api.server', () => ({ createApiClient: vi.fn() }));
|
||||
|
||||
import { createApiClient } from '$lib/api.server';
|
||||
|
||||
const mockFetch = vi.fn() as unknown as typeof fetch;
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
// ─── happy path ───────────────────────────────────────────────────────────────
|
||||
|
||||
describe('person detail load — happy path', () => {
|
||||
it('returns person, sentDocuments, and receivedDocuments on success', async () => {
|
||||
vi.mocked(createApiClient).mockReturnValue({
|
||||
GET: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
response: { ok: true, status: 200 },
|
||||
data: { id: 'p1', firstName: 'Hans', lastName: 'Müller' }
|
||||
})
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [{ id: 'd1', title: 'Brief' }] })
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
||||
} as ReturnType<typeof createApiClient>);
|
||||
|
||||
const result = await load({ params: { id: 'p1' }, fetch: mockFetch });
|
||||
|
||||
expect(result.person.firstName).toBe('Hans');
|
||||
expect(result.sentDocuments).toHaveLength(1);
|
||||
expect(result.receivedDocuments).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns empty arrays when sent/received document APIs fail', async () => {
|
||||
vi.mocked(createApiClient).mockReturnValue({
|
||||
GET: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({
|
||||
response: { ok: true, status: 200 },
|
||||
data: { id: 'p1', firstName: 'Anna', lastName: 'Schmidt' }
|
||||
})
|
||||
.mockResolvedValueOnce({ response: { ok: false }, data: null })
|
||||
.mockResolvedValueOnce({ response: { ok: false }, data: null })
|
||||
} as ReturnType<typeof createApiClient>);
|
||||
|
||||
const result = await load({ params: { id: 'p1' }, fetch: mockFetch });
|
||||
|
||||
expect(result.sentDocuments).toEqual([]);
|
||||
expect(result.receivedDocuments).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── error paths ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe('person detail load — error paths', () => {
|
||||
it('throws 404 when person does not exist', async () => {
|
||||
vi.mocked(createApiClient).mockReturnValue({
|
||||
GET: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ response: { ok: false, status: 404 }, error: null })
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
||||
} as ReturnType<typeof createApiClient>);
|
||||
|
||||
await expect(load({ params: { id: 'missing' }, fetch: mockFetch })).rejects.toMatchObject({
|
||||
status: 404
|
||||
});
|
||||
});
|
||||
|
||||
it('throws 403 when person is not accessible', async () => {
|
||||
vi.mocked(createApiClient).mockReturnValue({
|
||||
GET: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ response: { ok: false, status: 403 }, error: null })
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
||||
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
||||
} as ReturnType<typeof createApiClient>);
|
||||
|
||||
await expect(load({ params: { id: 'forbidden' }, fetch: mockFetch })).rejects.toMatchObject({
|
||||
status: 403
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user