puerto 0.34.0

Puerto: la CLI de un framework full-stack DDD en Rust. Genera proyectos con Clean Architecture, la inyección de dependencias cableada sola y soporte de SQLx, sin cablear nada a mano. Antes, puerto-framework.
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 or owner_camel -%}
import { MadreUsuarioActual } from "../../../pruebas/auth";
{% endif -%}
{% for t in targets | filter(attribute="is_self", value=false) -%}
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 }}";

{% if owner_camel -%}
/** Quien edita y borra en estos tests: el propietario de lo que se siembra (27.18). */
const PROPIETARIO = MadreUsuarioActual.llamante();

{% endif -%}
describe("ContenedorDetalle{{ pascal }}", () => {
  test("debería mostrar el detalle cuando existe", async () => {
    // Preparar
    const sembrado = Madre{{ pascal }}.aleatorio({% if owner_camel %}{ {{ owner_camel }}: PROPIETARIO.id }{% endif %});
    const api = apiFalsa([coleccion{{ pascal }}([sembrado])]);

    const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;

    // Actuar
    renderizarPantalla(pantalla, { api }{% if owner_camel %}, "/", { usuario: PROPIETARIO }{% endif %});

    // Comprobar
    expect(await screen.findByText("Editar")).toBeTruthy();
  });
{%- for r in references %}

  test("debería {% if r.ref_path %}enlazar {{ r.label }} con su página y su etiqueta{% else %}nombrar {{ r.label }} por su etiqueta{% endif %}", async () => {
    // Preparar
{%- if r.is_set %}
    const objetivo = Madre{{ r.target_pascal }}.aleatorio();
{%- if r.target_inline %}
    const sembrado = Madre{{ pascal }}.aleatorio({ {{ r.camel }}: [objetivo.id] });
{%- else %}
    const sembrado = Madre{{ pascal }}.aleatorio({
      {{ r.camel }}: [objetivo.id],
    });
{%- endif %}
{%- if r.is_self %}
    const api = apiFalsa([coleccion{{ pascal }}([sembrado, objetivo])]);
{%- elif 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 %}
{%- elif r.is_self %}
    const objetivo = Madre{{ pascal }}.aleatorio();
{%- if r.target_inline %}
    const sembrado = Madre{{ pascal }}.aleatorio({ {{ r.camel }}: objetivo.id });
{%- else %}
    const sembrado = Madre{{ pascal }}.aleatorio({
      {{ r.camel }}: objetivo.id,
    });
{%- endif %}
    const api = apiFalsa([coleccion{{ pascal }}([sembrado, objetivo])]);
{%- else %}
    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 %}
{%- endif %}
    const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;

    // Actuar
    renderizarPantalla(pantalla, { api });

{%- if r.ref_path %}
    // Comprobar: por su href; dos referencias al mismo objetivo pueden tener la misma etiqueta.
    const selector = `a[href="{{ r.ref_path }}/${% if r.is_set %}{objetivo.id}{% else %}{sembrado.{{ r.camel }}}{% endif %}"]`;
{%- else %}
    // Comprobar: sin enlace, un usuario no tiene página.
    const selector = "span";
{%- endif %}
    expect(await screen.findByText({{ r.target_label_of }}, { 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({% if owner_camel %}{ {{ owner_camel }}: PROPIETARIO.id }{% endif %});
    const guardados = coleccion{{ pascal }}([sembrado]);
    const api = apiFalsa([guardados]);
    const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
    const { historial } = renderizarPantalla(pantalla, { api }{% if owner_camel %}, "/", { usuario: PROPIETARIO }{% endif %});
    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({% if owner_camel %}{ {{ owner_camel }}: PROPIETARIO.id }{% endif %});
    const fallos = { "DELETE /{{ plural }}": "auth.prohibido" };
    const api = apiFalsa([coleccion{{ pascal }}([sembrado])], fallos);
    const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;
    renderizarPantalla(pantalla, { api }{% if owner_camel %}, "/", { usuario: PROPIETARIO }{% endif %});
    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 %}
{%- if owner_camel %}

  test("no debería ofrecer Editar ni Borrar a quien no es su propietario", 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("Editar", { selector: ".card-actions *" })).toBeNull();
    expect(screen.queryByText("Borrar", { selector: ".card-actions *" })).toBeNull();
  });

{%- if access.update_owner or access.delete_owner %}

  test("debería ofrecer {% if access.update_owner and access.delete_owner %}Editar y Borrar{% elif access.update_owner %}Editar{% else %}Borrar{% endif %} a su propietario aunque no tenga roles", async () => {
    // Preparar
    const usuario = MadreUsuarioActual.aleatorio();
    const sembrado = Madre{{ pascal }}.aleatorio({ {{ owner_camel }}: usuario.id });
    const api = apiFalsa([coleccion{{ pascal }}([sembrado])]);
    const pantalla = <ContenedorDetalle{{ pascal }} id={sembrado.id} />;

    // Actuar
    renderizarPantalla(pantalla, { api }, "/", { usuario });

    // Comprobar
{%- if access.update_owner and access.delete_owner %}
    expect(await screen.findByText("Editar", { selector: ".card-actions *" })).toBeTruthy();
    expect(screen.getByText("Borrar", { selector: ".card-actions *" })).toBeTruthy();
{%- elif access.update_owner %}
    expect(await screen.findByText("Editar", { selector: ".card-actions *" })).toBeTruthy();
    // Borrar es solo de su rol (`solo_rol`).
    expect(screen.queryByText("Borrar", { selector: ".card-actions *" })).toBeNull();
{%- else %}
    expect(await screen.findByText("Borrar", { selector: ".card-actions *" })).toBeTruthy();
    // Editar es solo de su rol (`solo_rol`).
    expect(screen.queryByText("Editar", { selector: ".card-actions *" })).toBeNull();
{%- endif %}
  });
{%- endif %}
{%- endif %}
});