treker-po-trenirovkam-i-pit.../tests/wizard.test.js
2026-11-17 11:49:09 +05:00

99 lines
5.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.

// Сценарии TPTIP-11: мастер первого запуска без чужих чисел, с понятным вторым шагом.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const { JSDOM, VirtualConsole } = require('jsdom');
const plan = require('../gym-plan.i18n.js');
const source = fs.readFileSync('index.html', 'utf8');
function open(saved) {
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 = () => {}; if (saved) w.localStorage.setItem('tpt.v1', saved); }
});
assert.deepEqual(errors, []);
return { dom, document: dom.window.document };
}
const btn = (d, t) => [...d.querySelectorAll('button')].find(b => b.textContent === t);
const inputs = d => [...d.querySelectorAll('.sheet input')];
test('Сценарий: на первом шаге поля пустые, числа только в подсказках', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const [age, height, weight] = inputs(document);
for (const i of [age, height, weight]) assert.equal(i.value, '');
assert.match(age.placeholder, /30/);
btn(document, 'Далее').click();
assert.match(document.querySelector('.sheet').textContent, /Шаг 1 из 2/);
assert.equal(inputs(document).length, 3, 'пустые поля не пускают дальше');
});
test('Сценарий: на втором шаге нормы посчитаны и правятся вручную; окно больше не появляется', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const [age, height, weight] = inputs(document);
age.value = '40'; height.value = '170'; weight.value = '65';
btn(document, 'Далее').click();
const sheet = document.querySelector('.sheet');
assert.match(sheet.textContent, /Шаг 2 из 2\. Мы посчитали/);
assert.ok(sheet.querySelector('[data-norms-hint]'));
const kcal = inputs(document)[0];
assert.ok(Number(kcal.value) > 1000);
kcal.value = '1800';
btn(document, 'Готово').click();
const saved = dom.window.localStorage.getItem('tpt.v1');
assert.equal(JSON.parse(saved).settings.kcal, 1800);
assert.equal(JSON.parse(saved).settings.onboarded, true);
assert.equal(document.querySelector('.sheet'), null);
const again = open(saved); t.after(() => again.dom.window.close());
assert.equal(again.document.querySelector('.sheet'), null);
});
test('Сценарий: после «Готово» кольца показывают посчитанные нормы, а не 2000/120/65/230', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const [age, height, weight] = inputs(document);
age.value = '40'; height.value = '170'; weight.value = '65';
btn(document, 'Далее').click();
btn(document, 'Готово').click();
const s = JSON.parse(dom.window.localStorage.getItem('tpt.v1')).settings;
assert.notEqual(s.kcal, 2000);
const rings = document.querySelector('.rings');
assert.ok(rings, 'кольца на главном экране');
assert.ok(rings.textContent.includes(String(s.kcal)), 'норма калорий ' + s.kcal + ' видна: ' + rings.textContent);
});
test('Сценарий: пустые и нереальные данные не пускают дальше, понятно почему, введённое остаётся', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const [age, height, weight] = inputs(document);
btn(document, 'Далее').click();
const fields = [...document.querySelectorAll('.sheet .field.bad')];
assert.equal(fields.length, 3, 'все пустые поля подсвечены');
assert.match(document.querySelector('.sheet').textContent, /Укажите возраст: поле пока пустое/);
age.value = '5'; height.value = '175'; weight.value = '9999';
btn(document, 'Далее').click();
assert.match(document.querySelector('.sheet').textContent, /Шаг 1 из 2/);
assert.equal(document.querySelectorAll('.sheet .field.bad').length, 2);
assert.match(document.querySelector('.sheet').textContent, /Проверьте возраст: от 10 до 100 лет/);
assert.match(document.querySelector('.sheet').textContent, /Проверьте вес: от 25 до 400 кг/);
assert.equal(inputs(document)[1].value, '175', 'верное значение не пропало');
assert.equal(inputs(document)[0].value, '5');
});
test('Сценарий: после мастера кольца подписаны полными единицами и показывают нормы, а не нули', t => {
const { dom, document } = open(); t.after(() => dom.window.close());
const [age, height, weight] = inputs(document);
age.value = '40'; height.value = '170'; weight.value = '65';
btn(document, 'Далее').click();
assert.match(document.querySelector('.sheet').textContent, /Шаг 2 из 2/);
btn(document, 'Готово').click();
assert.equal(document.querySelector('.sheet'), null);
const s = JSON.parse(dom.window.localStorage.getItem('tpt.v1')).settings;
const text = document.querySelector('.rings').textContent;
assert.match(text, /ккал/); assert.match(text, /г белка/); assert.match(text, /мл воды/);
for (const k of ['kcal', 'protein', 'water']) {
assert.ok(s[k] > 0, k + ' больше нуля');
assert.ok(text.includes(String(s[k])), k + ' ' + s[k] + ' виден в кольцах: ' + text);
}
});