import { describe, expect, test } from "bun:test";
import { fireEvent, screen, waitFor } from "@testing-library/react";
import { apiQueApunta } from "../../../pruebas/api-que-apunta";
import { Madre{{ pascal }} } from "../../../pruebas/{{ kebab }}";
import { renderizarPantalla } from "../../../pruebas/renderizar";
import { ContenedorFicheros{{ pascal }} } from "./ficheros-{{ kebab }}";
const FICHERO = { id: "f1", nombre: "foto.png", tipoContenido: "image/png", tamano: 3 };
describe("ContenedorFicheros{{ pascal }}", () => {
test("debería subir {{ first.name }} a su ruta, con su nombre y su tipo", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const { api, peticiones } = apiQueApunta(FICHERO);
{{ editable_screen }}
renderizarPantalla(pantalla, { api });
const archivo = new File(["png"], "foto.png", { type: "image/png" });
// Actuar
const entrada = await screen.findByLabelText("Subir {{ first.label }}");
fireEvent.change(entrada, { target: { files: [archivo] } });
// Comprobar
await waitFor(() => expect(peticiones).toHaveLength(1));
const [peticion] = peticiones;
const url = new URL(peticion?.url ?? "");
expect(peticion?.method).toBe("{% if first.list %}POST{% else %}PUT{% endif %}");
expect(url.pathname).toBe(`/api/{{ plural }}/${sembrado.id}/{{ first.name }}`);
expect(url.searchParams.get("nombre")).toBe("foto.png");
expect(peticion?.headers.get("Content-Type")).toBe("image/png");
});
test("debería no ofrecer subir ni quitar cuando no se puede editar", async () => {
// Preparar
const sembrado = Madre{{ pascal }}.aleatorio();
const { api } = apiQueApunta(FICHERO);
{{ readonly_screen }}
// Actuar
renderizarPantalla(pantalla, { api });
// Comprobar
expect(await screen.findByText("{{ first.label }}")).toBeTruthy();
expect(screen.queryByLabelText("Subir {{ first.label }}")).toBeNull();
});
});