Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions src/components/map/category-chips.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

import { CategoryChips, EXAM_COMBO, matchesCombo } from "@/components/map/category-chips";
import { PLACE_TYPES, PLACE_TYPE_LABELS } from "@/lib/types";
import type { PlaceType } from "@/lib/types";

afterEach(cleanup);

const counts = Object.fromEntries(PLACE_TYPES.map((type, i) => [type, i + 1])) as Record<
PlaceType,
number
>;

describe("CategoryChips (#352)", () => {
it("renders one chip per type, labelled exactly as PLACE_TYPE_LABELS", () => {
render(<CategoryChips selected={[]} counts={counts} onToggle={vi.fn()} />);

const group = screen.getByRole("group", { name: "Filter by place type" });
for (const type of PLACE_TYPES) {
expect(group.textContent).toContain(PLACE_TYPE_LABELS[type]);
}
});

it("is the only filter row: no second preset group", () => {
render(
<CategoryChips
selected={[]}
counts={counts}
onToggle={vi.fn()}
onSetTypes={vi.fn()}
onClear={vi.fn()}
/>,
);

expect(screen.queryByRole("group", { name: "Filter presets" })).toBeNull();
expect(screen.getAllByRole("group")).toHaveLength(1);
});

it("toggles a single type", () => {
const onToggle = vi.fn();
render(<CategoryChips selected={[]} counts={counts} onToggle={onToggle} />);

fireEvent.click(
screen.getByRole("button", { name: new RegExp(PLACE_TYPE_LABELS.airport) }),
);

expect(onToggle).toHaveBeenCalledWith("airport");
});

it("offers the exam-centres combo only when onSetTypes is given", () => {
const { rerender } = render(
<CategoryChips selected={[]} counts={counts} onToggle={vi.fn()} />,
);
expect(screen.queryByRole("button", { name: /All exam centres/ })).toBeNull();

rerender(
<CategoryChips
selected={[]}
counts={counts}
onToggle={vi.fn()}
onSetTypes={vi.fn()}
/>,
);
expect(screen.getByRole("button", { name: /All exam centres/ })).toBeTruthy();
});

it("selects both exam types from the combo chip, and clears them on a second tap", () => {
const onSetTypes = vi.fn();
const { rerender } = render(
<CategoryChips
selected={[]}
counts={counts}
onToggle={vi.fn()}
onSetTypes={onSetTypes}
/>,
);
fireEvent.click(screen.getByRole("button", { name: /All exam centres/ }));
expect(onSetTypes).toHaveBeenLastCalledWith([...EXAM_COMBO]);

rerender(
<CategoryChips
selected={[...EXAM_COMBO]}
counts={counts}
onToggle={vi.fn()}
onSetTypes={onSetTypes}
/>,
);
const chip = screen.getByRole("button", { name: /All exam centres/ });
expect(chip.getAttribute("aria-pressed")).toBe("true");
fireEvent.click(chip);
expect(onSetTypes).toHaveBeenLastCalledWith([]);
});

it("shows a selected count and a Clear action only while something is selected", () => {
const onClear = vi.fn();
const { rerender } = render(
<CategoryChips
selected={[]}
counts={counts}
onToggle={vi.fn()}
onClear={onClear}
/>,
);
expect(screen.queryByRole("button", { name: "Clear" })).toBeNull();

rerender(
<CategoryChips
selected={["library", "airport"]}
counts={counts}
onToggle={vi.fn()}
onClear={onClear}
/>,
);
expect(screen.getByText(/2 categories selected/)).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: "Clear" }));
expect(onClear).toHaveBeenCalledOnce();
});

it("uses the singular for one selected category", () => {
render(
<CategoryChips
selected={["library"]}
counts={counts}
onToggle={vi.fn()}
onClear={vi.fn()}
/>,
);
expect(screen.getByText(/1 category selected/)).toBeTruthy();
});

it("fades the right edge of the scrolling row instead of clipping a chip", () => {
render(<CategoryChips selected={[]} counts={counts} onToggle={vi.fn()} scroll />);

const group = screen.getByRole("group", { name: "Filter by place type" });
expect(group.className).toContain("overflow-x-auto");
expect(group.className).toContain("mask-image");
});
});

describe("matchesCombo", () => {
it("matches the same types in any order", () => {
expect(matchesCombo(["foreign_lang_exam_centre", "sat_centre"], EXAM_COMBO)).toBe(
true,
);
});

it("does not match a subset, a superset or an empty selection", () => {
expect(matchesCombo(["sat_centre"], EXAM_COMBO)).toBe(false);
expect(matchesCombo([...EXAM_COMBO, "library"], EXAM_COMBO)).toBe(false);
expect(matchesCombo([], EXAM_COMBO)).toBe(false);
});
});
166 changes: 123 additions & 43 deletions src/components/map/category-chips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,152 @@ import type { PlaceType } from "@/lib/types";
import { PLACE_TYPE_COLORS } from "@/lib/map";
import { cn } from "@/lib/utils";

/**
* A multi-type shortcut that single-type chips cannot express: SAT and
* foreign-language exam centres are both "somewhere a student sits a timed
* exam". It is the only combo, so it sits in front of the chips instead of in a
* second filter bar (#352).
*/
export const EXAM_COMBO: readonly PlaceType[] = [
"sat_centre",
"foreign_lang_exam_centre",
];

