94 lines
2.7 KiB
TypeScript
94 lines
2.7 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('$env/dynamic/private', () => ({
|
|
env: { BACKEND_URL: 'http://localhost:8080' }
|
|
}));
|
|
|
|
const mockPatch = vi.fn();
|
|
vi.mock('$lib/server/api', () => ({
|
|
apiClient: () => ({ PATCH: mockPatch })
|
|
}));
|
|
|
|
describe('household staples PATCH handler', () => {
|
|
let PATCH: any;
|
|
|
|
beforeEach(async () => {
|
|
mockPatch.mockReset();
|
|
const mod = await import('./+server');
|
|
PATCH = mod.PATCH;
|
|
});
|
|
|
|
function createRequest(body: object, rolle: 'planer' | 'mitglied' = 'planer') {
|
|
return {
|
|
request: {
|
|
json: () => Promise.resolve(body)
|
|
},
|
|
fetch: vi.fn(),
|
|
locals: { benutzer: { rolle } }
|
|
} as any;
|
|
}
|
|
|
|
it('calls backend PATCH /v1/ingredients/{id} with isStaple', async () => {
|
|
mockPatch.mockResolvedValue({ data: {}, error: undefined });
|
|
|
|
await PATCH(createRequest({ id: 'ing-1', isStaple: true }));
|
|
|
|
expect(mockPatch).toHaveBeenCalledWith('/v1/ingredients/{id}', {
|
|
params: { path: { id: 'ing-1' } },
|
|
body: { isStaple: true }
|
|
});
|
|
});
|
|
|
|
it('returns 204 on success', async () => {
|
|
mockPatch.mockResolvedValue({ data: {}, error: undefined });
|
|
|
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: true }));
|
|
|
|
expect(response.status).toBe(204);
|
|
});
|
|
|
|
it('returns 500 when backend returns a 500 error', async () => {
|
|
mockPatch.mockResolvedValue({ data: undefined, error: { status: 500, message: 'error' } });
|
|
|
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: false }));
|
|
|
|
expect(response.status).toBe(500);
|
|
});
|
|
|
|
it('forwards backend 404 status when ingredient not found', async () => {
|
|
mockPatch.mockResolvedValue({ data: undefined, error: { status: 404 } });
|
|
|
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: false }));
|
|
|
|
expect(response.status).toBe(404);
|
|
});
|
|
|
|
it('returns 400 when id is missing', async () => {
|
|
const response = await PATCH(createRequest({ isStaple: true }));
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(mockPatch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns 400 when isStaple is missing', async () => {
|
|
const response = await PATCH(createRequest({ id: 'ing-1' }));
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(mockPatch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns 403 when caller has mitglied role', async () => {
|
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: true }, 'mitglied'));
|
|
|
|
expect(response.status).toBe(403);
|
|
expect(mockPatch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns 400 when isStaple is not a boolean', async () => {
|
|
const response = await PATCH(createRequest({ id: 'ing-1', isStaple: 'yes' }));
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(mockPatch).not.toHaveBeenCalled();
|
|
});
|
|
});
|