Skip to content

Commit d79f9c6

Browse files
committed
feat(tests): add missing tests for utiliy functions under locales.ts in spec module
1 parent 385e3ca commit d79f9c6

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

packages/spec/src/locales.spec.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from "vitest";
2-
import { normalizeLocale } from "./locales";
2+
import { getLocaleCodeDelimiter, normalizeLocale, resolveLocaleCode, resolveOverridenLocale } from "./locales";
33

44
describe("normalizeLocale", () => {
55
it("should return normalized locale for short locale codes", () => {
@@ -24,3 +24,62 @@ describe("normalizeLocale", () => {
2424
expect(normalizeLocale("zh-rCN")).toEqual("zh-CN");
2525
});
2626
});
27+
28+
describe("resolveLocaleCode", () => {
29+
it("should resolve a short locale code to the first full locale code in the map", () => {
30+
expect(resolveLocaleCode("en")).toEqual("en-US");
31+
expect(resolveLocaleCode("fr")).toEqual("fr-FR");
32+
expect(resolveLocaleCode("az")).toEqual("az-AZ");
33+
});
34+
35+
it("should return the full locale code if it is already provided", () => {
36+
expect(resolveLocaleCode("en-US")).toEqual("en-US");
37+
expect(resolveLocaleCode("fr-CA")).toEqual("fr-CA");
38+
expect(resolveLocaleCode("es-MX")).toEqual("es-MX");
39+
});
40+
41+
it("should throw an error for an invalid or unsupported locale code", () => {
42+
expect(() => resolveLocaleCode("az-US")).toThrow("Invalid locale code");
43+
expect(() => resolveLocaleCode("au")).toThrow("Invalid locale code");
44+
});
45+
46+
it("should return first code for locales with multiple variants", () => {
47+
expect(resolveLocaleCode("sr")).toEqual("sr-RS");
48+
expect(resolveLocaleCode("zh")).toEqual("zh-CN");
49+
});
50+
});
51+
52+
53+
describe("getLocaleCodeDelimiter", () => {
54+
it("should return '-' for locale codes with hyphen delimiter", () => {
55+
expect(getLocaleCodeDelimiter("en-US")).toEqual("-");
56+
expect(getLocaleCodeDelimiter("fr-FR")).toEqual("-");
57+
});
58+
59+
it("should return '_' for locale codes with underscore delimiter", () => {
60+
expect(getLocaleCodeDelimiter("en_US")).toEqual("_");
61+
expect(getLocaleCodeDelimiter("fr_FR")).toEqual("_");
62+
});
63+
64+
it("should return undefined for locale codes without a recognized delimiter", () => {
65+
expect(getLocaleCodeDelimiter("enUS")).toBeNull();
66+
expect(getLocaleCodeDelimiter("frFR")).toBeNull();
67+
});
68+
});
69+
70+
describe("resolveOverridenLocale", () => {
71+
it("should return the same locale if no delimiter is provided", () => {
72+
expect(resolveOverridenLocale("en-US")).toEqual("en-US");
73+
expect(resolveOverridenLocale("fr_FR")).toEqual("fr_FR");
74+
});
75+
76+
it("should replace the delimiter with the specified one", () => {
77+
expect(resolveOverridenLocale("en-US", "_")).toEqual("en_US");
78+
expect(resolveOverridenLocale("fr_FR", "-")).toEqual("fr-FR");
79+
});
80+
81+
it("should return the same locale if no recognized delimiter is found", () => {
82+
expect(resolveOverridenLocale("enUS", "_")).toEqual("enUS");
83+
expect(resolveOverridenLocale("frFR", "-")).toEqual("frFR");
84+
});
85+
});

packages/spec/src/locales.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
221221
message: "Invalid locale code",
222222
});
223223

224-
export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
224+
export const resolveLocaleCode = (value: string): LocaleCodeFull => {
225225
const existingFullLocaleCode = Object.values(localeMap)
226226
.flat()
227227
.includes(value as any);

0 commit comments

Comments
 (0)