PronounsPage/new/backend/test/locales.test.ts
tecc 990aabf6d8
feat(rewrite): Locale class toJSON + lastLoaded property, etc.
locale-lastLoaded: `lastLoaded` allows code that uses locales to cache certain values until the locale is reloaded. Useful for hot-reloading locales.
locale-toJSON: The `Locale` class now has a generic `toJSON` implementation should that be necessary, provided by the new `toJSONGeneric` utility.
locale-logo: Locale-specific logos are now stored twice: once as the actual loaded data, and again as a URL. The `locale.logo` property resolves to the URL versions, whereas `locale.logoSource` resolves to the source data.

refreshLocaleDescriptionCaches: `refreshLocaleDescriptionCaches` allows reloading all derived values from the actual loaded data separately from loading everything again. Mainly used in test code but useful nonetheless.
getActiveLocaleDescriptions: The `activeLocaleDescriptions` array is now computed alongside the other cache values in `refreshLocaleDescriptionCaches`.
2024-04-17 04:57:07 +02:00

80 lines
2.7 KiB
TypeScript

import { prepareConfig } from "./util.ts";
import { getConfig } from "#self/config";
import {
getActiveLocaleDescriptions, getExpectedTranslations, getLocale,
getLocaleDescriptions,
loadAllLocales,
loadLocaleDescriptions,
Locale,
LocaleDescription,
refreshLocaleDescriptionCaches,
} from "#self/locales";
beforeAll(() => loadLocaleDescriptions());
describe("locale descriptions", () => {
it("loadLocaleDescriptions", async () => {
await loadLocaleDescriptions(true);
});
const compareDescriptions = (a: LocaleDescription, b: LocaleDescription) => a.code.localeCompare(b.code)
it("active descriptions (allowUnpublished = true)", () => {
getConfig().locale.allowUnpublished = true;
refreshLocaleDescriptionCaches();
const available = getLocaleDescriptions();
const correct = available.filter((v) => !v.pseudo).sort(compareDescriptions);
expect(getActiveLocaleDescriptions().sort(compareDescriptions)).toEqual(correct);
});
it("active descriptions (allowUnpublished = false)", () => {
getConfig().locale.allowUnpublished = false;
refreshLocaleDescriptionCaches();
const available = getLocaleDescriptions();
const correct = available.filter((v) => v.published).sort(compareDescriptions);
expect(getActiveLocaleDescriptions().sort(compareDescriptions)).toEqual(correct);
})
})
describe("locales", () => {
beforeAll(() => {
getConfig().locale.allowUnpublished = false;
refreshLocaleDescriptionCaches();
return loadAllLocales();
})
it("loadAllLocales", () => {
return loadAllLocales();
})
function allLocales(f: (locale: Locale) => void) {
const locales = getLocaleDescriptions();
for (const desc of locales) {
const locale = getLocale(desc.code);
if (locale != null) {
f(locale)
}
}
}
it("expected translations", () => {
const expectedKeys = getExpectedTranslations();
allLocales((locale) => {
for (const key of expectedKeys) {
const value = locale.getTranslation(key);
expect(value).toBeDefined();
expect(typeof value).toEqual("string");
}
});
});
it("toJSON", () => {
allLocales((locale) => {
const json = locale.toJSON();
expect(json["code"]).toEqual(locale.code);
expect(json["fallbackLocale"]).toEqual(locale.fallbackLocale);
expect(json["config"]).toEqual(locale.config);
expect(json["logo"]).toEqual(locale.logo);
expect(json["logoSource"]).toEqual(locale.logoSource);
});
})
})