Files
Investidor-V2/tests/frontend/desk-service-editor.test.mjs
T
2026-11-02 20:31:15 +02:00

118 lines
4.4 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 { renderToStaticMarkup } from 'react-dom/server';
import ts from 'typescript';
const require = createRequire(import.meta.url);
function nodes(tree) {
if (!tree || typeof tree !== 'object') return [];
if (Array.isArray(tree)) return tree.flatMap(nodes);
return [tree, ...nodes(tree.props?.children)];
}
await test('service editor retains the source, invalidates confirmation after edits and compares old/new values before publishing a remembered draft', () => {
const service = {
code: 'project-change',
version: 1,
definition: {
title: 'Synthetic service',
theme: 'Synthetic',
profile: 'Investor',
entity: 'Internal team',
result: 'Preparation',
stage: 'E10',
fields: {
question: {
label: 'Original question',
type: 'text',
required: false,
},
},
},
};
const before = JSON.stringify(service);
let form, rememberedKey, sent;
const output = ts.transpileModule(
fs.readFileSync(
new URL(
'../../resources/js/pages/desk/service-editor.tsx',
import.meta.url,
),
'utf8',
) + '\nexport { Editor };',
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
const module = { exports: {} };
compileFunction(output, ['require', 'module', 'exports'])(
(name) =>
name === 'react'
? { useState: (initial) => [initial, () => {}] }
: name === '@inertiajs/react'
? {
Link: 'a',
useForm(key, initial) {
rememberedKey = key;
form ??= {
data: initial,
errors: {},
processing: false,
setData(value, next) {
this.data =
typeof value === 'function'
? value(this.data)
: { ...this.data, [value]: next };
},
put(url) {
sent = {
url,
data: structuredClone(this.data),
};
},
};
return form;
},
}
: name === '@/pages/investment/shared'
? { Frame: 'main', card: '', button: '', input: '' }
: name === '@/components/desk-service-history'
? { default: () => null, __esModule: true }
: require(name),
module,
module.exports,
);
const render = () => module.exports.Editor({ service });
let tree = render();
assert.equal(rememberedKey, 'desk-service-editor:project-change:1');
form.data.confirm = true;
const question = nodes(tree).find(
(node) =>
node.type === 'input' && node.props.value === 'Original question',
);
question.props.onChange({ target: { value: 'Revised question' } });
assert.equal(form.data.confirm, false);
tree = render();
const html = renderToStaticMarkup(tree);
assert.match(html, /Versão anterior/);
assert.match(html, /Original question/);
assert.match(html, /Revised question/);
assert.match(html, /pedidos.*definição original/is);
assert.equal(JSON.stringify(service), before);
form.data.confirm = true;
tree.props.onSubmit({ preventDefault() {} });
assert.equal(sent.url, '/management/balcao/servicos/project-change');
assert.equal(sent.data.base_version, 1);
assert.equal(
sent.data.definition.fields.question.label,
'Revised question',
);
});