Files
kochwas/tests/unit/scaler.test.ts
Hendrik 0f9aabe76b refactor: move scaler out of $lib/server so it can run in browser
RecipeView needs scaleIngredients on the client for live portion scaling.
Moved scaler.ts from $lib/server/recipes/ to $lib/recipes/.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 15:41:20 +02:00

44 lines
1.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { scaleIngredients, roundQuantity } from '../../src/lib/recipes/scaler';
import type { Ingredient } from '../../src/lib/types';
const mk = (q: number | null, unit: string | null, name: string): Ingredient => ({
position: 0,
quantity: q,
unit,
name,
note: null,
raw_text: ''
});
describe('roundQuantity', () => {
it.each([
[0.333333, 0.33],
[12.345, 12],
[1.55, 1.6],
[100.49, 100],
[0.5, 0.5]
] as const)('rounds %f to %f', (input, expected) => {
expect(roundQuantity(input)).toBe(expected);
});
});
describe('scaleIngredients', () => {
it('scales parsed quantities', () => {
const scaled = scaleIngredients([mk(200, 'g', 'Mehl'), mk(3, null, 'Eier')], 2);
expect(scaled[0].quantity).toBe(400);
expect(scaled[1].quantity).toBe(6);
});
it('leaves null quantities alone', () => {
const scaled = scaleIngredients([mk(null, null, 'etwas Salz')], 2);
expect(scaled[0].quantity).toBeNull();
expect(scaled[0].name).toBe('etwas Salz');
});
it('rounds sensibly', () => {
const scaled = scaleIngredients([mk(100, 'g', 'Butter')], 1 / 3);
expect(scaled[0].quantity).toBe(33);
});
});