From ded01b76fc60f582e2f543afe5075f64a9bef05e Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Thu, 17 Oct 2024 14:36:58 -0400 Subject: [PATCH] Update tests --- packages/templates/src/utils.spec.ts | 9 ++++++++- packages/templates/src/utils.ts | 19 ++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/templates/src/utils.spec.ts b/packages/templates/src/utils.spec.ts index 922a399a..9747b3f0 100644 --- a/packages/templates/src/utils.spec.ts +++ b/packages/templates/src/utils.spec.ts @@ -1,4 +1,6 @@ -import { expFormat } from "./utils"; +import { PluginInfo, TrialType } from "jspsych"; +import { LocaleNotFoundError } from "./errors"; +import { expFormat, setLocale } from "./utils"; test("expFormat convert written text to format well in HTML", () => { expect(expFormat("abcdefg")).toStrictEqual("abcdefg"); @@ -18,3 +20,8 @@ test("expFormat convert written text to format well in HTML", () => { "    Tabbed text", ); }); + +test("setLocale throw error with non-existing locale", () => { + const trial = { locale: "non-existing" } as unknown as TrialType; + expect(() => setLocale(trial)).toThrow(LocaleNotFoundError); +}); diff --git a/packages/templates/src/utils.ts b/packages/templates/src/utils.ts index 1626e10b..fb4a6913 100644 --- a/packages/templates/src/utils.ts +++ b/packages/templates/src/utils.ts @@ -69,17 +69,14 @@ const resources = () => { * @param trial - Trial data including user supplied parameters. */ export const setLocale = (trial: TrialType) => { - try { - const lcl = new Intl.Locale(trial.locale); - if (i18next.language !== lcl.baseName) { - i18next.changeLanguage(lcl.baseName); - } - } catch (error) { - if (error instanceof RangeError) { - throw new LocaleNotFoundError(trial.locale); - } else { - throw error; - } + const lcl = new Intl.Locale(trial.locale); + + if (!i18next.hasResourceBundle(lcl.baseName, "translation")) { + throw new LocaleNotFoundError(trial.locale); + } + + if (i18next.language !== lcl.baseName) { + i18next.changeLanguage(lcl.baseName); } };