From 6f0b3d36cec13418cc777349dcc4246f6ffdd3a5 Mon Sep 17 00:00:00 2001 From: Matthieu Bergel Date: Thu, 8 Feb 2024 14:16:57 +0000 Subject: [PATCH] tests: add site navigation tests --- devTools/navigationTest/navigationTest.ts | 41 +++++++++++++++++++++++ devTools/navigationTest/tsconfig.json | 8 +++++ package.json | 1 + tsconfig.json | 3 ++ 4 files changed, 53 insertions(+) create mode 100644 devTools/navigationTest/navigationTest.ts create mode 100644 devTools/navigationTest/tsconfig.json diff --git a/devTools/navigationTest/navigationTest.ts b/devTools/navigationTest/navigationTest.ts new file mode 100644 index 00000000000..705aa45ff00 --- /dev/null +++ b/devTools/navigationTest/navigationTest.ts @@ -0,0 +1,41 @@ +// Test all the slugs in the SiteNavigationStatic object and makes sure +// https://ourworldindata.org/{slug} returns a 200 + +import { SiteNavigationStatic } from "../../site/SiteNavigation.js" + +const testSiteNavigation = async () => { + const slugs = SiteNavigationStatic.categories + .map((category) => { + const categorySlugs = category.entries + .map((entry) => entry.slug) + .concat( + (category.subcategories?.length && + category.subcategories.flatMap((subcategory) => + subcategory.entries.map((entry) => entry.slug) + )) || + [] + ) + return categorySlugs + }) + .flat() + + let promises = slugs.map((slug) => { + return fetch(`https://ourworldindata.org/${slug}`, { + method: "HEAD", + }) + }) + + const responses: Response[] = await Promise.all(promises) + if (responses.some((response) => !response.ok)) { + console.error( + "❌ One or more fetches failed: ", + responses + .filter((response) => !response.ok) + .map((response) => response.url) + ) + return + } + console.log("✅ All fetches completed") +} + +testSiteNavigation() diff --git a/devTools/navigationTest/tsconfig.json b/devTools/navigationTest/tsconfig.json new file mode 100644 index 00000000000..91e0ebd46fd --- /dev/null +++ b/devTools/navigationTest/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfigs/tsconfig.base.json", + "compilerOptions": { + "outDir": "../../itsJustJavascript/devTools/navigationTest", + "rootDir": "." + }, + "references": [{ "path": "../../site" }] +} diff --git a/package.json b/package.json index 4a152d1ae5f..018768255cf 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "testLint": "eslint .", "testPrettierAll": "yarn prettier --check \"**/*.{tsx,ts,jsx,js,json,md,html,css,scss,yml}\"", "testJest": "lerna run buildTests && jest", + "testSiteNavigation": "node --enable-source-maps ./itsJustJavascript/devTools/navigationTest/navigationTest.js", "generateDbTypes": "npx @rmp135/sql-ts -c db/sql-ts/sql-ts-config.json" }, "dependencies": { diff --git a/tsconfig.json b/tsconfig.json index 7e2ab3c01aa..75ff206086b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -51,6 +51,9 @@ }, { "path": "./devTools/regionsUpdater" + }, + { + "path": "./devTools/navigationTest" } ] }