Files
Investidor-V2/tests/frontend/document-scan-retry.test.mjs
T

144 lines
4.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import { compileFunction } from 'node:vm';
import { createElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import ts from 'typescript';
const require = createRequire(import.meta.url);
function load(file, mocks) {
const code = ts.transpileModule(
fs.readFileSync(
new URL('../../resources/js/' + file, import.meta.url),
'utf8',
),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
},
).outputText;
const module = { exports: {} };
compileFunction(code, ['require', 'module', 'exports'])(
(name) => mocks[name] ?? require(name),
module,
module.exports,
);
return module.exports;
}
const Empty = () => null;
function management(flash) {
return load('pages/management/shared.tsx', {
'@inertiajs/react': {
Head: Empty,
Link: 'a',
usePage: () => ({ props: { flash, basePath: '/management' } }),
},
'@/components/profile-navigation': { default: Empty },
'@/components/content-management-entry': { default: Empty },
}).Management;
}
test('the actual management shell exposes retry warnings as an alert alongside preserved page content', () => {
const html = renderToStaticMarkup(
createElement(
management({ warning: 'SYNTHETIC dispatch unconfirmed' }),
{ title: 'Documents' },
'SYNTHETIC unchanged editor',
),
);
assert.match(html, /role="alert"[^>]*>SYNTHETIC dispatch unconfirmed/);
assert.match(html, /SYNTHETIC unchanged editor/);
assert.doesNotMatch(html, /role="status"/);
});
test('the actual management shell still renders a confirmed retry as a status', () => {
const html = renderToStaticMarkup(
createElement(management({ success: 'SYNTHETIC accepted' }), {
title: 'Documents',
}),
);
assert.match(html, /role="status"[^>]*>SYNTHETIC accepted/);
assert.doesNotMatch(html, /role="alert"/);
});
function nodes(tree) {
return Array.isArray(tree)
? tree.flatMap(nodes)
: tree?.props
? [tree, ...nodes(tree.props.children)]
: [];
}
function zone(management) {
const sent = [],
setters = [];
const Page = load('pages/investment/zone-attachments.tsx', {
react: { useState: (value) => [value, (next) => setters.push(next)] },
'@inertiajs/react': { router: { post: (...args) => sent.push(args) } },
'./shared': {
api: () => {
throw new Error('Retry must use the redirect-aware transport');
},
button: '',
card: '',
input: '',
Field: 'label',
},
}).default;
const tree = Page({
zoneId: 'SYNTHETIC-zone',
management,
zonePublished: false,
attachments: [
{
id: 'SYNTHETIC-link',
document_id: 'SYNTHETIC-document',
kind: 'photo',
caption: 'SYNTHETIC attachment',
source_reference: '',
source_date: null,
media_type: 'image/png',
size_bytes: 12,
download_url: '/private-document',
malware_scan_status: 'failed',
},
],
});
return { tree, sent, setters };
}
test('the actual zone retry follows the flash response through Inertia and keeps the page scroll', () => {
const h = zone(true),
button = nodes(h.tree).find(
(node) =>
node.type === 'button' &&
node.props.children === 'Tentar análise novamente',
);
assert.ok(button);
button.props.onClick();
assert.equal(h.sent.length, 1);
assert.equal(
h.sent[0][0],
'/management/documents/SYNTHETIC-document/malware/retry',
);
assert.deepEqual(h.sent[0][1], {});
assert.equal(h.sent[0][2].preserveScroll, true);
assert.deepEqual(h.setters, [true, '']);
h.sent[0][2].onError();
assert.match(h.setters.at(-1), /Actualize o estado/);
h.sent[0][2].onFinish();
assert.equal(h.setters.at(-1), false);
});
test('public zone attachments expose no retry command', () => {
const h = zone(false);
assert.equal(
nodes(h.tree).filter(
(node) =>
node.type === 'button' &&
node.props.children === 'Tentar análise novamente',
).length,
0,
);
assert.equal(h.sent.length, 0);
});