97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
|
|
import assert from "node:assert/strict";
|
||
|
|
import { test } from "node:test";
|
||
|
|
|
||
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Maybe } from "@repo/rdx-utils";
|
||
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||
|
|
import { createInstance } from "i18next";
|
||
|
|
import type { ReactNode } from "react";
|
||
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
||
|
|
import { I18nextProvider } from "react-i18next";
|
||
|
|
|
||
|
|
import { buildCatalogResponse } from "../api/application/asset-catalog-response.builder";
|
||
|
|
import { AssetCatalog } from "../api/domain/asset-catalog.aggregate";
|
||
|
|
import { seedAssetCatalog } from "../api/domain/asset-catalog-seed";
|
||
|
|
|
||
|
|
import { AssetForm } from "./asset-form";
|
||
|
|
import { catalogKeys } from "./catalog-api";
|
||
|
|
import { CatalogTemplateForm } from "./catalog-template-form";
|
||
|
|
import { assetsEnglish, assetsSpanish } from "./i18n";
|
||
|
|
import { inventoryKeys } from "./inventory-api";
|
||
|
|
import { AssetInventoryPage } from "./inventory-page";
|
||
|
|
|
||
|
|
const companyId = UniqueID.generateNewID().toPrimitive();
|
||
|
|
const lookups = {
|
||
|
|
sites: [],
|
||
|
|
people: [],
|
||
|
|
onCreateSite: () => undefined,
|
||
|
|
onCreatePerson: () => undefined,
|
||
|
|
};
|
||
|
|
async function render(language: string, node: ReactNode) {
|
||
|
|
const i18n = createInstance();
|
||
|
|
await i18n.init({
|
||
|
|
lng: language,
|
||
|
|
resources: { es: { assets: assetsSpanish }, en: { assets: assetsEnglish } },
|
||
|
|
});
|
||
|
|
const client = new QueryClient();
|
||
|
|
client.setQueryData(inventoryKeys.company(companyId), { assets: [] });
|
||
|
|
client.setQueryData(
|
||
|
|
catalogKeys.scope(companyId),
|
||
|
|
buildCatalogResponse(seedAssetCatalog(AssetCatalog.create()), Maybe.none())
|
||
|
|
);
|
||
|
|
try {
|
||
|
|
return renderToStaticMarkup(
|
||
|
|
<I18nextProvider i18n={i18n}>
|
||
|
|
<QueryClientProvider client={client}>{node}</QueryClientProvider>
|
||
|
|
</I18nextProvider>
|
||
|
|
);
|
||
|
|
} finally {
|
||
|
|
client.clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
test("Inventario traducido con alta de cabecera y sin formulario junto al listado", async () => {
|
||
|
|
for (const language of ["es", "en"]) {
|
||
|
|
const html = await render(
|
||
|
|
language,
|
||
|
|
<AssetInventoryPage companyId={companyId} csrfToken="test" lookups={lookups} />
|
||
|
|
);
|
||
|
|
assert.equal(html.includes("<form"), false);
|
||
|
|
assert.ok(html.includes(language === "es" ? "Nuevo activo" : "New asset"));
|
||
|
|
assert.ok(
|
||
|
|
html.includes(
|
||
|
|
language === "es"
|
||
|
|
? "Esta compañía todavía no tiene activos."
|
||
|
|
: "This company has no assets yet."
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
test("El activo ofrece altas relacionadas, ayuda inferior y no encadena una tercera alta", async () => {
|
||
|
|
const html = await render(
|
||
|
|
"es",
|
||
|
|
<AssetForm
|
||
|
|
companyId={companyId}
|
||
|
|
csrfToken="test"
|
||
|
|
items={[]}
|
||
|
|
lookups={lookups}
|
||
|
|
onSaved={() => undefined}
|
||
|
|
/>
|
||
|
|
);
|
||
|
|
for (const label of ["Nueva plantilla", "Nueva sede", "Nueva persona"]) {
|
||
|
|
assert.ok(html.includes(`aria-label="${label}"`));
|
||
|
|
}
|
||
|
|
assert.ok(html.indexOf('name="notes"') < html.indexOf("<aside"));
|
||
|
|
assert.ok(html.indexOf("<aside") < html.indexOf('type="submit"'));
|
||
|
|
const child = await render(
|
||
|
|
"es",
|
||
|
|
<CatalogTemplateForm
|
||
|
|
allowQuickCreate={false}
|
||
|
|
csrfToken="test"
|
||
|
|
items={[]}
|
||
|
|
onSaved={() => undefined}
|
||
|
|
scope={companyId}
|
||
|
|
/>
|
||
|
|
);
|
||
|
|
assert.equal(child.includes('aria-label="Nuevo campo"'), false);
|
||
|
|
});
|