Show automatic stage help after native disclosure opens
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-17 09:17:14 +02:00
parent e11b5592c7
commit c0d62d4e89
3 changed files with 163 additions and 2 deletions
@@ -1,3 +1,7 @@
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';
@@ -86,3 +90,139 @@ test('dossier help follows only the steps already permitted by the adaptive path
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;
}
});