Adds mock for the new GET /api/persons/{id}/aliases call added
in the parallel Promise.all.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
114 lines
4.4 KiB
TypeScript
114 lines
4.4 KiB
TypeScript
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;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const mockLocals = { user: { groups: [{ permissions: ['READ_ALL'] }] } } as any;
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const mockLocalsWriter = { user: { groups: [{ permissions: ['WRITE_ALL'] }] } } as any;
|
|
|
|
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: [] })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
const result = await load({ params: { id: 'p1' }, fetch: mockFetch, locals: mockLocals });
|
|
|
|
expect(result.person.firstName).toBe('Hans');
|
|
expect(result.sentDocuments).toHaveLength(1);
|
|
expect(result.receivedDocuments).toEqual([]);
|
|
});
|
|
|
|
it('returns canWrite=true when user has WRITE_ALL', async () => {
|
|
vi.mocked(createApiClient).mockReturnValue({
|
|
GET: vi
|
|
.fn()
|
|
.mockResolvedValueOnce({
|
|
response: { ok: true, status: 200 },
|
|
data: { id: 'p1', firstName: 'Anna', lastName: 'Schmidt' }
|
|
})
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
const result = await load({ params: { id: 'p1' }, fetch: mockFetch, locals: mockLocalsWriter });
|
|
|
|
expect(result.canWrite).toBe(true);
|
|
});
|
|
|
|
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 })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
const result = await load({ params: { id: 'p1' }, fetch: mockFetch, locals: mockLocals });
|
|
|
|
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: [] })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
await expect(
|
|
load({ params: { id: 'missing' }, fetch: mockFetch, locals: mockLocals })
|
|
).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: [] })
|
|
.mockResolvedValueOnce({ response: { ok: true }, data: [] })
|
|
} as ReturnType<typeof createApiClient>);
|
|
|
|
await expect(
|
|
load({ params: { id: 'forbidden' }, fetch: mockFetch, locals: mockLocals })
|
|
).rejects.toMatchObject({
|
|
status: 403
|
|
});
|
|
});
|
|
});
|