/** * The module that ends the stranding: before it, a school page's only anchor * pointed at the school's own website, so ~27k pages sent authority off-site * and none of it reached the location layer. */ import { render, screen } from '@testing-library/react'; import { NearbyPlaces } from '@/components/school/NearbyPlaces'; const essex = { kind: 'authority', slug: 'essex', name: 'Essex', count: 480, url: '/schools/authority/essex', phases: [] }; const brentwood = { kind: 'town', slug: 'brentwood', name: 'Brentwood', count: 37, url: '/schools/brentwood', phases: [] }; const cm15 = { kind: 'outcode', slug: 'cm15', name: 'CM15', count: 12, url: '/schools/near/cm15', phases: [] }; describe('NearbyPlaces', () => { it('links to every place the school belongs to', () => { render(); expect(screen.getByRole('link', { name: /Brentwood/ })) .toHaveAttribute('href', '/schools/brentwood'); expect(screen.getByRole('link', { name: /Essex/ })) .toHaveAttribute('href', '/schools/authority/essex'); expect(screen.getByRole('link', { name: /CM15/ })) .toHaveAttribute('href', '/schools/near/cm15'); }); it('says how many schools each link leads to', () => { // An anchor that states its destination's size is worth more to a reader // and to a crawler than "see more". render(); expect(screen.getByRole('link', { name: /37 schools in Brentwood/ })) .toBeInTheDocument(); }); it('renders nothing at all when the school has no published places', () => { // Not an empty heading. A school whose town and authority both fall below // the threshold has nowhere to point, and the page should look as it did // before the module existed. const { container } = render(); expect(container).toBeEmptyDOMElement(); }); it('puts the narrowest place first, which is the most useful link', () => { // The API orders widest-first for the breadcrumb; a reader on a school // page wants its town before its county. render(); const hrefs = screen.getAllByRole('link').map((a) => a.getAttribute('href')); expect(hrefs.indexOf('/schools/brentwood')) .toBeLessThan(hrefs.indexOf('/schools/authority/essex')); }); it('handles a singular count without saying "1 schools"', () => { render(); expect(screen.getByRole('link', { name: /1 school in Brentwood/ })) .toBeInTheDocument(); }); it('links the phase page the school appears on', () => { // "primary schools in brentwood" is the query these pages exist for. render(); expect(screen.getByRole('link', { name: /22 primary schools in Brentwood/ })) .toHaveAttribute('href', '/schools/brentwood/primary'); }); it('links both phase pages for an all-through school', () => { render(); expect(screen.getByRole('link', { name: /22 primary schools/ })).toBeInTheDocument(); expect(screen.getByRole('link', { name: /9 secondary schools/ })).toBeInTheDocument(); }); it('keeps a phase link next to the place it belongs to', () => { // Grouping matters: "22 primary schools in Brentwood" directly after // "37 schools in Brentwood" reads as one place, not two unrelated links. render(); const hrefs = screen.getAllByRole('link').map((a) => a.getAttribute('href')); expect(hrefs.indexOf('/schools/brentwood/primary')) .toBe(hrefs.indexOf('/schools/brentwood') + 1); }); });