/** True when `selected` is exactly `combo`, in any order. */
export function matchesCombo(
selected: readonly PlaceType[],
combo: readonly PlaceType[],
) {
return (
selected.length === combo.length && combo.every((type) => selected.includes(type))
);
}

interface CategoryChipsProps {
selected: PlaceType[];
counts: Record<PlaceType, number>;
onToggle: (type: PlaceType) => void;
/** Replaces the whole type selection; enables the leading exam-centres chip. */
onSetTypes?: (types: PlaceType[]) => void;
/** Clears the type selection; enables the "N selected, Clear" line. */
onClear?: () => void;
/** When true, lay out as a single horizontally scrolling row (mobile). */
scroll?: boolean;
}

/**
* Category filters as colour-coded toggle chips. Each chip doubles as the map
* legend (colour dot matches the pin) and shows how many of that type are
* currently visible. Selecting none means everything shows.
* currently visible. Selecting none means everything shows. The label of every
* chip is the one used everywhere else for that type (`PLACE_TYPE_LABELS`).
*/
export function CategoryChips({
selected,
counts,
onToggle,
onSetTypes,
onClear,
scroll = false,
}: CategoryChipsProps) {
const examCount = EXAM_COMBO.reduce((sum, type) => sum + counts[type], 0);
const examActive = matchesCombo(selected, EXAM_COMBO);

return (
<div
className={cn(
"flex gap-1.5",
scroll
? "overflow-x-auto pb-1 [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
: "flex-wrap",
)}
role="group"
aria-label="Filter by place type"
>
{PLACE_TYPES.map((type) => {
const active = selected.includes(type);
const color = PLACE_TYPE_COLORS[type];
return (
<div className="flex flex-col gap-1.5">
{onClear && selected.length > 0 && (
<div className="flex items-center justify-between text-xs">
<span className="text-muted-foreground">
{selected.length} {selected.length === 1 ? "category" : "categories"} selected
</span>
<button
key={type}
type="button"
aria-pressed={active}
onClick={() => onToggle(type)}
className={cn(
"inline-flex shrink-0 items-center gap-1.5 rounded-full border px-3.5 py-2 text-[13px] font-medium transition-colors sm:px-3 sm:py-1.5",
active
? "border-transparent text-foreground"
: "border-border bg-card text-muted-foreground hover:bg-muted hover:text-foreground",
)}
style={
active
? {
backgroundColor: `color-mix(in oklab, ${color} 16%, transparent)`,
boxShadow: `inset 0 0 0 1px ${color}`,
}
: undefined
}
onClick={onClear}
className="text-primary underline-offset-2 hover:underline"
>
<span
aria-hidden
className="size-2.5 shrink-0 rounded-full"
style={{ backgroundColor: color }}
/>
<span className="whitespace-nowrap">{PLACE_TYPE_LABELS[type]}</span>
<span className="tabular-nums text-[11px] text-muted-foreground">
{counts[type]}
</span>
Clear
</button>
);
})}
</div>
)}
<div
className={cn(
"flex gap-1.5",
// The right edge fades out and the row ends in padding, so a chip cut
// off by the panel edge reads as "scroll for more", not as a bug.
scroll
? "overflow-x-auto pb-1 pr-7 [mask-image:linear-gradient(to_right,#000_calc(100%-28px),transparent)] [&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]"
: "flex-wrap",
)}
role="group"
aria-label="Filter by place type"
>
{onSetTypes && (
<Chip
active={examActive}
color={PLACE_TYPE_COLORS.sat_centre}
label="All exam centres"
title={EXAM_COMBO.map((type) => PLACE_TYPE_LABELS[type]).join(" + ")}
count={examCount}
onClick={() => onSetTypes(examActive ? [] : [...EXAM_COMBO])}
/>
)}
{PLACE_TYPES.map((type) => (
<Chip
key={type}
active={selected.includes(type)}
color={PLACE_TYPE_COLORS[type]}
label={PLACE_TYPE_LABELS[type]}
count={counts[type]}
onClick={() => onToggle(type)}
/>
))}
</div>
</div>
);
}

function Chip({
active,
color,
label,
title,
count,
onClick,
}: {
active: boolean;
color: string;
label: string;
title?: string;
count: number;
onClick: () => void;
}) {
return (
<button
type="button"
aria-pressed={active}
title={title}
onClick={onClick}
className={cn(
"inline-flex shrink-0 items-center gap-1.5 rounded-full border px-3.5 py-2 text-[13px] font-medium transition-colors sm:px-3 sm:py-1.5",
active
? "border-transparent text-foreground"
: "border-border bg-card text-muted-foreground hover:bg-muted hover:text-foreground",
)}
style={
active
? {
backgroundColor: `color-mix(in oklab, ${color} 16%, transparent)`,
boxShadow: `inset 0 0 0 1px ${color}`,
}
: undefined
}
>
<span
aria-hidden
className="size-2.5 shrink-0 rounded-full"
style={{ backgroundColor: color }}
/>
<span className="whitespace-nowrap">{label}</span>
<span className="tabular-nums text-[11px] text-muted-foreground">{count}</span>
</button>
);
}
42 changes: 0 additions & 42 deletions src/components/map/filter-presets.test.tsx

This file was deleted.

Loading
Loading