Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Oct 17, 2024
1 parent a29f33f commit ded01b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
9 changes: 8 additions & 1 deletion packages/templates/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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<PluginInfo>;
expect(() => setLocale(trial)).toThrow(LocaleNotFoundError);
});
19 changes: 8 additions & 11 deletions packages/templates/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ const resources = () => {
* @param trial - Trial data including user supplied parameters.
*/
export const setLocale = (trial: TrialType<PluginInfo>) => {
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);
}
};

Expand Down

0 comments on commit ded01b7

Please sign in to comment.