-
Notifications
You must be signed in to change notification settings - Fork 837
test: add unit tests for locale utility functions under the spec module #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
385e3ca
d79f9c6
8e1f8f4
8d24342
8414318
38fb17c
d33eef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@lingo.dev/_spec": patch | ||
| --- | ||
|
|
||
| Add unit test for utility function in locales.ts | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { normalizeLocale } from "./locales"; | ||
| import { getLocaleCodeDelimiter, normalizeLocale, resolveLocaleCode, resolveOverridenLocale } from "./locales"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khalatevarun the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved, renamed the function |
||
|
|
||
| describe("normalizeLocale", () => { | ||
| it("should return normalized locale for short locale codes", () => { | ||
|
|
@@ -24,3 +24,61 @@ describe("normalizeLocale", () => { | |
| expect(normalizeLocale("zh-rCN")).toEqual("zh-CN"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveLocaleCode", () => { | ||
| it("should resolve a short locale code to the first full locale code in the map", () => { | ||
| expect(resolveLocaleCode("en")).toEqual("en-US"); | ||
| expect(resolveLocaleCode("fr")).toEqual("fr-FR"); | ||
| expect(resolveLocaleCode("az")).toEqual("az-AZ"); | ||
| }); | ||
|
|
||
| it("should return the full locale code if it is already provided", () => { | ||
| expect(resolveLocaleCode("en-US")).toEqual("en-US"); | ||
| expect(resolveLocaleCode("fr-CA")).toEqual("fr-CA"); | ||
| expect(resolveLocaleCode("es-MX")).toEqual("es-MX"); | ||
| }); | ||
|
|
||
| it("should throw an error for an invalid or unsupported locale code", () => { | ||
| expect(() => resolveLocaleCode("az-US")).toThrow("Invalid locale code"); | ||
| expect(() => resolveLocaleCode("au")).toThrow("Invalid locale code"); | ||
| }); | ||
|
|
||
| it("should return first code for locales with multiple variants", () => { | ||
| expect(resolveLocaleCode("sr")).toEqual("sr-RS"); | ||
| expect(resolveLocaleCode("zh")).toEqual("zh-CN"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getLocaleCodeDelimiter", () => { | ||
| it("should return '-' for locale codes with hyphen delimiter", () => { | ||
| expect(getLocaleCodeDelimiter("en-US")).toEqual("-"); | ||
| expect(getLocaleCodeDelimiter("fr-FR")).toEqual("-"); | ||
| }); | ||
|
|
||
| it("should return '_' for locale codes with underscore delimiter", () => { | ||
| expect(getLocaleCodeDelimiter("en_US")).toEqual("_"); | ||
| expect(getLocaleCodeDelimiter("fr_FR")).toEqual("_"); | ||
| }); | ||
|
|
||
| it("should return undefined for locale codes without a recognized delimiter", () => { | ||
| expect(getLocaleCodeDelimiter("enUS")).toBeNull(); | ||
| expect(getLocaleCodeDelimiter("frFR")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveOverridenLocale", () => { | ||
| it("should return the same locale if no delimiter is provided", () => { | ||
| expect(resolveOverridenLocale("en-US")).toEqual("en-US"); | ||
| expect(resolveOverridenLocale("fr_FR")).toEqual("fr_FR"); | ||
| }); | ||
|
|
||
| it("should replace the delimiter with the specified one", () => { | ||
| expect(resolveOverridenLocale("en-US", "_")).toEqual("en_US"); | ||
| expect(resolveOverridenLocale("fr_FR", "-")).toEqual("fr-FR"); | ||
| }); | ||
|
|
||
| it("should return the same locale if no recognized delimiter is found", () => { | ||
| expect(resolveOverridenLocale("enUS", "_")).toEqual("enUS"); | ||
| expect(resolveOverridenLocale("frFR", "-")).toEqual("frFR"); | ||
| }); | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done @mathio |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,7 +221,7 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include | |
| message: "Invalid locale code", | ||
| }); | ||
|
|
||
| export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => { | ||
| export const resolveLocaleCode = (value: string): LocaleCodeFull => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the function already handles case for an invalid code on line
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! 🚀 |
||
| const existingFullLocaleCode = Object.values(localeMap) | ||
| .flat() | ||
| .includes(value as any); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to patch