From d367e18eecd427cb6338faf891e7795df72c3444 Mon Sep 17 00:00:00 2001
From: nala
Sem linhas significa que não declarou despesas de arranque; para confirmar a inexistência, registe uma linha «Sem despesas de diff --git a/tests/frontend/integral-financial-plan-ui.test.mjs b/tests/frontend/integral-financial-plan-ui.test.mjs new file mode 100644 index 00000000..8530710c --- /dev/null +++ b/tests/frontend/integral-financial-plan-ui.test.mjs @@ -0,0 +1,85 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import fs from 'node:fs'; +import { createRequire } from 'node:module'; +import { compileFunction } from 'node:vm'; +import ts from 'typescript'; +import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; +import { financialReportFixture } from './fixtures/integral-financial-plan.mjs'; +import * as report from '../../resources/js/lib/integral-financial-report.ts'; +import { + serializeBusinessPlanSnapshot, + restoreBusinessPlanSnapshot, +} from '../../resources/js/lib/business-financial-plan.ts'; + +void test('one startup editor retains all rows and persists an edit without duplicating controls or changing reconciliation', () => { + const source = fs.readFileSync( + new URL( + '../../resources/js/components/integral-financial-plan.tsx', + import.meta.url, + ), + 'utf8', + ); + const code = ts.transpileModule(source, { + compilerOptions: { + module: ts.ModuleKind.CommonJS, + target: ts.ScriptTarget.ES2022, + jsx: ts.JsxEmit.ReactJSX, + }, + }).outputText; + const module = { exports: {} }, + require = createRequire(import.meta.url); + compileFunction(code, ['require', 'module', 'exports'])( + (name) => + name === '@/lib/integral-financial-report' + ? report + : name === './investment-report-tables' + ? { InvestmentReportTables: () => null } + : require(name), + module, + module.exports, + ); + let plan = financialReportFixture(12); + const render = () => + module.exports.IntegralFinancialPlan({ + plan, + onChange: (next) => { + plan = next; + }, + }); + const html = renderToStaticMarkup(render()); + assert.equal( + (html.match(/Adicionar despesa de arranque/g) ?? []).length, + 1, + ); + assert.equal((html.match(/Remover despesa/g) ?? []).length, 1); + const nodes = (node) => + Array.isArray(node) + ? node.flatMap(nodes) + : node && typeof node === 'object' + ? [node, ...nodes(node.props?.children)] + : []; + const label = nodes(render()).find( + (node) => + node.type === 'label' && + React.Children.toArray(node.props.children).includes( + 'Montante de arranque 1', + ), + ); + nodes(label) + .find((node) => node.type === 'input') + .props.onChange({ target: { value: '100.25' } }); + plan = restoreBusinessPlanSnapshot(serializeBusinessPlanSnapshot(plan)); + assert.equal(plan.reportAssumptions.startupExpenses[0].amount, '100.25'); + assert.match(renderToStaticMarkup(render()), /value="100.25"/); + const balance = report + .buildIntegralFinancialTables(plan) + .find((table) => table.id === 'balance'); + assert.ok( + balance.rows + .find((row) => row[0] === 'Diferença do balanço') + .slice(1) + .every((value) => value === '0.00'), + ); +});