Files
Investidor-V2/tests/frontend/zones-group-selection.test.mjs
T
Claude b7ec4578dd Criar Hotel: a escolha do dossier mostra a actividade de cada um
Vindo da página de um sector, os dossiers do mesmo sector aparecem primeiro,
cada um com a sua actividade, e escolher um dossier de outra actividade
avisa que o novo sector fica só como proposta.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
2026-12-04 21:11:45 +02:00

183 lines
6.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { createRequire } from "node:module";
import { compileFunction } from "node:vm";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import ts from "typescript";
const require = createRequire(import.meta.url);
const root = path.resolve("resources/js");
function load(file) {
const absolute = path.resolve(root, file);
const module = { exports: {} };
const code = ts.transpileModule(fs.readFileSync(absolute, "utf8"), {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
jsx: ts.JsxEmit.ReactJSX,
},
}).outputText;
compileFunction(code, ["require", "module", "exports"])(
(id) => {
if (id === "@inertiajs/react")
return {
Link: ({ children, ...props }) =>
React.createElement("a", props, children),
usePage: () => ({ url: "/zonas", props: {} }),
router: { get() {} },
Head: () => null,
};
if (
id === "lucide-react" ||
id.startsWith("@/components/") ||
id.startsWith("@/layouts/")
)
return new Proxy(
{ __esModule: true },
{
get: (target, key) =>
key in target
? target[key]
: key === "Frame"
? ({ children }) =>
React.createElement(
"main",
null,
children,
)
: () => null,
},
);
if (id.startsWith(".") || id.startsWith("@/")) {
const base = id.startsWith("@/")
? path.resolve(root, id.slice(2))
: path.resolve(path.dirname(absolute), id);
const found = [base, base + ".tsx", base + ".ts"].find(
(p) => fs.existsSync(p) && fs.statSync(p).isFile(),
);
if (found) return load(path.relative(root, found));
}
return require(id);
},
module,
module.exports,
);
return module.exports;
}
function fakeSelect(applied, values, opened) {
const options = values.map((value) => ({
value,
defaultSelected: value === opened,
}));
const select = {
value: applied,
options,
getAttribute: (name) => (name === "data-applied" ? applied : null),
};
return select;
}
test('after a search the group list keeps the applied group instead of returning to "Todas" (mapa de erros 4.4)', () => {
const { resetToAppliedFilters } = load("lib/applied-form-reset.ts");
const group = fakeSelect("zee", ["", "zee", "zfi", "industrial_park"], "");
const unrelated = {
value: "x",
options: [{ value: "x", defaultSelected: true }],
getAttribute: () => null,
};
let resets = 0;
const form = {
querySelectorAll: () => [group, unrelated],
reset() {
resets++;
for (const select of [group, unrelated])
select.value =
select.options.find((o) => o.defaultSelected)?.value ?? "";
},
};
resetToAppliedFilters(form);
assert.equal(resets, 1);
assert.equal(group.value, "zee");
assert.equal(unrelated.value, "x");
resetToAppliedFilters(null);
});
test("a group known to have no records says so and cannot be chosen, unless it is the applied one (mapa de erros 4.4)", () => {
const { default: Zones } = load("pages/investment/zones.tsx");
const render = (category) =>
renderToStaticMarkup(
React.createElement(Zones, {
records: {
data: [],
total: 0,
next_page_url: null,
prev_page_url: null,
},
filters: category ? { category } : {},
groupAvailability: { zee: 1, zfi: 0, industrial_park: 3 },
management: false,
}),
);
const html = render("");
assert.match(html, /<option value="zee">ZEE — 1 registo<\/option>/);
assert.match(
html,
/<option value="zfi" disabled="">ZFI — sem registos para esta selecção<\/option>/,
);
assert.match(html, /Parque industrial — 3 registos/);
assert.match(html, /name="category" data-applied=""/);
const applied = render("zfi");
assert.match(
applied,
/<option value="zfi" selected="">ZFI — sem registos para esta selecção<\/option>/,
);
assert.match(applied, /data-applied="zfi"/);
});
test("the education page reads the population by age groups and says the training comparison is not public yet (mapa de erros 4.6)", () => {
const { demographyQuery } = load("lib/demography-link.ts");
assert.equal(demographyQuery(undefined, false), "");
assert.equal(
decodeURIComponent(demographyQuery("abc", true)),
"?territories[]=abc&sector=education",
);
assert.equal(
decodeURIComponent(demographyQuery("abc", false)),
"?territories[]=abc",
);
const source = fs.readFileSync(
path.resolve(root, "pages/investment/catalogue.tsx"),
"utf8",
);
assert.match(
source,
/ainda não está disponível no portal público[\s\S]*Pedir apoio da equipa para esta localização/,
);
});
test('choosing a dossier from a sector page shows each dossier activity, same sector first (Criar Hotel)', () => {
const { default: ContextStart } = load('pages/investor-path/context-start.tsx');
const html = renderToStaticMarkup(
React.createElement(ContextStart, {
contexts: [
{ id: 'a', title: 'Criar Hotel', version: 1, sector_id: '7', sector: 'Turismo' },
{ id: 'b', title: 'Machamba', version: 1, sector_id: '1', sector: 'Agronegócio' },
{ id: 'c', title: 'Sem sector', version: 1 },
],
source: { key: 'sector:1', kind: 'sector', id: '1', title: 'Agronegócio', href: '/sectores/agronegocio', version: null, sha256: '', fields: { sector_id: '1', sector: 'Agronegócio' } },
opportunityChoices: [],
entryQuery: {},
}),
);
const options = [...html.matchAll(/<option value="([a-z]*)">([^<]*)<\/option>/g)].map((m) => m[2]);
assert.deepEqual(options, [
'Machamba — Agronegócio',
'Criar Hotel — Turismo',
'Sem sector',
]);
});