Files
Investidor-V2/tests/frontend/catalogue-guide-links.test.mjs
T

68 lines
2.3 KiB
JavaScript

import { compileFunction } from 'node:vm';
import assert from 'node:assert/strict';
import test from 'node:test';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import ts from 'typescript';
const require = createRequire(import.meta.url);
const source = fs.readFileSync(
new URL(
'../../resources/js/components/catalogue-guide-links.tsx',
import.meta.url,
),
'utf8',
);
const compiled = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
const module = { exports: {} };
compileFunction(compiled, ['require', 'module', 'exports'])(
(name) =>
name === '@inertiajs/react'
? {
Link: ({ href, children, ...props }) =>
React.createElement('a', { href, ...props }, children),
}
: require(name),
module,
module.exports,
);
const render = (articles) =>
renderToStaticMarkup(
React.createElement(module.exports.default, { articles }),
);
void test('catalogue guides are closed optional links, not educational bodies or nationwide opportunity shortcuts', () => {
const html = render([
{
title: 'Turismo',
href: '/artigos/turismo',
summary: 'EDUCATIONAL-BODY-NOT-A-RESULT',
image: { href: '/photo.jpg', alt: 'ILLUSTRATION' },
opportunities_href: '/oportunidades?sector=turismo',
opportunities_label: 'ALL-NATIONAL-OPPORTUNITIES',
has_opportunities: true,
},
]);
assert.match(html, /<details\b/);
assert.doesNotMatch(html, /<details[^>]*\bopen\b/);
assert.match(html, /apoio opcional/);
assert.match(html, /href="\/artigos\/turismo"/);
for (const forbidden of [
'EDUCATIONAL-BODY-NOT-A-RESULT',
'ALL-NATIONAL-OPPORTUNITIES',
'/oportunidades?sector=turismo',
'photo.jpg',
'<img',
])
assert.ok(!html.includes(forbidden), forbidden);
});
void test('no educational substitute is rendered when no guides are available', () => {
assert.equal(render([]), '');
});