diff --git a/e2e/tests/journeys.spec.ts b/e2e/tests/journeys.spec.ts index f3a218c..8c0c9b7 100644 --- a/e2e/tests/journeys.spec.ts +++ b/e2e/tests/journeys.spec.ts @@ -270,6 +270,9 @@ test('searching by name returns school results', async ({ page }) => { test('searching by postcode returns nearby schools', async ({ page }) => { await searchByName(page, 'B1 1BB'); await expect(schoolLinks(page).first()).toBeVisible({ timeout: 15_000 }); + // A new postcode search starts at half a mile. + await expect(page).toHaveURL(/[?&]radius=0\.5(&|$)/); + await expect(page.getByRole('combobox', { name: 'Distance' })).toHaveValue('0.5'); }); test('the phase filter switches straight from secondary to primary', async ({ page }) => { diff --git a/nextjs-app/__tests__/components/FilterBarRadius.test.tsx b/nextjs-app/__tests__/components/FilterBarRadius.test.tsx new file mode 100644 index 0000000..d6d200e --- /dev/null +++ b/nextjs-app/__tests__/components/FilterBarRadius.test.tsx @@ -0,0 +1,41 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { FilterBar } from '@/components/FilterBar'; + +const push = jest.fn(); +let searchParams = new URLSearchParams(); +jest.mock('next/navigation', () => ({ + useRouter: () => ({ push, replace: jest.fn(), prefetch: jest.fn() }), + usePathname: () => '/', + useSearchParams: () => searchParams, +})); + +const FILTERS = { + local_authorities: [], school_types: [], years: [], phases: [], + genders: [], admissions_policies: [], +}; + +beforeEach(() => { + push.mockClear(); + searchParams = new URLSearchParams(); +}); + +describe('FilterBar default distance', () => { + it('searches a new postcode within half a mile', async () => { + render(); + await userEvent.type(screen.getByPlaceholderText(/School name or postcode/i), 'SW19 6AR{Enter}'); + await waitFor(() => expect(push).toHaveBeenCalledWith(expect.stringContaining('radius=0.5'))); + }); + + it('shows half a mile when the URL carries a postcode but no radius', () => { + searchParams = new URLSearchParams('postcode=SW196AR'); + render(); + expect(screen.getByRole('combobox', { name: 'Distance' })).toHaveValue('0.5'); + }); + + it('keeps a distance the user already chose', async () => { + searchParams = new URLSearchParams('postcode=SW196AR&radius=3'); + render(); + expect(screen.getByRole('combobox', { name: 'Distance' })).toHaveValue('3'); + }); +}); diff --git a/nextjs-app/app/(frontend)/page.tsx b/nextjs-app/app/(frontend)/page.tsx index 378ae99..7faf807 100644 --- a/nextjs-app/app/(frontend)/page.tsx +++ b/nextjs-app/app/(frontend)/page.tsx @@ -6,7 +6,7 @@ import { absoluteUrl } from '@/lib/site'; import type { Metadata } from 'next'; import { fetchSchools, fetchFilters, fetchDataInfo } from '@/lib/api'; -import { formatAcademicYear } from '@/lib/utils'; +import { DEFAULT_RADIUS_MILES, formatAcademicYear } from '@/lib/utils'; import { HomeView } from '@/components/HomeView'; import { getFlags } from '@/lib/flags'; import { HowItWorksSection } from '@/components/HowItWorksSection'; @@ -71,7 +71,9 @@ export default async function HomePage({ searchParams }: HomePageProps) { // Parse search params const page = parseInt(params.page || '1'); - const radius = params.radius ? parseFloat(params.radius) : undefined; + // Without this the API applies its own 5-mile default while the Distance + // control shows DEFAULT_RADIUS_MILES. + const radius = params.radius ? parseFloat(params.radius) : DEFAULT_RADIUS_MILES; // Check if user has performed a search const hasSearchParams = !!( diff --git a/nextjs-app/components/FilterBar.tsx b/nextjs-app/components/FilterBar.tsx index 50119e0..ebab6e9 100644 --- a/nextjs-app/components/FilterBar.tsx +++ b/nextjs-app/components/FilterBar.tsx @@ -3,7 +3,7 @@ import { useState, useCallback, useTransition, useRef, useEffect } from "react"; import type { ReactNode } from "react"; import { useRouter, useSearchParams, usePathname } from "next/navigation"; -import { isValidPostcode, schoolUrl } from "@/lib/utils"; +import { DEFAULT_RADIUS_MILES, isValidPostcode, schoolUrl } from "@/lib/utils"; import { track } from "@/lib/analytics"; import { useSchoolSuggest } from "@/hooks/useSchoolSuggest"; import { SuggestList, suggestOptionId } from "./SuggestList"; @@ -70,7 +70,7 @@ export function FilterBar({ const currentSearch = searchParams.get("search") || ""; const currentPostcode = searchParams.get("postcode") || ""; - const currentRadius = searchParams.get("radius") || "1"; + const currentRadius = searchParams.get("radius") || String(DEFAULT_RADIUS_MILES); const initialOmniValue = currentPostcode || currentSearch; const [omniValue, setOmniValue] = useState(initialOmniValue); @@ -269,7 +269,7 @@ export function FilterBar({ if (isPostcode) { updateURL({ postcode: cleaned.toUpperCase(), - radius: currentRadius || "1", + radius: currentRadius, search: "", }); } else { diff --git a/nextjs-app/components/HomeView.tsx b/nextjs-app/components/HomeView.tsx index d291ee0..f14c019 100644 --- a/nextjs-app/components/HomeView.tsx +++ b/nextjs-app/components/HomeView.tsx @@ -17,6 +17,7 @@ import { useComparisonContext } from '@/context/ComparisonContext'; import { fetchSchools, fetchLAaverages, fetchNationalAverages } from '@/lib/api'; import type { SchoolsResponse, Filters, School } from '@/lib/types'; import { track } from '@/lib/analytics'; +import { DEFAULT_RADIUS_MILES } from '@/lib/utils'; import styles from './HomeView.module.css'; interface HomeViewProps { @@ -340,6 +341,7 @@ export function HomeView({ initialSchools, filters, totalSchools, howItWorks, ed setIsLoadingMap(true); const params: Record = {}; searchParams.forEach((value, key) => { params[key] = value; }); + params.radius ??= DEFAULT_RADIUS_MILES; params.page = 1; params.page_size = 500; fetchSchools(params, { cache: 'no-store', signal: controller.signal }) @@ -419,7 +421,7 @@ export function HomeView({ initialSchools, filters, totalSchools, howItWorks, ed setGeoState('idle'); track('near_me_used', { outcome: 'granted' }); track('search_submitted', { query: postcode, via: 'near_me', has_postcode: true, filters_active: '', filters_count: 0 }); - router.push(`/?postcode=${encodeURIComponent(postcode)}&radius=1`); + router.push(`/?postcode=${encodeURIComponent(postcode)}&radius=${DEFAULT_RADIUS_MILES}`); } else { track('near_me_used', { outcome: 'no_postcode' }); setGeoState('error'); diff --git a/nextjs-app/lib/utils.ts b/nextjs-app/lib/utils.ts index b528e7e..3422edb 100644 --- a/nextjs-app/lib/utils.ts +++ b/nextjs-app/lib/utils.ts @@ -235,6 +235,9 @@ export function getVariabilityLabel(stdDev: number | null): string { // Validation // ============================================================================ +/** Radius a postcode search uses until the user picks another. */ +export const DEFAULT_RADIUS_MILES = 0.5; + /** * Validate UK postcode format */