From fbed86e249361b565cee08db12ad22ed1ebe509b Mon Sep 17 00:00:00 2001 From: Christopher Byrd Date: Wed, 23 Oct 2024 14:30:00 -0700 Subject: [PATCH] nit #11567 --- .../arches/utils/generate-arches-url.test.ts | 43 +++++++++++++++++++ .../src/arches/utils/generate-arches-url.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 arches/app/src/arches/utils/generate-arches-url.test.ts diff --git a/arches/app/src/arches/utils/generate-arches-url.test.ts b/arches/app/src/arches/utils/generate-arches-url.test.ts new file mode 100644 index 0000000000..af4a55059d --- /dev/null +++ b/arches/app/src/arches/utils/generate-arches-url.test.ts @@ -0,0 +1,43 @@ +import { describe, it, expect } from "vitest"; +import generateArchesURL from "@/arches/utils/generate-arches-url.ts"; + +// @ts-expect-error ARCHES_URLS is defined globally +global.ARCHES_URLS = { + example_url: "/admin/{language_code}/example/{id}", + another_url: "/admin/another/{id}", +}; + +describe("generateArchesURL", () => { + it("should return a valid URL with language code and parameters", () => { + const result = generateArchesURL("example_url", "fr", { id: "123" }); + expect(result).toBe("/admin/fr/example/123"); + }); + + it("should use the lang attribute when no language code is provided", () => { + Object.defineProperty(document.documentElement, "lang", { + value: "de", + configurable: true, + }); + + const result = generateArchesURL("example_url", null, { id: "123" }); + expect(result).toBe("/admin/de/example/123"); + }); + + it("should throw an error if the URL name is not found", () => { + expect(() => + generateArchesURL("invalid_url", "fr", { id: "123" }), + ).toThrowError("Key 'invalid_url' not found in JSON object"); + }); + + it("should replace URL parameters correctly", () => { + const result = generateArchesURL("another_url", null, { id: "456" }); + expect(result).toBe("/admin/another/456"); + }); + + it("should handle missing language code and parameter placeholders", () => { + const result = generateArchesURL("another_url", "es", { id: "789" }); + + console.log(result); + expect(result).toBe("/admin/another/789"); + }); +}); diff --git a/arches/app/src/arches/utils/generate-arches-url.ts b/arches/app/src/arches/utils/generate-arches-url.ts index 1c5098d897..a95bd18aed 100644 --- a/arches/app/src/arches/utils/generate-arches-url.ts +++ b/arches/app/src/arches/utils/generate-arches-url.ts @@ -19,7 +19,7 @@ export default function ( url = url.replace("{language_code}", languageCode); } - Object.entries(urlParams).forEach((key, value) => { + Object.entries(urlParams).forEach(([key, value]) => { url = url.replace(new RegExp(`{${key}}`, "g"), value); });