2026-09-14 20:45:50 +01:00
|
|
|
/**
|
|
|
|
|
* 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';
|
|
|
|
|
|
2026-09-14 20:54:59 +01:00
|
|
|
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: [] };
|
2026-09-14 20:45:50 +01:00
|
|
|
|
|
|
|
|
describe('NearbyPlaces', () => {
|
|
|
|
|
it('links to every place the school belongs to', () => {
|
|
|
|
|
render(<NearbyPlaces places={[essex, brentwood, cm15]} />);
|
|
|
|
|
|
|
|
|
|
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(<NearbyPlaces places={[brentwood]} />);
|
|
|
|
|
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(<NearbyPlaces places={[]} />);
|
|
|
|
|
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(<NearbyPlaces places={[essex, brentwood, cm15]} />);
|
|
|
|
|
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(<NearbyPlaces places={[{ ...brentwood, count: 1 }]} />);
|
|
|
|
|
expect(screen.getByRole('link', { name: /1 school in Brentwood/ }))
|
|
|
|
|
.toBeInTheDocument();
|
|
|
|
|
});
|
2026-09-14 20:54:59 +01:00
|
|
|
|
|
|
|
|
it('links the phase page the school appears on', () => {
|
|
|
|
|
// "primary schools in brentwood" is the query these pages exist for.
|
|
|
|
|
render(<NearbyPlaces places={[{
|
|
|
|
|
...brentwood,
|
|
|
|
|
phases: [{ phase: 'primary', count: 22, url: '/schools/brentwood/primary' }],
|
|
|
|
|
}]} />);
|
|
|
|
|
|
|
|
|
|
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(<NearbyPlaces places={[{
|
|
|
|
|
...brentwood,
|
|
|
|
|
phases: [
|
|
|
|
|
{ phase: 'primary', count: 22, url: '/schools/brentwood/primary' },
|
|
|
|
|
{ phase: 'secondary', count: 9, url: '/schools/brentwood/secondary' },
|
|
|
|
|
],
|
|
|
|
|
}]} />);
|
|
|
|
|
|
|
|
|
|
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(<NearbyPlaces places={[essex, {
|
|
|
|
|
...brentwood,
|
|
|
|
|
phases: [{ phase: 'primary', count: 22, url: '/schools/brentwood/primary' }],
|
|
|
|
|
}]} />);
|
|
|
|
|
|
|
|
|
|
const hrefs = screen.getAllByRole('link').map((a) => a.getAttribute('href'));
|
|
|
|
|
expect(hrefs.indexOf('/schools/brentwood/primary'))
|
|
|
|
|
.toBe(hrefs.indexOf('/schools/brentwood') + 1);
|
|
|
|
|
});
|
2026-09-14 20:45:50 +01:00
|
|
|
});
|