159 lines
4.0 KiB
TypeScript
159 lines
4.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('$env/dynamic/private', () => ({
|
|
env: { BACKEND_URL: 'http://localhost:8080' }
|
|
}));
|
|
|
|
const mockPost = vi.fn();
|
|
vi.mock('$lib/server/api', () => ({
|
|
apiClient: () => ({ POST: mockPost })
|
|
}));
|
|
|
|
describe('household setup — load', () => {
|
|
let load: any;
|
|
|
|
beforeEach(async () => {
|
|
const mod = await import('./+page.server');
|
|
load = mod.load;
|
|
});
|
|
|
|
it('redirects to /planner when user already has a household', async () => {
|
|
const event = {
|
|
locals: {
|
|
benutzer: { id: '1', name: 'Sarah', rolle: 'planer' },
|
|
haushalt: { id: 'household-123', name: 'Smith family' }
|
|
}
|
|
} as any;
|
|
|
|
try {
|
|
await load(event);
|
|
expect.unreachable();
|
|
} catch (e: any) {
|
|
expect(e.status).toBe(303);
|
|
expect(e.location).toBe('/planner');
|
|
}
|
|
});
|
|
|
|
it('allows access when user has no household', async () => {
|
|
const event = {
|
|
locals: {
|
|
benutzer: { id: '1', name: 'Sarah', rolle: 'planer' },
|
|
haushalt: { id: undefined, name: 'Kein Haushalt' }
|
|
}
|
|
} as any;
|
|
|
|
const result = await load(event);
|
|
expect(result).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('household setup — form action', () => {
|
|
let actions: any;
|
|
|
|
beforeEach(async () => {
|
|
mockPost.mockReset();
|
|
const mod = await import('./+page.server');
|
|
actions = mod.actions;
|
|
});
|
|
|
|
function createRequest(formData: Record<string, string>) {
|
|
const fd = new FormData();
|
|
for (const [key, value] of Object.entries(formData)) {
|
|
fd.append(key, value);
|
|
}
|
|
return {
|
|
request: { formData: () => Promise.resolve(fd) },
|
|
fetch: vi.fn(),
|
|
cookies: { get: vi.fn(), set: vi.fn() }
|
|
} as any;
|
|
}
|
|
|
|
function mockSuccess() {
|
|
return {
|
|
data: { data: { id: 'hh-123', name: 'Smith family', members: [] } },
|
|
error: undefined
|
|
};
|
|
}
|
|
|
|
it('calls POST /v1/households with the household name', async () => {
|
|
mockPost.mockResolvedValue(mockSuccess());
|
|
|
|
try {
|
|
await actions.default(createRequest({ name: 'Smith family' }));
|
|
} catch {
|
|
// redirect throws
|
|
}
|
|
|
|
expect(mockPost).toHaveBeenCalledWith('/v1/households', {
|
|
body: { name: 'Smith family' }
|
|
});
|
|
});
|
|
|
|
it('redirects to /household/staples on success', async () => {
|
|
mockPost.mockResolvedValue(mockSuccess());
|
|
|
|
try {
|
|
await actions.default(createRequest({ name: 'Smith family' }));
|
|
expect.unreachable();
|
|
} catch (e: any) {
|
|
expect(e.status).toBe(303);
|
|
expect(e.location).toBe('/household/staples?ctx=onboarding');
|
|
}
|
|
});
|
|
|
|
it('returns fail(400) when name is empty', async () => {
|
|
const result = await actions.default(createRequest({ name: '' }));
|
|
|
|
expect(result.status).toBe(400);
|
|
expect(result.data.errors.name).toBeTruthy();
|
|
expect(mockPost).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns fail(400) when name is whitespace only', async () => {
|
|
const result = await actions.default(createRequest({ name: ' ' }));
|
|
|
|
expect(result.status).toBe(400);
|
|
expect(result.data.errors.name).toBeTruthy();
|
|
expect(mockPost).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('echoes name back on validation error', async () => {
|
|
const result = await actions.default(createRequest({ name: '' }));
|
|
expect(result.data.name).toBe('');
|
|
});
|
|
|
|
it('returns fail(400) when name exceeds 100 characters', async () => {
|
|
const longName = 'a'.repeat(101);
|
|
const result = await actions.default(createRequest({ name: longName }));
|
|
|
|
expect(result.status).toBe(400);
|
|
expect(result.data.errors.name).toBeTruthy();
|
|
expect(mockPost).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('accepts name at exactly 100 characters', async () => {
|
|
mockPost.mockResolvedValue(mockSuccess());
|
|
const maxName = 'a'.repeat(100);
|
|
|
|
try {
|
|
await actions.default(createRequest({ name: maxName }));
|
|
} catch {
|
|
// redirect throws
|
|
}
|
|
|
|
expect(mockPost).toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns fail with form error on API failure', async () => {
|
|
mockPost.mockResolvedValue({
|
|
data: undefined,
|
|
error: { status: 500, message: 'Internal server error' }
|
|
});
|
|
|
|
const result = await actions.default(createRequest({ name: 'Smith family' }));
|
|
|
|
expect(result.status).toBe(500);
|
|
expect(result.data.errors.form).toBeTruthy();
|
|
});
|
|
});
|