import { describe, expect, test } from "bun:test";
import { fireEvent, screen, waitFor } from "@testing-library/react";
import { apiFalsa } from "../../../pruebas/api-falsa";
{% if access.update_roles or access.delete_roles -%}
import { MadreUsuarioActual } from "../../../pruebas/auth";
{% endif -%}
{% for t in targets -%}
import { coleccion{{ t.pascal }}, Madre{{ t.pascal }} } from "../../../pruebas/{{ t.kebab }}";
{% endfor -%}
import { coleccion{{ pascal }}, Madre{{ pascal }} } from "../../../pruebas/{{ kebab }}";
import { renderizarPantalla } from "../../../pruebas/renderizar";
import { ContenedorDetalle{{ pascal }} } from "./detalle-{{ kebab }}";
describe("ContenedorDetalle{{ pascal }}", () => {
test("debería mostrar el detalle cuando existe", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const api = apiFalsa([coleccion{{ pascal }}([sembrado])]);
const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
// Actuar
renderizarPantalla(pantalla, { api });
// Comprobar
expect(await screen.findByText("Editar")).toBeTruthy();
});
{%- for r in references %}
test("debería enlazar {{ r.label }} con su página y su etiqueta", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
{%- if r.target_inline %}
const objetivo = Madre{{ r.target_pascal }}.aleatorio({ id: sembrado.{{ r.camel }}{% if not r.required %} ?? crypto.randomUUID(){% endif %} });
{%- else %}
const objetivo = Madre{{ r.target_pascal }}.aleatorio({
id: sembrado.{{ r.camel }}{% if not r.required %} ?? crypto.randomUUID(){% endif %},
});
{%- endif %}
{%- if r.api_inline %}
const api = apiFalsa([coleccion{{ pascal }}([sembrado]), coleccion{{ r.target_pascal }}([objetivo])]);
{%- else %}
const api = apiFalsa([
coleccion{{ pascal }}([sembrado]),
coleccion{{ r.target_pascal }}([objetivo]),
]);
{%- endif %}
const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
// Actuar
renderizarPantalla(pantalla, { api });
// Comprobar: por su href; dos referencias al mismo objetivo pueden tener la misma etiqueta.
const selector = `a[href="{{ r.ref_path }}/${sembrado.{{ r.camel }}}"]`;
expect(await screen.findByText(objetivo.{{ r.target_label }}, { selector })).toBeTruthy();
});
{%- endfor %}
test("debería mostrar el código de no encontrado cuando no existe", async () => {
// Preparar
const api = apiFalsa([coleccion{{ pascal }}()]);
const pantalla = <ContenedorDetalle{{ pascal }} id={crypto.randomUUID()} />;
// Actuar
renderizarPantalla(pantalla, { api });
// Comprobar
expect((await screen.findByRole("alert")).textContent).toBe("{{ snake }}.no_encontrado");
});
test("debería borrar solo tras confirmar el borrado", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const guardados = coleccion{{ pascal }}([sembrado]);
const api = apiFalsa([guardados]);
const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
const { historial } = renderizarPantalla(pantalla, { api });
fireEvent.click(await screen.findByText("Borrar", { selector: ".card-actions button" }));
expect(guardados.elementos.has(sembrado.id)).toBe(true);
// Actuar
fireEvent.click(screen.getByText("Borrar", { selector: ".modal-action button" }));
// Comprobar
await waitFor(() => expect(historial.at(-1)).toBe("/{{ plural }}"));
expect(guardados.elementos.has(sembrado.id)).toBe(false);
});
{%- for action in ["Editar", "Borrar"] %}
{%- if action == "Editar" %}{% set guard = access.update_roles %}{% else %}{% set guard = access.delete_roles %}{% endif %}
{%- if guard %}
test("no debería ofrecer {{ action }} a quien no tiene su rol", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const api = apiFalsa([coleccion{{ pascal }}([sembrado])]);
const usuario = MadreUsuarioActual.aleatorio();
const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
// Actuar
renderizarPantalla(pantalla, { api }, "/", { usuario });
// Comprobar
expect(await screen.findByText("{{ fields[0].label }}", { selector: "dt" })).toBeTruthy();
expect(screen.queryByText("{{ action }}", { selector: ".card-actions *" })).toBeNull();
});
{%- endif %}
{%- endfor %}
{%- if access.delete_protected %}
test("debería mostrar auth.prohibido cuando la API rechaza el borrado", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const fallos = { "DELETE /{{ plural }}": "auth.prohibido" };
const api = apiFalsa([coleccion{{ pascal }}([sembrado])], fallos);
const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
renderizarPantalla(pantalla, { api });
fireEvent.click(await screen.findByText("Borrar", { selector: ".card-actions button" }));
// Actuar
fireEvent.click(screen.getByText("Borrar", { selector: ".modal-action button" }));
// Comprobar
expect((await screen.findByRole("alert")).textContent).toBe("auth.prohibido");
});
{%- endif %}
});