Files
Investidor-V2/tests/frontend/investment-step-help.test.mjs
T
2026-11-17 09:17:14 +02:00

229 lines
7.3 KiB
JavaScript

import React from 'react';
import ts from 'typescript';
import { compileFunction } from 'node:vm';
import { createRequire } from 'node:module';
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import {
investmentStageHelp,
preparationHelp,
helpPreferenceKey,
readHelpPreference,
} from '../../resources/js/lib/investment-step-help.ts';
test('each of the 19 existing stages has specific orientation, an example and a purpose', () => {
const instructions = new Set();
for (let i = 1; i <= 19; i++) {
const code = `S${String(i).padStart(2, '0')}`;
const help = investmentStageHelp(code, `Título ${i}`);
assert.equal(help.code, code);
assert.equal(help.title, `Título ${i}`);
assert.ok(help.instruction.length > 30);
assert.ok(help.example.length > 25);
assert.ok(help.purpose.length > 25);
instructions.add(help.instruction);
}
assert.equal(instructions.size, 19);
assert.equal(
investmentStageHelp('future-stage', 'Nova etapa').title,
'Nova etapa',
);
});
test('preparation guidance preserves the review before confirmation sequence', () => {
assert.deepEqual(
preparationHelp.map((step) => step.code),
['help-choices', 'help-declarations', 'help-review'],
);
assert.match(preparationHelp[2].instruction, /Compare.*confirme/);
});
test('preferences default safely, survive malformed storage and remain separate per user and flow', () => {
assert.deepEqual(readHelpPreference(null), {
automatic: true,
seen: false,
});
assert.deepEqual(readHelpPreference('{bad'), {
automatic: true,
seen: false,
});
assert.deepEqual(readHelpPreference('{"automatic":false,"seen":true}'), {
automatic: false,
seen: true,
});
assert.deepEqual(readHelpPreference('{"automatic":true,"seen":true}'), {
automatic: true,
seen: true,
});
assert.notEqual(helpPreferenceKey(1, 'path'), helpPreferenceKey(2, 'path'));
assert.notEqual(
helpPreferenceKey(1, 'path'),
helpPreferenceKey(1, 'preparation'),
);
assert.notEqual(
helpPreferenceKey('a:b', 'c'),
helpPreferenceKey('a', 'b:c'),
);
});
test('dossier help follows only the steps already permitted by the adaptive path', () => {
const page = readFileSync(
new URL(
'../../resources/js/pages/investor-path/show.tsx',
import.meta.url,
),
'utf8',
);
assert.match(
page,
/steps=\{path\.steps\s*\.filter\(\(step\) => step\.preparation_allowed\)/,
);
assert.match(page, /<HelpAtStep code=\{step.code\}/);
const component = readFileSync(
new URL(
'../../resources/js/components/investment-step-help.tsx',
import.meta.url,
),
'utf8',
);
assert.doesNotMatch(component, /router\.|fetch\(|\.submit\(|\.post\(/);
assert.doesNotMatch(component, /location\.(assign|reload)|window\.history/);
});
test('summary focus and click wait for the native open toggle; closing a stage does not reopen it', () => {
const require = createRequire(import.meta.url);
const memory = [];
let cursor = 0;
let refCursor = 0;
const refs = [];
const effects = [];
const listeners = {};
const rootNode = {
querySelectorAll: () => [],
addEventListener: (name, listener) => {
listeners[name] = listener;
},
removeEventListener() {},
};
const hooks = {
...React,
useEffect(effect) {
effects.push(effect);
},
useRef: (initial) => {
const index = refCursor++;
return (refs[index] ??= { current: initial });
},
useState(initial) {
const index = cursor++;
if (!(index in memory)) memory[index] = initial;
return [
memory[index],
(value) => {
memory[index] = value;
},
];
},
};
const source = readFileSync(
new URL(
'../../resources/js/components/investment-step-help.tsx',
import.meta.url,
),
'utf8',
);
const code = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
const module = { exports: {} };
compileFunction(code, ['require', 'module', 'exports'])(
(name) =>
name === 'react'
? hooks
: name === 'lucide-react'
? { CircleHelp: () => null }
: name === '@/lib/investment-step-help'
? { helpPreferenceKey, readHelpPreference }
: require(name),
module,
module.exports,
);
class Target {
constructor(summary, open) {
this.summary = summary;
this.open = open;
this.dataset = { investmentStep: 'S02' };
}
closest(selector) {
return selector === 'summary'
? this.summary
? this
: null
: selector === '[data-investment-step]'
? this
: null;
}
}
class Disclosure extends Target {}
const originalElement = globalThis.Element;
const originalDetails = globalThis.HTMLDetailsElement;
globalThis.Element = Target;
globalThis.HTMLDetailsElement = Disclosure;
const render = () => {
cursor = 0;
refCursor = 0;
effects.length = 0;
const tree = module.exports.InvestmentHelpGuide({
user: 1,
flow: 'regression',
steps: [investmentStageHelp('S02', 'Análise')],
});
refs[0].current = rootNode;
effects.at(-1)();
return tree;
};
try {
let tree = render();
const summary = new Target(true);
tree.props.children.props.onFocusCapture({ target: summary });
tree.props.children.props.onClickCapture({ target: summary });
assert.equal(
render().props.value.active,
null,
'summary events must not open details before their native click',
);
listeners.toggle({
target: new Disclosure(false, false),
});
assert.equal(render().props.value.active, null);
listeners.toggle({
target: new Disclosure(false, true),
});
tree = render();
assert.equal(
tree.props.value.active,
'S02',
'opening a stage selects its automatic help',
);
tree.props.value.choose(null);
tree = render();
listeners.toggle({
target: new Disclosure(false, true),
});
assert.equal(
render().props.value.active,
null,
'closing the guide keeps automatic help paused',
);
} finally {
if (originalElement === undefined) delete globalThis.Element;
else globalThis.Element = originalElement;
if (originalDetails === undefined) delete globalThis.HTMLDetailsElement;
else globalThis.HTMLDetailsElement = originalDetails;
}
});