diff --git a/e2e/tests/journeys.spec.ts b/e2e/tests/journeys.spec.ts index 5bc91d6..614a21c 100644 --- a/e2e/tests/journeys.spec.ts +++ b/e2e/tests/journeys.spec.ts @@ -417,6 +417,9 @@ test('a secondary search row compares its Attainment 8 with the LA average', asy // guards the comparison itself; the unit test pins the cache mode. const la = await (await page.request.get('/api/la-averages')).json(); const averages: Record = la.secondary?.attainment_8_by_la ?? {}; + // DfE publishes about 152 LA averages. An empty map (a missing mart, or a + // year the LA data set has not reached) hides every comparison: fail, not skip. + expect(Object.keys(averages).length).toBeGreaterThan(100); const res = await page.request.get('/api/schools?search=school&phase=secondary&page_size=50'); expect(res.ok()).toBeTruthy(); const school = ((await res.json()).schools ?? []).find( @@ -435,23 +438,35 @@ test('a secondary search row compares its Attainment 8 with the LA average', asy test('an independent secondary shows its Attainment 8 without an LA comparison', async ({ page }) => { // DfE's LA averages cover state-funded schools, and an independent school's - // Attainment 8 leaves out IGCSEs, so a gap would mislead (audit H2). - const res = await page.request.get('/api/schools?school_type=independent&phase=secondary&page_size=50'); - expect(res.ok()).toBeTruthy(); - const school = ((await res.json()).schools ?? []).find( - (s: { attainment_8_score?: number | null; school_type?: string }) => - s.attainment_8_score != null && !/special/i.test(s.school_type ?? '')); - test.skip(!school, 'no independent secondary with an Attainment 8 here'); + // Attainment 8 leaves out IGCSEs, so a gap would mislead (audit H2). A state + // school on the same page must show its gap first, so the absence is real. + const la = await (await page.request.get('/api/la-averages')).json(); + const averages: Record = la.secondary?.attainment_8_by_la ?? {}; + expect(Object.keys(averages).length).toBeGreaterThan(100); - const averagesLoaded = page.waitForResponse(r => r.url().includes('/la-averages')); - await searchByName(page, school.school_name); - await averagesLoaded; - const link = page.locator(`a[href^="/school/${school.urn}-"]`).first(); - await expect(link).toBeVisible({ timeout: 15_000 }); - const stats = link.locator('xpath=ancestor::div[contains(@class, "__rowContent")][1]') + type Row = { urn: number; school_type?: string; local_authority?: string; attainment_8_score?: number | null }; + const compared = (s: Row) => s.attainment_8_score != null && s.local_authority != null + && averages[s.local_authority] != null + && !/special|pupil referral|alternative provision/i.test(s.school_type ?? ''); + let found: { la: string; state: Row; independent: Row } | null = null; + for (const name of ['Kensington and Chelsea', 'Westminster', 'Camden', 'Hammersmith and Fulham', 'Barnet']) { + // The search page asks for the same first 50 schools. + const res = await page.request.get(`/api/schools?search=${encodeURIComponent(name)}&phase=secondary&page_size=50`); + const schools: Row[] = ((await res.json()).schools ?? []).filter(compared); + const independent = schools.find(s => /independent/i.test(s.school_type ?? '')); + const state = schools.find(s => !/independent/i.test(s.school_type ?? '')); + if (independent && state) { found = { la: name, state, independent }; break; } + } + test.skip(!found, 'no LA here lists a state and an independent secondary on one page'); + + await page.goto(`/?search=${encodeURIComponent(found!.la)}&phase=secondary`); + const stats = (urn: number) => page.locator(`a[href^="/school/${urn}-"]`).first() + .locator('xpath=ancestor::div[contains(@class, "__rowContent")][1]') .locator('[class*="__line3"]'); - await expect(stats.getByText(school.attainment_8_score.toFixed(1))).toBeVisible(); - await expect(stats.getByText(/vs LA avg/)).toHaveCount(0); + await expect(stats(found!.state.urn).getByText(/vs LA avg/)).toBeVisible({ timeout: 15_000 }); + const independent = stats(found!.independent.urn); + await expect(independent.getByText(found!.independent.attainment_8_score!.toFixed(1))).toBeVisible(); + await expect(independent.getByText(/vs LA avg/)).toHaveCount(0); }); test('a secondary shows its 2023/24 GCSE results, the last year DfE published Progress 8', async ({ page }) => {