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 { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { CampoFichero, CampoFicheros, type DatosFichero, tamanoLegible } from "./fichero";

const FOTO: DatosFichero = {
  id: "f1",
  nombre: "foto.png",
  tipoContenido: "image/png",
  tamano: 2048,
};
const INFORME: DatosFichero = {
  id: "f2",
  nombre: "informe.pdf",
  tipoContenido: "application/pdf",
  tamano: 10,
};

const crearUrl = URL.createObjectURL;
const liberarUrl = URL.revokeObjectURL;
const creadas = mock((_: Blob) => "blob:foto");
const liberadas = mock((_: string) => {});

beforeEach(() => {
  creadas.mockClear();
  liberadas.mockClear();
  URL.createObjectURL = creadas;
  URL.revokeObjectURL = liberadas;
});

afterEach(() => {
  URL.createObjectURL = crearUrl;
  URL.revokeObjectURL = liberarUrl;
});

describe("CampoFichero", () => {
  test("debería pintar una imagen descargada y liberarla al desmontar", async () => {
    // Preparar
    const descargar = () => Promise.resolve(new Blob(["png"]));

    // Actuar
    const { unmount } = render(
      <CampoFichero etiqueta="Foto" fichero={FOTO} descargar={descargar} />,
    );

    // Comprobar
    const imagen = await screen.findByRole("img", { name: "foto.png" });
    expect(imagen.getAttribute("src")).toBe("blob:foto");
    unmount();
    expect(liberadas).toHaveBeenCalledWith("blob:foto");
  });

  test("debería enseñar un guion cuando no hay fichero", () => {
    // Actuar
    render(<CampoFichero etiqueta="Foto" fichero={null} descargar={() => Promise.reject()} />);

    // Comprobar
    expect(screen.getByText("—")).toBeDefined();
  });

  test("debería subir el fichero elegido", async () => {
    // Preparar
    const subir = mock((_: File) => Promise.resolve());
    const archivo = new File(["png"], "nueva.png", { type: "image/png" });
    render(
      <CampoFichero
        etiqueta="Foto"
        fichero={null}
        descargar={() => Promise.reject()}
        subir={subir}
      />,
    );

    // Actuar
    fireEvent.change(screen.getByLabelText("Subir Foto"), { target: { files: [archivo] } });

    // Comprobar
    await waitFor(() => expect(subir).toHaveBeenCalledWith(archivo));
  });

  test("debería ocultar subir y quitar cuando no se puede editar", () => {
    // Actuar
    render(
      <CampoFichero etiqueta="Informe" fichero={INFORME} descargar={() => Promise.reject()} />,
    );

    // Comprobar
    expect(screen.queryByLabelText("Subir Informe")).toBeNull();
    expect(screen.queryByText("Quitar")).toBeNull();
  });

  test("debería decir lo que significa el error cuando no se puede subir", async () => {
    // Preparar
    const subir = () => Promise.reject({ codigo: "documento.portada_demasiado_grande" });
    const mensaje = () => "Foto: el fichero es demasiado grande";
    const archivo = new File(["png"], "enorme.png", { type: "image/png" });
    render(
      <CampoFichero
        etiqueta="Foto"
        fichero={null}
        descargar={() => Promise.reject()}
        subir={subir}
        mensaje={mensaje}
      />,
    );

    // Actuar
    fireEvent.change(screen.getByLabelText("Subir Foto"), { target: { files: [archivo] } });

    // Comprobar
    expect((await screen.findByRole("alert")).textContent).toBe(
      "Foto: el fichero es demasiado grande",
    );
  });
});

describe("CampoFicheros", () => {
  test("debería quitar el fichero que se pide", () => {
    // Preparar
    const quitar = mock((_: string) => Promise.resolve());
    render(
      <CampoFicheros
        etiqueta="Anexos"
        ficheros={[INFORME]}
        descargar={() => Promise.reject()}
        quitar={quitar}
      />,
    );

    // Actuar
    fireEvent.click(screen.getByText("Quitar"));

    // Comprobar
    expect(quitar).toHaveBeenCalledWith("f2");
  });
});

describe("tamanoLegible", () => {
  test("debería leer un tamaño en B, KB o MB", () => {
    expect(tamanoLegible(512)).toBe("512 B");
    expect(tamanoLegible(2048)).toBe("2 KB");
    expect(tamanoLegible(3.5 * 1024 * 1024)).toBe("3,5 MB");
  });
});