treker-po-trenirovkam-i-pit.../tests/dish-card.test.js
Peppa 14be9f33df
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 1с
ci / checks (pull_request) Successful in 8s
deploy / deploy (pull_request) Successful in 15s
TPTIP-13: вес блюда в приёме пищи, подтверждение с названием, тест
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 11:32:57 +05:00

78 lines
4.8 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.

// Сценарии TPTIP-10: карточка блюда с граммовкой, КБЖУ и рецептом; «Выбрать» пишет приём пищи.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const { JSDOM, VirtualConsole } = require('jsdom');
const plan = require('../gym-plan.i18n.js');
const source = fs.readFileSync('index.html', 'utf8');
const initial = vm.runInNewContext(source.split('/* LOGIC:START */')[1].split('/* LOGIC:END */')[0] + '; defaultData()');
initial.settings.onboarded = true;
function open() {
const errors = [];
const vc = new VirtualConsole();
vc.on('jsdomError', e => errors.push(e));
const dom = new JSDOM(source.replace('<script src="gym-plan.i18n.js"></script>', ''), {
url: 'http://localhost/', runScripts: 'dangerously', virtualConsole: vc,
beforeParse(w) { w.GymPlan = plan; w.scrollTo = () => {}; w.localStorage.setItem('tpt.v1', JSON.stringify(initial)); }
});
assert.deepEqual(errors, []);
return { dom, errors, document: dom.window.document };
}
test('Сценарий: карточка блюда показывает фото, граммы, КБЖУ и шаги', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const card = [...document.querySelectorAll('.item')].find(i => i.querySelector('[data-nutrition]'));
assert.ok(card, 'нет карточки с КБЖУ');
assert.ok(card.querySelector('img'));
assert.match(card.querySelector('[data-nutrition]').textContent, /Порция \d+ г · \d+ ккал · Б .* · Ж .* · У /);
assert.ok(card.querySelectorAll('[data-steps] li').length >= 3);
});
test('Сценарий: «Выбрать» добавляет блюдо в приём пищи с теми же значениями', t => {
const { dom, document, errors } = open(); t.after(() => dom.window.close());
const card = [...document.querySelectorAll('.item')].find(i => i.querySelector('[data-nutrition]'));
const title = card.querySelector('b').textContent;
const kcal = card.querySelector('[data-nutrition]').textContent.match(/(\d+) ккал/)[1];
[...card.querySelectorAll('button')].find(b => b.textContent === 'Выбрать').click();
assert.deepEqual(errors, []);
const saved = JSON.parse(dom.window.localStorage.getItem('tpt.v1'));
const m = saved.meals.find(x => x.name === title);
assert.ok(m);
assert.equal(String(m.kcal), kcal);
assert.equal(m.qty, 1);
assert.match(document.querySelector('#view').textContent, new RegExp(kcal));
});
test('Сценарий: у каждого готового блюда есть фото, вес, КБЖУ и не менее трёх шагов на русском', t => {
const { dom } = open(); t.after(() => dom.window.close());
const dishes = dom.window.eval('BUILTIN_RECIPES');
assert.ok(dishes.length >= 7);
for (const d of dishes) {
assert.ok(d.photo, 'нет фото: ' + d.title);
assert.ok(d.grams > 0 && d.kcal > 0 && d.protein >= 0 && d.fat >= 0 && d.carbs >= 0, 'нет цифр: ' + d.title);
const steps = d.steps.split('\n').filter(Boolean);
assert.ok(steps.length >= 3, 'мало шагов: ' + d.title);
assert.ok(steps.every(x => /[а-яё]/i.test(x)), 'шаг не на русском: ' + d.title);
}
});
test('Сценарий: после «Выбрать» видны вес и КБЖУ блюда, кольца и подтверждение обновились', t => {
const { dom, document, errors } = open(); t.after(() => dom.window.close());
const card = [...document.querySelectorAll('.item')].find(i => i.querySelector('[data-nutrition]'));
const title = card.querySelector('b').textContent;
const n = card.querySelector('[data-nutrition]').textContent.match(/Порция (\d+) г · (\d+) ккал · Б (\S+) · Ж (\S+) · У (\S+)/);
const ringsBefore = document.querySelector('.rings').textContent;
[...card.querySelectorAll('button')].find(b => b.textContent === 'Выбрать').click();
assert.deepEqual(errors, []);
assert.notEqual(document.querySelector('.rings').textContent, ringsBefore);
assert.match(document.querySelector('.rings').textContent, new RegExp(n[2]));
assert.match(document.getElementById('toast').textContent, /добавлено в приём пищи/);
[...document.querySelectorAll('#tabsIn button')].find(x => /Питание/.test(x.textContent)).click();
const row = [...document.querySelectorAll('#view .item')].find(i => i.querySelector('b').textContent === title && /порц\./.test(i.textContent));
assert.ok(row, 'нет записи в приёме пищи');
assert.match(row.textContent, new RegExp('\\(' + n[1] + ' г\\)'));
assert.match(row.textContent, new RegExp(n[2] + ' ккал · Б ' + n[3] + ' · Ж ' + n[4] + ' · У ' + n[5]));
});