what-to-cook/test/recipe-example.test.js
2026-11-16 06:37:21 +05:00

81 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { recipeExample, recipeExamples, hasNoRecipe, hasRecipe, addDish } from "../src/logic.js";
import { catalog } from "../src/catalog.js";
import { findRecipe } from "../src/recipes.js";
import { messages } from "../src/i18n.js";
const read = (f) => readFileSync(new URL(`../src/${f}`, import.meta.url), "utf8");
// WTC-20: подсказка с примером, как получить рецепт
test("Сценарий: Пустой список — подсказка и пример", () => {
assert.equal(recipeExample([], catalog, findRecipe), "Бешбармак");
assert.equal(messages.ru.addExample.replace("{name}", "Бешбармак"), "Добавить «Бешбармак»");
assert.match(messages.ru.recipeExampleHint, /появится кнопка «Рецепт»/);
const html = read("index.html");
assert.ok(html.indexOf('id="listTitle"') < html.indexOf('id="recipeExampleHint"'));
assert.ok(html.indexOf('id="recipeExampleHint"') < html.indexOf('id="examples"'));
for (const l of ["ru", "kk", "en"]) {
for (const k of ["recipeExampleHint", "noRecipeHint"]) assert.ok(messages[l][k].length > 20, `${l}.${k}`);
assert.match(messages[l].addExample, /\{name\}/, l);
}
});
test("Сценарий: Пример добавляется одним нажатием", () => {
const name = recipeExample([], catalog, findRecipe);
const dishes = addDish([], name);
assert.deepEqual(dishes, ["Бешбармак"]);
assert.equal(hasRecipe(dishes, findRecipe), true);
const app = read("app.js");
const click = app.slice(app.indexOf('$("examples").addEventListener'), app.indexOf("function showNameError"));
assert.match(click, /addDish\(state\.dishes, btn\.dataset\.name\)/);
assert.match(click, /save\(\);.*render\(\)/);
});
test("Сценарий: В списке только своё блюдо", () => {
assert.equal(hasRecipe(["Мамин пирог"], findRecipe), false);
assert.equal(recipeExample(["Мамин пирог"], catalog, findRecipe), "Бешбармак");
});
test("Сценарий: Блюдо с рецептом уже есть", () => {
assert.equal(hasRecipe(["Плов"], findRecipe), true);
assert.match(read("app.js"), /const examples = exampleStates\(state\.dishes, catalog, findRecipe\)/);
assert.match(read("app.js"), /\$\("recipeExample"\)\.hidden = examples.length === 0/);
});
test("Граница: пример не повторяет блюдо из списка, пустая база — примера нет", () => {
assert.equal(recipeExample(["бешбармак"], catalog, findRecipe), "Плов");
assert.equal(recipeExample([], [], findRecipe), null);
assert.equal(recipeExample([], undefined, findRecipe), null);
assert.equal(recipeExample([], ["Мамин пирог"], findRecipe), null);
});
// WTC-20: подпись объясняет пометку «Рецепта нет»
test("Сценарий: Своё блюдо в списке", () => {
assert.equal(hasNoRecipe(["Бешбармак", "Мамин пирог"], findRecipe), true);
assert.equal(findRecipe("Мамин пирог"), null);
assert.match(messages.ru.noRecipeHint, /«Рецепта для этого блюда пока нет»/);
assert.ok(messages.ru.noRecipeHint.includes(messages.ru.noRecipe));
assert.match(read("app.js"), /\$\("noRecipeHint"\)\.hidden = n !== 0 && !hasNoRecipe\(state\.dishes, findRecipe\)/);
const html = read("index.html");
assert.ok(html.indexOf('id="list"') < html.indexOf('id="noRecipeHint"'));
});
test("Сценарий: Все блюда из базы", () => {
assert.equal(hasNoRecipe(["Бешбармак"], findRecipe), false);
assert.equal(hasNoRecipe([], findRecipe), false);
});
// WTC-27: несколько примеров из базы
test("Сценарий: Пустой экран — 4 примера, уже добавленные скрыты", () => {
const four = recipeExamples([], catalog, findRecipe);
assert.equal(four.length, 4);
assert.ok(four.every((n) => findRecipe(n)));
const rest = recipeExamples([four[0].toLowerCase()], catalog, findRecipe);
assert.ok(!rest.includes(four[0]));
assert.equal(rest.length, 4);
assert.deepEqual(recipeExamples([], [], findRecipe), []);
assert.deepEqual(recipeExamples([], undefined, findRecipe), []);
});
test("Сценарий: Подпись — только примеры, можно своё", () => {
for (const l of ["ru", "kk", "en"]) assert.ok(messages[l].examplesNote.length > 20, l);
assert.match(messages.ru.examplesNote, /только примеры/);
assert.match(read("index.html"), /id="examplesNote"/);
});