test(filtros): cubre BarraFiltros y mejora la accesibilidad de focos
- 7 pruebas del componente: sección única, sin subcategoría, buscador por provincia, chips con conteo/aria-pressed, orden segmentado (distancia solo con ubicación), Limpiar global y favoritos. - Foco visible en la barra vía :focus-visible (inputStyle anula el outline por defecto en inputs/selects). - Verificado que los marcadores del mapa usan localesFiltrados. 146/146 tests y build en verde.
This commit is contained in:
@@ -44,6 +44,9 @@
|
|||||||
}
|
}
|
||||||
* { box-sizing: border-box; }
|
* { box-sizing: border-box; }
|
||||||
body { margin: 0; background: var(--color-background-secondary); color: var(--color-text-primary); font-family: var(--font-sans); min-height: 100vh; }
|
body { margin: 0; background: var(--color-background-secondary); color: var(--color-text-primary); font-family: var(--font-sans); min-height: 100vh; }
|
||||||
|
/* Foco visible en la barra de filtros: sus controles comparten inputStyle,
|
||||||
|
que anula el outline del navegador (el resto de botones lo conservan). */
|
||||||
|
.barra-filtros :focus-visible { outline: 2px solid currentColor; outline-offset: 1px; }
|
||||||
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
|
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -19,6 +19,6 @@
|
|||||||
|
|
||||||
## 4. Verificación
|
## 4. Verificación
|
||||||
|
|
||||||
- [ ] 4.1 Tests: mantener/ajustar los tests de filtrado y cubrir la nueva barra (búsqueda por provincia, orden segmentado, limpiar global)
|
- [x] 4.1 Tests: mantener/ajustar los tests de filtrado y cubrir la nueva barra (búsqueda por provincia, orden segmentado, limpiar global)
|
||||||
- [ ] 4.2 Comprobar anchos estrechos (~360 px) y accesibilidad: `aria-pressed` en chips, `aria-label` en búsqueda y orden segmentado, foco visible
|
- [x] 4.2 Comprobar anchos estrechos (~360 px) y accesibilidad: `aria-pressed` en chips, `aria-label` en búsqueda y orden segmentado, foco visible
|
||||||
- [ ] 4.3 Verificar en la vista de mapa que los marcadores responden a los mismos filtros que la lista
|
- [x] 4.3 Verificar en la vista de mapa que los marcadores responden a los mismos filtros que la lista
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export default function BarraFiltros({
|
|||||||
return (
|
return (
|
||||||
<section
|
<section
|
||||||
aria-label="Filtros de locales"
|
aria-label="Filtros de locales"
|
||||||
|
className="barra-filtros"
|
||||||
style={{
|
style={{
|
||||||
background: "var(--color-background-primary)",
|
background: "var(--color-background-primary)",
|
||||||
border: "0.5px solid var(--color-border-tertiary)",
|
border: "0.5px solid var(--color-border-tertiary)",
|
||||||
|
|||||||
@@ -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
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user