import * as publicPresentation from '../../resources/js/lib/public-presentation.ts'; import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { compileFunction } from 'node:vm'; 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); function load(file, mocks = {}) { const output = ts.transpileModule( fs .readFileSync( new URL('../../resources/js/components/' + file, import.meta.url), 'utf8', ) .replaceAll('import.meta.env.VITE_MAP_TILE_URL', 'undefined'), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText; const module = { exports: {} }; compileFunction(output, ['require', 'module', 'exports'])( (name) => { if (name in mocks) return mocks[name]; if (name === '@/lib/public-presentation') return publicPresentation; if (name.startsWith('./')) return load( name.slice(2) + (fs.existsSync( new URL( '../../resources/js/components/' + name.slice(2) + '.tsx', import.meta.url, ), ) ? '.tsx' : '.ts'), mocks, ); return require(name); }, module, module.exports, ); return module.exports; } const polygon = (x, y) => ({ type: 'Polygon', coordinates: [[[x, y], [x + 1, y], [x + 1, y + 1], [x, y + 1], [x, y]]], }); const area = (title, selected) => ({ geometry: polygon(selected ? 39 : 38, -13), title, purpose: 'administrative_boundary', selected_territory: selected, location_type: selected ? 'district' : 'province', url: '/territorio', source_reference: 'Mozambique INE - Instituto Nacional de Estatistica', source_date: '2019-06-07', }); test('each administrative boundary is listed once, with its edition, and the count is not repeated (mapa de erros 4.3–4.5)', () => { const mocks = { leaflet: {}, 'leaflet/dist/leaflet.css': {} }; const { TerritoryGeometryDetails } = load( 'territory-geometry-preview.tsx', mocks, ); const context = { selected_geometry_available: true, context_geometry_count: 2, areas: [area('ANCUABE', true), area('Cabo Delgado', false)], truncated: false, child_coverage: null, }; const html = renderToStaticMarkup( React.createElement(TerritoryGeometryDetails, { context, error: '', renderMap: true, }), ); assert.equal(html.split('>ANCUABE<').length - 1, 1); assert.equal(html.split('>Cabo Delgado<').length - 1, 1); assert.doesNotMatch(html, /Data: 2019/); assert.equal(html.split('Edição: 2019-06-07').length - 1, 2); assert.doesNotMatch(html, /limites disponíveis na hierarquia/); assert.equal(html.split('limites administrativos disponíveis').length - 1, 1); assert.equal( html.split('não representa um local de investimento').length - 1, 0, ); assert.match(html, /São contexto administrativo/); // Without a map the textual list remains the only list, with the count. const textOnly = renderToStaticMarkup( React.createElement(TerritoryGeometryDetails, { context, error: '' }), ); assert.match(textOnly, /2 limites disponíveis na hierarquia consultada/); assert.equal(textOnly.split('Edição: 2019-06-07').length - 1, 2); });