182 lines
5.6 KiB
JavaScript
182 lines
5.6 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 finance from '../../resources/js/lib/financial-calculators.ts';
|
|
import * as operating from '../../resources/js/lib/investment-operating-assistance.ts';
|
|
const require = createRequire(import.meta.url);
|
|
const nodes = (tree) =>
|
|
Array.isArray(tree)
|
|
? tree.flatMap(nodes)
|
|
: tree && typeof tree === 'object'
|
|
? [tree, ...nodes(tree.props?.children)]
|
|
: [];
|
|
const text = (tree) =>
|
|
Array.isArray(tree)
|
|
? tree.map(text).join('')
|
|
: tree?.props
|
|
? text(tree.props.children)
|
|
: typeof tree === 'string'
|
|
? tree
|
|
: '';
|
|
function harness(file, snapshot = '') {
|
|
let cursor = 0,
|
|
tree;
|
|
const memory = [],
|
|
changes = [];
|
|
const module = { exports: {} };
|
|
const hooks = {
|
|
...React,
|
|
useState(initial) {
|
|
const id = cursor++;
|
|
if (!(id in memory))
|
|
memory[id] =
|
|
typeof initial === 'function' ? initial() : initial;
|
|
return [
|
|
memory[id],
|
|
(next) => {
|
|
memory[id] =
|
|
typeof next === 'function' ? next(memory[id]) : next;
|
|
},
|
|
];
|
|
},
|
|
};
|
|
compileFunction(
|
|
ts.transpileModule(
|
|
fs.readFileSync('resources/js/components/' + file, 'utf8'),
|
|
{
|
|
compilerOptions: {
|
|
module: ts.ModuleKind.CommonJS,
|
|
jsx: ts.JsxEmit.ReactJSX,
|
|
},
|
|
},
|
|
).outputText,
|
|
['require', 'module', 'exports'],
|
|
)(
|
|
(id) =>
|
|
id === 'react'
|
|
? hooks
|
|
: id === '@/lib/financial-calculators'
|
|
? finance
|
|
: id === '@/lib/investment-operating-assistance'
|
|
? operating
|
|
: require(id),
|
|
module,
|
|
module.exports,
|
|
);
|
|
const props = {
|
|
snapshot,
|
|
currency: 'MZN',
|
|
onChange(key, value) {
|
|
changes.push([key, value]);
|
|
},
|
|
};
|
|
const render = () => {
|
|
cursor = 0;
|
|
tree = module.exports.default(props);
|
|
};
|
|
render();
|
|
return {
|
|
render,
|
|
changes,
|
|
nodes: () => nodes(tree),
|
|
text: () => text(tree),
|
|
button: (label) =>
|
|
nodes(tree).find(
|
|
(node) =>
|
|
node.type === 'button' && text(node).startsWith(label),
|
|
),
|
|
};
|
|
}
|
|
|
|
test('initial budget has no sample amounts and adopts a sum only through the explicit action', () => {
|
|
const h = harness('investment-initial-budget.tsx');
|
|
const amounts = () =>
|
|
h
|
|
.nodes()
|
|
.filter(
|
|
(node) => node.type === 'input' && node.props.type === 'number',
|
|
);
|
|
assert.deepEqual(
|
|
amounts().map((node) => node.props.value),
|
|
['', '', '', ''],
|
|
);
|
|
h.button('Usar a soma').props.onClick();
|
|
h.render();
|
|
assert.match(h.text(), /montantes em falta/);
|
|
assert.equal(
|
|
h.changes.some(([key]) => key === 'total_investment'),
|
|
false,
|
|
);
|
|
for (let i = 0; i < 4; i++) {
|
|
amounts()[i].props.onChange({
|
|
target: { value: String((i + 1) * 1000) },
|
|
});
|
|
h.render();
|
|
}
|
|
assert.equal(
|
|
h.changes.some(([key]) => key === 'total_investment'),
|
|
false,
|
|
);
|
|
h.button('Usar a soma').props.onClick();
|
|
h.render();
|
|
assert.deepEqual(h.changes.at(-1), ['total_investment', '10000.00']);
|
|
const saved = h.changes
|
|
.filter(([key]) => key === 'initial_budget_snapshot')
|
|
.at(-1)[1];
|
|
const restored = harness('investment-initial-budget.tsx', saved);
|
|
assert.deepEqual(
|
|
restored
|
|
.nodes()
|
|
.filter(
|
|
(node) => node.type === 'input' && node.props.type === 'number',
|
|
)
|
|
.map((node) => node.props.value),
|
|
['1000', '2000', '3000', '4000'],
|
|
);
|
|
});
|
|
|
|
test('hotel estimate is selected deliberately, saved as assumptions, then adopted without replacing another field', () => {
|
|
const h = harness('investment-operating-assistance.tsx');
|
|
h.nodes()
|
|
.find((node) => node.type === 'select')
|
|
.props.onChange({ target: { value: 'hotel' } });
|
|
h.render();
|
|
const amounts = () =>
|
|
h
|
|
.nodes()
|
|
.filter(
|
|
(node) => node.type === 'input' && node.props.type === 'number',
|
|
);
|
|
for (const [i, value] of ['10', '5000', '50', '300'].entries()) {
|
|
amounts()[i].props.onChange({ target: { value } });
|
|
h.render();
|
|
}
|
|
assert.equal(
|
|
h.changes.some(([key]) => key === 'annual_revenue'),
|
|
false,
|
|
);
|
|
h.button('Usar esta estimativa de receitas').props.onClick();
|
|
h.render();
|
|
assert.deepEqual(h.changes.at(-1), ['annual_revenue', '7500000.00']);
|
|
assert.equal(
|
|
h.changes.some(
|
|
([key]) =>
|
|
key === 'total_investment' || key === 'investment_budget_max',
|
|
),
|
|
false,
|
|
);
|
|
const saved = h.changes
|
|
.filter(([key]) => key === 'planning_assumptions_snapshot')
|
|
.at(-1)[1];
|
|
const restored = harness('investment-operating-assistance.tsx', saved);
|
|
assert.equal(
|
|
restored.nodes().find((node) => node.type === 'select').props.value,
|
|
'hotel',
|
|
);
|
|
assert.match(restored.text(), /ocupação/);
|
|
});
|