|
|
|
@@ -0,0 +1,118 @@
|
|
|
|
|
// @vitest-environment jsdom
|
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
|
|
|
|
|
|
|
|
import BarraFiltros from "../../src/components/BarraFiltros.jsx";
|
|
|
|
|
import { PROVINCIAS } from "../../src/constants/provincias.js";
|
|
|
|
|
|
|
|
|
|
function renderBarra(over = {}) {
|
|
|
|
|
const props = {
|
|
|
|
|
locales: [],
|
|
|
|
|
favoritos: [],
|
|
|
|
|
busqueda: "", setBusqueda: vi.fn(),
|
|
|
|
|
filtroCategoria: "", setFiltroCategoria: vi.fn(),
|
|
|
|
|
filtroProvincia: "", setFiltroProvincia: vi.fn(),
|
|
|
|
|
orden: "fecha", setOrden: vi.fn(),
|
|
|
|
|
soloFavoritos: false, setSoloFavoritos: vi.fn(),
|
|
|
|
|
ubicacion: null, buscandoUbicacion: false, errorUbicacion: "",
|
|
|
|
|
pedirUbicacion: vi.fn(), limpiarUbicacion: vi.fn(),
|
|
|
|
|
...over,
|
|
|
|
|
};
|
|
|
|
|
const utils = render(<BarraFiltros {...props} />);
|
|
|
|
|
return { ...utils, props };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
afterEach(cleanup);
|
|
|
|
|
|
|
|
|
|
describe("BarraFiltros", () => {
|
|
|
|
|
it("Sección única con búsqueda, provincia, categorías y acciones rápidas", () => {
|
|
|
|
|
const { container } = renderBarra();
|
|
|
|
|
expect(screen.getByRole("region", { name: /filtros de locales/i })).toBeTruthy();
|
|
|
|
|
expect(screen.getByLabelText(/buscar por nombre, dirección o provincia/i)).toBeTruthy();
|
|
|
|
|
expect(screen.getByLabelText(/filtrar por provincia/i)).toBeTruthy();
|
|
|
|
|
expect(screen.getByRole("button", { name: /restauración/i })).toBeTruthy();
|
|
|
|
|
expect(screen.getByRole("button", { name: /favoritos/i })).toBeTruthy();
|
|
|
|
|
expect(screen.getByRole("group", { name: /ordenar locales/i })).toBeTruthy();
|
|
|
|
|
// Todo vive dentro de la misma sección
|
|
|
|
|
expect(container.querySelectorAll("section[aria-label]").length).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("No ofrece control de subcategoría y el único select es el de provincia", () => {
|
|
|
|
|
const { container } = renderBarra({ filtroCategoria: "Restauración" });
|
|
|
|
|
expect(screen.queryByLabelText(/tipo específico/i)).toBeNull();
|
|
|
|
|
expect(screen.queryByRole("option", { name: /todos los tipos/i })).toBeNull();
|
|
|
|
|
const selects = container.querySelectorAll("select");
|
|
|
|
|
expect(selects.length).toBe(1);
|
|
|
|
|
expect(selects[0].getAttribute("aria-label")).toMatch(/provincia/i);
|
|
|
|
|
// Todas las provincias disponibles en el desplegable compacto
|
|
|
|
|
expect(selects[0].querySelectorAll("option").length).toBe(PROVINCIAS.length + 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("El buscador comunica el filtrado por provincia y notifica los cambios", () => {
|
|
|
|
|
const { props } = renderBarra();
|
|
|
|
|
const input = screen.getByLabelText(/buscar por nombre, dirección o provincia/i);
|
|
|
|
|
expect(input.getAttribute("placeholder")).toMatch(/provincia/i);
|
|
|
|
|
fireEvent.change(input, { target: { value: "Madrid" } });
|
|
|
|
|
expect(props.setBusqueda).toHaveBeenCalledWith("Madrid");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Chips de categoría: conteo, aria-pressed y alternado", () => {
|
|
|
|
|
const locales = [
|
|
|
|
|
{ id: "1", categoria: "Restauración" },
|
|
|
|
|
{ id: "2", categoria: "Restauración" },
|
|
|
|
|
{ id: "3", categoria: "Otros" },
|
|
|
|
|
];
|
|
|
|
|
const { props } = renderBarra({ locales, filtroCategoria: "Restauración" });
|
|
|
|
|
const chip = screen.getByRole("button", { name: /restauración/i });
|
|
|
|
|
expect(chip.getAttribute("aria-pressed")).toBe("true");
|
|
|
|
|
expect(chip.textContent).toContain("2"); // conteo de locales de la categoría
|
|
|
|
|
fireEvent.click(chip);
|
|
|
|
|
expect(props.setFiltroCategoria).toHaveBeenCalledWith(""); // pulsar de nuevo la deselecciona
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Orden segmentado: sin desplegable y con distancia solo si hay ubicación", () => {
|
|
|
|
|
const sinUbicacion = renderBarra();
|
|
|
|
|
expect(screen.queryByRole("button", { name: /más cerca/i })).toBeNull();
|
|
|
|
|
expect(sinUbicacion.container.querySelectorAll("select").length).toBe(1);
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /mejor/i }));
|
|
|
|
|
expect(sinUbicacion.props.setOrden).toHaveBeenCalledWith("puntuacion");
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
|
|
const conUbicacion = renderBarra({ ubicacion: { lat: 40.4, lng: -3.7 }, orden: "distancia" });
|
|
|
|
|
const cerca = screen.getByRole("button", { name: /más cerca/i });
|
|
|
|
|
expect(cerca.getAttribute("aria-pressed")).toBe("true");
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /reciente/i }));
|
|
|
|
|
expect(conUbicacion.props.setOrden).toHaveBeenCalledWith("fecha");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("«✕ Limpiar» aparece solo con filtros activos y resetea todos", () => {
|
|
|
|
|
renderBarra();
|
|
|
|
|
expect(screen.queryByRole("button", { name: /limpiar/i })).toBeNull();
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
|
|
const { props } = renderBarra({
|
|
|
|
|
busqueda: "pizza",
|
|
|
|
|
filtroCategoria: "Restauración",
|
|
|
|
|
filtroProvincia: "Madrid",
|
|
|
|
|
orden: "puntuacion",
|
|
|
|
|
soloFavoritos: true,
|
|
|
|
|
ubicacion: { lat: 40.4, lng: -3.7 },
|
|
|
|
|
});
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: /limpiar/i }));
|
|
|
|
|
expect(props.setBusqueda).toHaveBeenCalledWith("");
|
|
|
|
|
expect(props.setFiltroCategoria).toHaveBeenCalledWith("");
|
|
|
|
|
expect(props.setFiltroProvincia).toHaveBeenCalledWith("");
|
|
|
|
|
expect(props.setOrden).toHaveBeenCalledWith("fecha");
|
|
|
|
|
expect(props.setSoloFavoritos).toHaveBeenCalledWith(false);
|
|
|
|
|
expect(props.limpiarUbicacion).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Favoritos refleja el conteo y su estado", () => {
|
|
|
|
|
const { props } = renderBarra({ favoritos: ["a", "b"], soloFavoritos: true });
|
|
|
|
|
const boton = screen.getByRole("button", { name: /favoritos \(2\)/i });
|
|
|
|
|
expect(boton.getAttribute("aria-pressed")).toBe("true");
|
|
|
|
|
fireEvent.click(boton);
|
|
|
|
|
const alternar = props.setSoloFavoritos.mock.calls[0][0];
|
|
|
|
|
expect(alternar(false)).toBe(true); // setSoloFavoritos recibe la función de alternado
|
|
|
|
|
});
|
|
|
|
|
});
|