Files
Investidor-V2/tests/frontend/irpc-pec-plan-ui.test.mjs
2026-11-11 10:37:33 +02:00

101 lines
3.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { compileFunction } from 'node:vm';
import { createRequire } from 'node:module';
import React from 'react';
import ts from 'typescript';
import * as annual from '../../resources/js/lib/irpc-annual-settlement.ts';
import * as investment from '../../resources/js/lib/irpc-pec-plan.ts';
import * as rational from '../../resources/js/lib/fiscal-rational.ts';
import { renderToStaticMarkup } from 'react-dom/server';
const require = createRequire(import.meta.url),
module = { exports: {} };
const source = fs.readFileSync(
new URL(
'../../resources/js/components/simulation-irpc-pec-plan.tsx',
import.meta.url,
),
'utf8',
);
const output = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
compileFunction(output, ['require', 'module', 'exports'])(
(name) =>
name === './simulation-irpc-pec-plan'
? { SimulationIrpcPecPlan: () => null }
: name === '@/lib/irpc-annual-settlement'
? annual
: name === '@/lib/irpc-pec-plan'
? investment
: name === '@/lib/fiscal-rational'
? rational
: name === './simulation-irpc-special'
? { QualificationFields: () => null }
: name === './fiscal-proof-parcel'
? { FiscalProofParcelFields: () => null }
: require(name),
module,
module.exports,
);
const Component = module.exports.SimulationIrpcPecPlan;
const text = (node) =>
typeof node === 'string' || typeof node === 'number'
? String(node)
: React.isValidElement(node)
? React.Children.toArray(node.props.children).map(text).join('')
: '';
const all = (node) =>
React.isValidElement(node)
? [node, ...React.Children.toArray(node.props.children).flatMap(all)]
: [];
test('actual plan form retains deliberate amounts across mode changes and explains legal deadline limitation', () => {
let p;
const total = { numerator: '40000', denominator: '1' };
const render = () =>
Component({
plan: p,
total,
notApplicable: false,
year: '2026',
onChange: (next) => (p = next),
});
const click = (label) => {
const n = all(render()).find(
(x) => x.type === 'button' && text(x) === label,
);
assert.ok(n, label);
n.props.onClick();
};
const change = (label, value) => {
const n = all(render()).find((x) => x.props['aria-label'] === label);
assert.ok(n, label);
n.props.onChange({ target: { value } });
};
click('Simular tesouraria PEC');
assert.equal(p.june, '');
change('Opção de preparação PEC', 'three');
change('Fundamento do projecção PEC', 'SYNTHETIC allocation');
change('PEC Junho — MZN', '10000');
change('PEC Agosto — MZN', '15000');
change('PEC Outubro — MZN', '15000');
assert.match(text(render()), /Soma reconciliada/);
assert.match(text(render()), /Prazo legal não validado/);
change('Opção de preparação PEC', 'single');
change('Mês previsto do PEC único', '2026-06');
assert.equal(p.june, '10000');
assert.match(text(render()), /40000/);
change('Opção de preparação PEC', 'three');
assert.equal(p.singleMonth, '2026-06');
assert.match(text(render()), /15000/);
const html = renderToStaticMarkup(render());
assert.match(html, /Alterar projecção PEC/);
assert.match(html, /guia/);
});