what-to-cook/test/name-field.test.js
Peppa d4c20db42e WTC-16: пример в поле берётся из базы блюд
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 06:15:00 +05:00

65 lines
3.0 KiB
JavaScript
Raw Permalink 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 { submitDish } from "../src/logic.js";
import { messages } from "../src/i18n.js";
const read = (f) => readFileSync(new URL(`../src/${f}`, import.meta.url), "utf8");
// Контраст по WCAG для цветов вида #rrggbb.
function contrast(a, b) {
const lum = (hex) => {
const [r, g, bl] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)
.map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
return 0.2126 * r + 0.7152 * g + 0.0722 * bl;
};
const [hi, lo] = [lum(a), lum(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
test("Сценарий: подпись у поля — постоянная подпись «Название блюда» над полем", () => {
const html = read("index.html");
assert.match(html, /<label id="nameLabel" for="name">/);
assert.ok(html.indexOf('id="nameLabel"') < html.indexOf('id="name"'));
assert.match(read("app.js"), /\$\("nameLabel"\)\.textContent = t\.nameLabel/);
assert.equal(messages.ru.nameLabel, "Название блюда");
assert.notEqual(messages.ru.placeholder, messages.ru.nameLabel);
});
test("Сценарий: пустое поле — сообщение «Впишите название блюда», список не меняется", () => {
const dishes = ["Борщ"];
const r = submitDish(dishes, "");
assert.equal(r.error, "emptyName");
assert.equal(r.dishes, dishes);
assert.equal(messages.ru[r.error], "Впишите название блюда");
const html = read("index.html");
assert.match(html, /<form id="form" novalidate>/);
assert.doesNotMatch(html, /required/);
assert.match(html, /id="nameError"[^>]*role="alert"/);
assert.ok(html.indexOf('id="name"') < html.indexOf('id="nameError"'));
const color = read("style.css").match(/\.error \{[^}]*color: (#[0-9a-f]{6})/)[1];
assert.ok(contrast(color, "#fffaf3") >= 4.5);
});
test("Сценарий: одни пробелы — сообщение, список не меняется", () => {
const dishes = ["Борщ"];
const r = submitDish(dishes, " ");
assert.equal(r.error, "emptyName");
assert.equal(r.dishes, dishes);
});
test("Сценарий: успешное добавление — блюдо в списке, поле очищается, сообщения нет", () => {
const r = submitDish(["Борщ"], "Плов");
assert.equal(r.error, null);
assert.deepEqual(r.dishes, ["Борщ", "Плов"]);
const app = read("app.js");
assert.match(app, /if \(r\.error\) return;[\s\S]*\$\("name"\)\.value = ""/);
assert.match(app, /showNameError\(r\.error\)/);
});
test("Тексты поля есть на всех языках", () => {
for (const lang of ["ru", "kk", "en"]) {
for (const k of ["nameLabel", "placeholder", "placeholderNeutral", "emptyName"]) assert.ok(messages[lang][k], `${lang}.${k}`);
}
});