Files
kochwas/tests/unit/scaler.test.ts
2026-04-19 15:00:21 +02:00

56 lines
1.7 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: '',
section_heading: null
});
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);
});
it('preserves section_heading through scaling', () => {
const input: Ingredient[] = [
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Teig' },
{ position: 2, quantity: null, unit: null, name: 'Ei', note: null, raw_text: 'Ei', section_heading: null }
];
const scaled = scaleIngredients(input, 2);
expect(scaled[0].section_heading).toBe('Teig');
expect(scaled[1].section_heading).toBeNull();
expect(scaled[0].quantity).toBe(400);
});
});