Mapa de erros 4.4: o grupo escolhido nas zonas mantém-se após a pesquisa

A lista de grupos voltava a «Todas» depois de cada pesquisa, embora o
endereço guardasse o grupo. Os grupos sem registos para a selecção passam
a dizê-lo e não podem ser escolhidos, salvo o que já está aplicado.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MzJmxFy3izprXr5dSq91jM
This commit is contained in:
Claude
2026-12-04 17:30:39 +02:00
committed by nala
parent bb2529f624
commit b530ef252b
3 changed files with 190 additions and 16 deletions
@@ -0,0 +1,139 @@
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"/);
});