78 lines
2.1 KiB
TypeScript
78 lines
2.1 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) {
|
|
return {
|
|
request: {
|
|
json: () => Promise.resolve(body)
|
|
},
|
|
fetch: vi.fn()
|
|
} 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 an 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('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 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();
|
|
});
|
|
});
|