Skip to content

Commit 743d93e

Browse files
authored
test: add unit tests for locale utility functions under the spec module (#584)
1 parent b71bf63 commit 743d93e

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

.changeset/metal-bottles-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lingo.dev/_spec": patch
3+
---
4+
5+
Add unit test for utility function in locales.ts

packages/spec/src/locales.spec.ts

Lines changed: 59 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, resolveOverriddenLocale } from "./locales";
33

44
describe("normalizeLocale", () => {
55
it("should return normalized locale for short locale codes", () => {
@@ -24,3 +24,61 @@ 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+
describe("getLocaleCodeDelimiter", () => {
53+
it("should return '-' for locale codes with hyphen delimiter", () => {
54+
expect(getLocaleCodeDelimiter("en-US")).toEqual("-");
55+
expect(getLocaleCodeDelimiter("fr-FR")).toEqual("-");
56+
});
57+
58+
it("should return '_' for locale codes with underscore delimiter", () => {
59+
expect(getLocaleCodeDelimiter("en_US")).toEqual("_");
60+
expect(getLocaleCodeDelimiter("fr_FR")).toEqual("_");
61+
});
62+
63+
it("should return undefined for locale codes without a recognized delimiter", () => {
64+
expect(getLocaleCodeDelimiter("enUS")).toBeNull();
65+
expect(getLocaleCodeDelimiter("frFR")).toBeNull();
66+
});
67+
});
68+
69+
describe("resolveOverridenLocale", () => {
70+
it("should return the same locale if no delimiter is provided", () => {
71+
expect(resolveOverriddenLocale("en-US")).toEqual("en-US");
72+
expect(resolveOverriddenLocale("fr_FR")).toEqual("fr_FR");
73+
});
74+
75+
it("should replace the delimiter with the specified one", () => {
76+
expect(resolveOverriddenLocale("en-US", "_")).toEqual("en_US");
77+
expect(resolveOverriddenLocale("fr_FR", "-")).toEqual("fr-FR");
78+
});
79+
80+
it("should return the same locale if no recognized delimiter is found", () => {
81+
expect(resolveOverriddenLocale("enUS", "_")).toEqual("enUS");
82+
expect(resolveOverriddenLocale("frFR", "-")).toEqual("frFR");
83+
});
84+
});

packages/spec/src/locales.ts

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

225-
226225
/**
227226
* Resolves a locale code to its full locale representation.
228227
*
@@ -234,9 +233,7 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
234233
* @return {LocaleCodeFull} The resolved full locale code
235234
* @throws {Error} If the provided locale code is invalid.
236235
*/
237-
238-
239-
export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
236+
export const resolveLocaleCode = (value: string): LocaleCodeFull => {
240237
const existingFullLocaleCode = Object.values(localeMap)
241238
.flat()
242239
.includes(value as any);

0 commit comments

Comments
 (0)