From 2583db058203de999c1829b9bec2cf98da8199f3 Mon Sep 17 00:00:00 2001 From: Utsav Luintel Date: Wed, 21 Jan 2026 15:53:47 +0545 Subject: [PATCH 1/5] test: add unit test for country-picker functions --- .../src/utils/__test__/country-picker.test.ts | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 packages/ui/src/utils/__test__/country-picker.test.ts diff --git a/packages/ui/src/utils/__test__/country-picker.test.ts b/packages/ui/src/utils/__test__/country-picker.test.ts new file mode 100644 index 000000000..0aec2e208 --- /dev/null +++ b/packages/ui/src/utils/__test__/country-picker.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, test } from "vitest"; + +import defaultEnglishTranslation from "../../FormWidgets/CountryPicker/en.json"; +import { getFallbackTranslation, getFlagClass } from "../country-picker"; + +const locales = { + fr: { key: "Bonjour" }, + es: { key: "Hola" }, +}; + +describe("getFallbackTranslation", () => { + test("Should return translation from locales if fallbackLocale exists in it", () => { + const result = getFallbackTranslation("fr", locales); + expect(result).toEqual({ key: "Bonjour" }); + }); + + test("Should return defaultEnglishTranslation if fallbackLocale is 'en' and not in locales", () => { + const result = getFallbackTranslation("en", {}); + expect(result).toEqual(defaultEnglishTranslation); + }); + + test("Should prioritize locales['en'] over defaultEnglishTranslation if provided", () => { + const customEn = { key: "Hello Custom" }; + const localesWithEn = { en: customEn }; + const result = getFallbackTranslation("en", localesWithEn); + expect(result).toEqual(customEn); + }); + + test("Should return null if fallbackLocale is not found and is not 'en'", () => { + const result = getFallbackTranslation("de", locales); + expect(result).toBeNull(); + }); + + test("Should handle undefined locales gracefully", () => { + const result = getFallbackTranslation("fr", undefined); + expect(result).toBeNull(); + }); + + test("Should return defaultEnglishTranslation when locales is undefined but fallback is 'en'", () => { + const result = getFallbackTranslation("en", undefined); + expect(result).toEqual(defaultEnglishTranslation); + }); +}); + +describe("getFlagClass", () => { + test("Should generate basic class with code only", () => { + const result = getFlagClass("US", "left", "normal"); + expect(result).toBe("flag-icon flag-icon-us"); + }); + + test("Should handle lowercase and trimming of country code", () => { + const result = getFlagClass(" GB ", "left", "normal"); + expect(result).toBe("flag-icon flag-icon-gb"); + }); + + test("Should add 'flag-icon-right' when position is 'right'", () => { + const result = getFlagClass("fr", "right", "normal"); + expect(result).toBe("flag-icon flag-icon-fr flag-icon-right"); + }); + + test("Should add 'flag-icon-right-edge' when position is 'right-edge'", () => { + const result = getFlagClass("fr", "right-edge", "normal"); + expect(result).toBe("flag-icon flag-icon-fr flag-icon-right-edge"); + }); + + test("Should add 'flag-icon-rounded' when style is 'circle'", () => { + const result = getFlagClass("jp", "left", "circle"); + expect(result).toBe("flag-icon flag-icon-jp flag-icon-rounded"); + }); + + test("Should add 'flag-icon-squared' when style is 'square'", () => { + const result = getFlagClass("jp", "left", "square"); + expect(result).toBe("flag-icon flag-icon-jp flag-icon-squared"); + }); + + test("Should combine multiple classes correctly", () => { + const result = getFlagClass("ca", "right", "circle"); + expect(result).toBe( + "flag-icon flag-icon-ca flag-icon-right flag-icon-rounded", + ); + }); + + test("Should handle undefined code gracefully", () => { + const result = getFlagClass(undefined, "left", "normal"); + expect(result).toBe("flag-icon"); + }); +}); From 695d0cb0c78aaf6c0f836ec6837b272f2d0b8ba3 Mon Sep 17 00:00:00 2001 From: Utsav Luintel Date: Wed, 21 Jan 2026 16:36:56 +0545 Subject: [PATCH 2/5] test: modify locales --- .../src/utils/__test__/country-picker.test.ts | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/ui/src/utils/__test__/country-picker.test.ts b/packages/ui/src/utils/__test__/country-picker.test.ts index 0aec2e208..3d18c45b8 100644 --- a/packages/ui/src/utils/__test__/country-picker.test.ts +++ b/packages/ui/src/utils/__test__/country-picker.test.ts @@ -3,15 +3,43 @@ import { describe, expect, test } from "vitest"; import defaultEnglishTranslation from "../../FormWidgets/CountryPicker/en.json"; import { getFallbackTranslation, getFlagClass } from "../country-picker"; +const frenchTranslation = { + DE: "Allemagne", + BR: "Brésil", + CA: "Canada", + CN: "Chine", + ES: "Espagne", + US: "États‑Unis", + FR: "France", + IT: "Italie", + JP: "Japon", + GB: "Royaume‑Uni", + RU: "Russie", +}; + +const spanishTranslation = { + DE: "Alemania", + BR: "Brasil", + CA: "Canadá", + CN: "China", + ES: "España", + US: "Estados Unidos", + FR: "Francia", + IT: "Italia", + JP: "Japón", + GB: "Reino Unido", + RU: "Rusia", +}; + const locales = { - fr: { key: "Bonjour" }, - es: { key: "Hola" }, + fr: frenchTranslation, + es: spanishTranslation, }; describe("getFallbackTranslation", () => { test("Should return translation from locales if fallbackLocale exists in it", () => { const result = getFallbackTranslation("fr", locales); - expect(result).toEqual({ key: "Bonjour" }); + expect(result).toEqual(frenchTranslation); }); test("Should return defaultEnglishTranslation if fallbackLocale is 'en' and not in locales", () => { @@ -20,7 +48,7 @@ describe("getFallbackTranslation", () => { }); test("Should prioritize locales['en'] over defaultEnglishTranslation if provided", () => { - const customEn = { key: "Hello Custom" }; + const customEn = { FR: "Hello Custom" }; const localesWithEn = { en: customEn }; const result = getFallbackTranslation("en", localesWithEn); expect(result).toEqual(customEn); From bbd82a610e7d6a40dad73f5a9a75fd27f1eb6a25 Mon Sep 17 00:00:00 2001 From: Utsav Luintel Date: Wed, 21 Jan 2026 16:58:33 +0545 Subject: [PATCH 3/5] test: add empty line --- .../src/utils/__test__/country-picker.test.ts | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/ui/src/utils/__test__/country-picker.test.ts b/packages/ui/src/utils/__test__/country-picker.test.ts index 3d18c45b8..922581e5c 100644 --- a/packages/ui/src/utils/__test__/country-picker.test.ts +++ b/packages/ui/src/utils/__test__/country-picker.test.ts @@ -39,33 +39,39 @@ const locales = { describe("getFallbackTranslation", () => { test("Should return translation from locales if fallbackLocale exists in it", () => { const result = getFallbackTranslation("fr", locales); + expect(result).toEqual(frenchTranslation); }); test("Should return defaultEnglishTranslation if fallbackLocale is 'en' and not in locales", () => { const result = getFallbackTranslation("en", {}); + expect(result).toEqual(defaultEnglishTranslation); }); - test("Should prioritize locales['en'] over defaultEnglishTranslation if provided", () => { - const customEn = { FR: "Hello Custom" }; - const localesWithEn = { en: customEn }; - const result = getFallbackTranslation("en", localesWithEn); - expect(result).toEqual(customEn); + test("Should prioritize locales['np'] over defaultEnglishTranslation if provided", () => { + const nepaliTranslation = { NP: "नेपाल" }; + const locales = { np: nepaliTranslation }; + const result = getFallbackTranslation("np", locales); + + expect(result).toEqual(nepaliTranslation); }); test("Should return null if fallbackLocale is not found and is not 'en'", () => { const result = getFallbackTranslation("de", locales); + expect(result).toBeNull(); }); test("Should handle undefined locales gracefully", () => { const result = getFallbackTranslation("fr", undefined); + expect(result).toBeNull(); }); test("Should return defaultEnglishTranslation when locales is undefined but fallback is 'en'", () => { const result = getFallbackTranslation("en", undefined); + expect(result).toEqual(defaultEnglishTranslation); }); }); @@ -73,36 +79,43 @@ describe("getFallbackTranslation", () => { describe("getFlagClass", () => { test("Should generate basic class with code only", () => { const result = getFlagClass("US", "left", "normal"); + expect(result).toBe("flag-icon flag-icon-us"); }); test("Should handle lowercase and trimming of country code", () => { const result = getFlagClass(" GB ", "left", "normal"); + expect(result).toBe("flag-icon flag-icon-gb"); }); test("Should add 'flag-icon-right' when position is 'right'", () => { const result = getFlagClass("fr", "right", "normal"); + expect(result).toBe("flag-icon flag-icon-fr flag-icon-right"); }); test("Should add 'flag-icon-right-edge' when position is 'right-edge'", () => { const result = getFlagClass("fr", "right-edge", "normal"); + expect(result).toBe("flag-icon flag-icon-fr flag-icon-right-edge"); }); test("Should add 'flag-icon-rounded' when style is 'circle'", () => { const result = getFlagClass("jp", "left", "circle"); + expect(result).toBe("flag-icon flag-icon-jp flag-icon-rounded"); }); test("Should add 'flag-icon-squared' when style is 'square'", () => { const result = getFlagClass("jp", "left", "square"); + expect(result).toBe("flag-icon flag-icon-jp flag-icon-squared"); }); test("Should combine multiple classes correctly", () => { const result = getFlagClass("ca", "right", "circle"); + expect(result).toBe( "flag-icon flag-icon-ca flag-icon-right flag-icon-rounded", ); @@ -110,6 +123,7 @@ describe("getFlagClass", () => { test("Should handle undefined code gracefully", () => { const result = getFlagClass(undefined, "left", "normal"); + expect(result).toBe("flag-icon"); }); }); From c585af66c8cf9554675c0df8f30b1e8cc9696ffd Mon Sep 17 00:00:00 2001 From: Utsav Luintel Date: Wed, 21 Jan 2026 17:12:02 +0545 Subject: [PATCH 4/5] fix: locale naming --- packages/ui/src/utils/__test__/country-picker.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/ui/src/utils/__test__/country-picker.test.ts b/packages/ui/src/utils/__test__/country-picker.test.ts index 922581e5c..f84ee23d1 100644 --- a/packages/ui/src/utils/__test__/country-picker.test.ts +++ b/packages/ui/src/utils/__test__/country-picker.test.ts @@ -49,12 +49,12 @@ describe("getFallbackTranslation", () => { expect(result).toEqual(defaultEnglishTranslation); }); - test("Should prioritize locales['np'] over defaultEnglishTranslation if provided", () => { - const nepaliTranslation = { NP: "नेपाल" }; - const locales = { np: nepaliTranslation }; - const result = getFallbackTranslation("np", locales); + test("Should prioritize locales['en'] (custom englishTranslation) over defaultEnglishTranslation if provided", () => { + const englishTranslation = { FR: "France" }; + const locales = { en: englishTranslation }; + const result = getFallbackTranslation("en", locales); - expect(result).toEqual(nepaliTranslation); + expect(result).toEqual(englishTranslation); }); test("Should return null if fallbackLocale is not found and is not 'en'", () => { From 4baf93c9bdd0abfb60899a2f7375e6e202230534 Mon Sep 17 00:00:00 2001 From: Utsav Luintel Date: Wed, 21 Jan 2026 17:13:43 +0545 Subject: [PATCH 5/5] fix: test message --- packages/ui/src/utils/__test__/country-picker.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/src/utils/__test__/country-picker.test.ts b/packages/ui/src/utils/__test__/country-picker.test.ts index f84ee23d1..ff5952eaf 100644 --- a/packages/ui/src/utils/__test__/country-picker.test.ts +++ b/packages/ui/src/utils/__test__/country-picker.test.ts @@ -49,7 +49,7 @@ describe("getFallbackTranslation", () => { expect(result).toEqual(defaultEnglishTranslation); }); - test("Should prioritize locales['en'] (custom englishTranslation) over defaultEnglishTranslation if provided", () => { + test("Should prioritize locales['en'] over defaultEnglishTranslation if provided", () => { const englishTranslation = { FR: "France" }; const locales = { en: englishTranslation }; const result = getFallbackTranslation("en", locales);