// @vitest-environment jsdom import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { act, cleanup, renderHook, waitFor } from "@testing-library/react"; // Mock del cliente API (firebase.js re-exporta api.js: el mock intercepta la // resolución del módulo y cubre ambas vías). vi.mock("../../src/api.js", () => ({ listarResenas: vi.fn(), enviarResena: vi.fn(), borrarResena: vi.fn(), reportarLocal: vi.fn(), })); import useResenas from "../../src/hooks/useResenas.js"; import { borrarResena, enviarResena, listarResenas, reportarLocal } from "../../src/api.js"; const ISO = "2026-10-11T10:00:00.000Z"; const PROPIA = { id: "r9", huella: "#999999", puntuacion: 5, comentario: "Mi favorito", fecha: ISO, fechaEdicion: null, esPropia: true }; beforeEach(() => { vi.clearAllMocks(); localStorage.clear(); }); afterEach(cleanup); describe("useResenas (dueño único del estado de reseñas)", () => { it("carga al montar y precarga la reseña propia", async () => { listarResenas.mockResolvedValue({ resenas: [PROPIA], total: 1 }); const { result } = renderHook(() => useResenas("loc-1", { totalInicial: 1, mediaInicial: 4 })); await waitFor(() => expect(result.current.resenas).not.toBeNull()); expect(listarResenas).toHaveBeenCalledWith("loc-1", expect.any(String)); expect(result.current.cargando).toBe(false); expect(result.current.total).toBe(1); // La media la aporta el local (el listado no la devuelve) expect(result.current.media).toBe(4); expect(result.current.tienePropia).toBe(true); expect(result.current.puntuacion).toBe(5); expect(result.current.comentario).toBe("Mi favorito"); }); it("enviar manda el deviceId y refresca lista, media y total", async () => { listarResenas .mockResolvedValueOnce({ resenas: [], total: 0 }) .mockResolvedValueOnce({ resenas: [PROPIA], total: 1 }); enviarResena.mockResolvedValue({ ok: true, media: 4, total: 1 }); const { result } = renderHook(() => useResenas("loc-1")); await waitFor(() => expect(result.current.resenas).toEqual([])); act(() => { result.current.setPuntuacion(4); result.current.setComentario("Muy bien"); }); await act(async () => { await result.current.enviar(); }); expect(enviarResena).toHaveBeenCalledWith("loc-1", { deviceId: expect.any(String), puntuacion: 4, comentario: "Muy bien", }); expect(result.current.media).toBe(4); expect(result.current.total).toBe(1); expect(result.current.tienePropia).toBe(true); expect(listarResenas).toHaveBeenCalledTimes(2); expect(localStorage.getItem("localesp_device_id")).toMatch(/./); }); it("sin puntuación no envía", async () => { listarResenas.mockResolvedValue({ resenas: [], total: 0 }); const { result } = renderHook(() => useResenas("loc-1")); await waitFor(() => expect(result.current.resenas).toEqual([])); await act(async () => { await result.current.enviar(); }); expect(enviarResena).not.toHaveBeenCalled(); }); it("el error del servidor se expone como mensaje", async () => { listarResenas.mockResolvedValue({ resenas: [], total: 0 }); enviarResena.mockRejectedValue(new Error("El comentario contiene palabras no permitidas")); const { result } = renderHook(() => useResenas("loc-1")); await waitFor(() => expect(result.current.resenas).toEqual([])); act(() => result.current.setPuntuacion(3)); await act(async () => { await result.current.enviar(); }); expect(result.current.error).toMatch(/no permitidas/i); expect(result.current.enviando).toBe(false); }); it("borrar la propia recalcula la media y limpia el formulario", async () => { listarResenas .mockResolvedValueOnce({ resenas: [PROPIA], total: 1 }) .mockResolvedValueOnce({ resenas: [], total: 0 }); borrarResena.mockResolvedValue({ ok: true, media: 0, total: 0 }); const { result } = renderHook(() => useResenas("loc-1", { totalInicial: 1, mediaInicial: 5 })); await waitFor(() => expect(result.current.tienePropia).toBe(true)); await act(async () => { await result.current.borrarPropia(); }); expect(borrarResena).toHaveBeenCalledWith("loc-1", expect.any(String)); expect(result.current.media).toBe(0); expect(result.current.tienePropia).toBe(false); expect(result.current.puntuacion).toBe(0); expect(result.current.comentario).toBe(""); }); it("reportar marca la reseña reportada", async () => { listarResenas.mockResolvedValue({ resenas: [{ id: "r1", huella: "#111", puntuacion: 5, comentario: "Uno", fecha: ISO, esPropia: false }], total: 1 }); reportarLocal.mockResolvedValue({ ok: true }); const { result } = renderHook(() => useResenas("loc-1")); await waitFor(() => expect(result.current.resenas?.length).toBe(1)); await act(async () => { await result.current.reportar(result.current.resenas[0]); }); expect(reportarLocal).toHaveBeenCalledWith("loc-1", "inapropiado", "", "r1"); expect(result.current.reportada).toBe("r1"); }); it("si falla la carga lo avisa y deja de cargar", async () => { listarResenas.mockRejectedValue(new Error("boom")); const { result } = renderHook(() => useResenas("loc-1")); await waitFor(() => expect(result.current.error).toMatch(/no se pudieron cargar/i)); expect(result.current.cargando).toBe(false); expect(result.current.resenas).toBeNull(); }); });