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, /