From e617b30f6dfd0c507cf0d9cdce02fc580802c351 Mon Sep 17 00:00:00 2001 From: mantagen Date: Tue, 3 Dec 2024 15:58:24 +0000 Subject: [PATCH] more helpers test --- apps/nextjs/src/lib/analytics/helpers.test.ts | 175 +++++++++++++++++- apps/nextjs/src/lib/analytics/helpers.ts | 8 +- 2 files changed, 179 insertions(+), 4 deletions(-) diff --git a/apps/nextjs/src/lib/analytics/helpers.test.ts b/apps/nextjs/src/lib/analytics/helpers.test.ts index a6e955507..b74d5f2d7 100644 --- a/apps/nextjs/src/lib/analytics/helpers.test.ts +++ b/apps/nextjs/src/lib/analytics/helpers.test.ts @@ -1,5 +1,172 @@ import { parseKeyStage } from "./helpers"; +const testKeyStageMap: Record< + string, + "ks1" | "ks2" | "ks3" | "ks4" | "ks5" | "unknown" | "eyfs" +> = { + "1-2": "ks1", + "10": "ks3", + "12": "ks4", + "16+": "ks5", + "2 and 3": "ks1", + "6": "ks2", + "A Level": "ks5", + "A level": "ks5", + "A-Level": "ks5", + "A-level": "ks5", + ALevel: "ks5", + "Adult Learners": "unknown", + "Adult Workplace Learning": "unknown", + All: "unknown", + "Cross-Stage": "unknown", + DP2: "unknown", + E1: "unknown", + EYFS: "eyfs", + "EYFS SEN": "eyfs", + "Early Years": "eyfs", + "Early Years Foundation Stage": "eyfs", + "Early Years Foundation Stage (EYFS)": "eyfs", + English: "unknown", + "Entry Level 1": "unknown", + "Example invitation added to additional materials for reference.": "unknown", + "Foundation Stage": "eyfs", + "Further Education": "ks5", + GCSE: "ks4", + "GCSE Higher": "ks4", + "GCSE OCR": "ks4", + General: "unknown", + "Higher Education": "unknown", + IGCSE: "ks4", + Introductory: "unknown", + KS1: "ks1", + KS2: "ks2", + "KS2 Upper": "ks2", + "KS2 Year 4": "ks2", + "KS2 Year 5": "ks2", + KS3: "ks3", + "KS3 Geography": "ks3", + "KS3 Geography L1 . Mrs Karaphillides": "ks3", + "KS3 and KS4": "unknown", + "KS3,4": "unknown", + "KS3-4": "unknown", + KS4: "ks4", + "KS4 and KS5": "unknown", + KS5: "ks5", + KS6: "unknown", + "Key Stage": "unknown", + "Key Stage 1": "ks1", + "Key Stage 1 & 2": "ks1", + "Key Stage 1 and Key Stage 2": "ks1", + "Key Stage 1/2": "ks1", + "Key Stage 2": "ks2", + "Key Stage 2 Year 4": "ks2", + "Key Stage 2, Year 3": "ks2", + "Key Stage 2/3": "unknown", + "Key Stage 3": "ks3", + "Key Stage 3 & 4": "unknown", + "Key Stage 3, 4": "unknown", + "Key Stage 3, Year 9": "ks3", + "Key Stage 4": "ks4", + "Key Stage 4 (Year 11, GCSE)": "ks4", + "Key Stage 5": "ks5", + "Key Stage 5 (AS Level)": "ks5", + "Key Stage 5+": "ks5", + "Key Stage Placeholder": "unknown", + LKS2: "ks2", + "Level 1": "unknown", + "Level 2": "unknown", + "Level 3": "unknown", + "Level One": "unknown", + "Lower Key Stage 2": "ks2", + "Lower Key Stage Two": "ks2", + "Lower Primary": "ks1", + "MYP Extended Grade 9": "ks3", + "Middle Years": "ks3", + "Mrs Karaphillides L1": "unknown", + "N/A": "unknown", + "National 5": "unknown", + Nursery: "eyfs", + Placeholder: "unknown", + "Placeholder Key Stage": "unknown", + "Please specify": "unknown", + "Post-16": "ks5", + "Primary School": "ks1", + "Professional Development": "unknown", + "Professional Training": "unknown", + Reception: "eyfs", + "SEN 16-18": "ks5", + "Staff Training": "unknown", + "Teacher Training": "unknown", + "To be decided": "unknown", + "To be defined": "unknown", + Undefined: "unknown", + "Undefined Key Stage": "unknown", + University: "unknown", + "University Level 6": "unknown", + "University Year 1 / Level 4": "unknown", + "Upper Key Stage 2": "ks2", + "Using mental strategies to solve addition and subtraction within 10.": + "unknown", + "Year 1": "ks1", + "Year 1/2": "ks1", + "Year 10": "ks4", + "Year 11": "ks4", + "Year 12": "ks5", + "Year 2": "ks1", + "Year 3": "ks2", + "Year 3 & 4": "ks2", + "Year 4": "ks2", + "Year 5": "ks2", + "Year 5 - Key Stage 2": "ks2", + "Year 6": "ks2", + "Year 7": "ks3", + "Year 8": "ks3", + "Year 9": "ks3", + adult: "unknown", + "all-key-stages": "unknown", + "between key-stage-4 and A-Level": "unknown", + "early-years": "eyfs", + "early-years-foundation-stage": "eyfs", + "early-years-foundation-stage, key-stage-1, key-stage-2": "eyfs", + eyfs: "eyfs", + "functional-skill-level-1": "unknown", + "independent-living": "unknown", + "key stage 3": "ks3", + "key stage 4": "ks4", + "key-stage-1": "ks1", + "key-stage-1 and key-stage-2": "ks1", + "key-stage-1, key-stage-2": "ks1", + "key-stage-2": "ks2", + "key-stage-2 Year 4": "ks2", + "key-stage-2 year 6": "ks2", + "key-stage-2, key-stage-3": "unknown", + "key-stage-3": "ks3", + "key-stage-3, key-stage-4": "unknown", + "key-stage-3|key-stage-4": "unknown", + "key-stage-4": "ks4", + "key-stage-5": "ks5", + "lower key stage 2": "ks2", + "lower-key-stage-2": "ks2", + null: "unknown", + "post-16": "ks5", + "pre-key-stage-2": "unknown", + "rshe-pshe": "unknown", + "sixth form": "ks5", + specialist: "unknown", + tick: "unknown", + undefined: "unknown", + university: "unknown", + "year 1": "ks1", + "year 4": "ks2", + "year-3": "ks2", + "year-4": "ks2", + "year-5": "ks2", + "year-7": "ks3", + "year-8": "ks3", + "year-9": "ks3", + "year-one": "ks1", +}; + describe("Analytics helpers", () => { describe("parseKeyStage", () => { test("Converts 'Key stage {n}' to 'ks{n}'", () => { @@ -9,7 +176,7 @@ describe("Analytics helpers", () => { expect(parseKeyStage("key-stage-2")).toBe("ks2"); }); test("Converts 'KS{n}' to 'ks{n}'", () => { - expect(parseKeyStage("KS1")).toBe("ks1 "); + expect(parseKeyStage("KS1")).toBe("ks1"); }); test("Converts '{n} to 'ks{n}'", () => { expect(parseKeyStage("1")).toBe("ks1"); @@ -17,5 +184,11 @@ describe("Analytics helpers", () => { test("lowercases any text", () => { expect(parseKeyStage("ssWROijfdSKFNLe")).toBe("sswroijfdskfnle"); }); + test.each(Object.entries(testKeyStageMap))( + "Converts '%s' to '%s'", + (input, expected) => { + expect(parseKeyStage(input)).toBe(expected); + }, + ); }); }); diff --git a/apps/nextjs/src/lib/analytics/helpers.ts b/apps/nextjs/src/lib/analytics/helpers.ts index d36a5d97f..097aca79d 100644 --- a/apps/nextjs/src/lib/analytics/helpers.ts +++ b/apps/nextjs/src/lib/analytics/helpers.ts @@ -87,8 +87,10 @@ const keyStageMap: Record = { }; export function parseKeyStage(maybeKeyStage: string): string { - maybeKeyStage = maybeKeyStage.toLowerCase().replace(/[^a-z0-9]/g, ""); - const keyStageSlug = keyStageMap[maybeKeyStage]; + const strippedMaybeKeyStage = maybeKeyStage + .toLowerCase() + .replace(/[^a-z0-9]/g, ""); + const keyStageSlug = keyStageMap[strippedMaybeKeyStage]; - return keyStageSlug ?? maybeKeyStage; + return keyStageSlug ?? "unknown"; }