import { describe, expect, test } from "bun:test";
import { fireEvent, screen } from "@testing-library/react";
import { apiFalsa{% if owner_camel %}, coleccion{% endif %} } from "../../../pruebas/api-falsa";
{% if access.create_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 { ContenedorLista{{ pascal }} } from "./lista-{{ kebab }}";
describe("ContenedorLista{{ pascal }}", () => {
{%- if owner_camel %}
test("debería mostrar solo las propias cuando se le pide", async () => {
// Preparar: `GET /mis_{{ plural }}` responde las de quien llama; la lista entera, otras
const otras = coleccion{{ pascal }}([Madre{{ pascal }}.aleatorio(), Madre{{ pascal }}.aleatorio()]);
const propias = coleccion("/mis_{{ plural }}", "{{ snake }}", [Madre{{ pascal }}.aleatorio()]);
const api = apiFalsa([otras, propias]);
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} propias />, { api });
// Comprobar
expect(await screen.findByText("Mis {{ words_plural }}")).toBeTruthy();
expect(await screen.findAllByText("Ver")).toHaveLength(1);
});
{%- endif %}
test("debería mostrar una fila por cada {{ words }}", async () => {
// Preparar
const semilla = [Madre{{ pascal }}.aleatorio(), Madre{{ pascal }}.aleatorio()];
const api = apiFalsa([coleccion{{ pascal }}(semilla)]);
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api });
// Comprobar
expect(await screen.findAllByText("Ver")).toHaveLength(2);
});
test("debería mostrar la página siguiente cuando se pulsa Siguiente", async () => {
// Preparar
const semilla = Array.from({ length: 25 }, () => Madre{{ pascal }}.aleatorio());
const api = apiFalsa([coleccion{{ pascal }}(semilla)]);
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api });
expect(await screen.findAllByText("Ver")).toHaveLength(20);
// Actuar
fireEvent.click(screen.getByRole("button", { name: "Siguiente" }));
// Comprobar
expect(await screen.findByText("21–25 de 25")).toBeTruthy();
expect(screen.getAllByText("Ver")).toHaveLength(5);
});
test("debería abrir la página que nombra la URL", async () => {
// Preparar
const semilla = Array.from({ length: 25 }, () => Madre{{ pascal }}.aleatorio());
const api = apiFalsa([coleccion{{ pascal }}(semilla)]);
const ruta = "/{{ plural }}?pagina=2";
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api }, ruta);
// Comprobar
expect(await screen.findByText("21–25 de 25")).toBeTruthy();
});
test("debería decirlo cuando no hay {{ words_plural }}", async () => {
// Preparar
const api = apiFalsa([coleccion{{ pascal }}()]);
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api });
// Comprobar
expect(await screen.findByText("Todavía no hay {{ words_plural }}.")).toBeTruthy();
});
test("debería mostrar el código del error cuando la API no da la lista", async () => {
// Preparar
const fallos = { "GET /{{ plural }}": "{{ snake }}.repositorio" };
const api = apiFalsa([coleccion{{ pascal }}()], fallos);
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api });
// Comprobar
expect((await screen.findByRole("alert")).textContent).toBe("{{ snake }}.repositorio");
});
{%- if access.create_roles %}
test("no debería ofrecer Crear a quien no tiene su rol", async () => {
// Preparar
const api = apiFalsa([coleccion{{ pascal }}([Madre{{ pascal }}.aleatorio()])]);
const usuario = MadreUsuarioActual.aleatorio();
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api }, "/", { usuario });
// Comprobar
expect(await screen.findByText("Ver")).toBeTruthy();
expect(screen.queryByText("Crear {{ words }}")).toBeNull();
});
{%- endif %}
{%- for r in references %}
test("debería nombrar {{ r.label }} por 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 %}
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { 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 %}
{%- for r in references %}{% if loop.first %}
test("debería nombrar {{ r.label }} por su id cuando no se puede cargar", async () => {
// Preparar: sin {{ r.target_pascal }} en la API falsa, su lista responde un 404.
const sembrado = Madre{{ pascal }}.aleatorio();
const api = apiFalsa([coleccion{{ pascal }}([sembrado])]);
// Actuar
renderizarPantalla(<ContenedorLista{{ pascal }} />, { api });
// Comprobar
const selector = `a[href="{{ r.ref_path }}/${sembrado.{{ r.camel }}}"]`;
expect(await screen.findByText(`${sembrado.{{ r.camel }}}`, { selector })).toBeTruthy();
});
{%- endif %}{% endfor %}
});