|
| 1 | +// npx vitest run src/lib/locale.test.ts |
| 2 | + |
| 3 | +import { enUS, frFR } from '@clerk/localizations'; |
| 4 | + |
| 5 | +import { locales, isLocale, Locales, getClerkLocale, Locale } from './locale'; |
| 6 | + |
| 7 | +describe('locale module', () => { |
| 8 | + describe('locales', () => { |
| 9 | + it('should contain supported locales', () => { |
| 10 | + expect(locales).toEqual(['en', 'fr']); |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('isLocale', () => { |
| 15 | + it('should return true for valid locales', () => { |
| 16 | + expect(isLocale('en')).toBe(true); |
| 17 | + expect(isLocale('fr')).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should return false for invalid locales', () => { |
| 21 | + expect(isLocale('es')).toBe(false); |
| 22 | + expect(isLocale('de')).toBe(false); |
| 23 | + expect(isLocale('')).toBe(false); |
| 24 | + expect(isLocale(undefined)).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should properly type-narrow the input', () => { |
| 28 | + const testLocale = (locale: string | undefined): Locale | null => { |
| 29 | + if (isLocale(locale)) { |
| 30 | + const validLocale: Locale = locale; |
| 31 | + return validLocale; |
| 32 | + } |
| 33 | + |
| 34 | + return null; |
| 35 | + }; |
| 36 | + |
| 37 | + expect(testLocale('en')).toBe('en'); |
| 38 | + expect(testLocale('invalid')).toBe(null); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('Locales', () => { |
| 43 | + it('should map locale codes to display names', () => { |
| 44 | + expect(Locales).toEqual({ |
| 45 | + en: 'English', |
| 46 | + fr: 'Français', |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should have an entry for each supported locale', () => { |
| 51 | + locales.forEach((locale) => { |
| 52 | + expect(Locales[locale]).toBeDefined(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('getClerkLocale', () => { |
| 58 | + it('should return enUS for "en" locale', () => { |
| 59 | + expect(getClerkLocale('en')).toBe(enUS); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return frFR for "fr" locale', () => { |
| 63 | + expect(getClerkLocale('fr')).toBe(frFR); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return enUS for invalid locales', () => { |
| 67 | + expect(getClerkLocale('es')).toBe(enUS); |
| 68 | + expect(getClerkLocale('de')).toBe(enUS); |
| 69 | + expect(getClerkLocale('')).toBe(enUS); |
| 70 | + // @ts-expect-error Testing with invalid type |
| 71 | + expect(getClerkLocale(undefined)).toBe(enUS); |
| 72 | + // @ts-expect-error Testing with invalid type |
| 73 | + expect(getClerkLocale(null)).toBe(enUS); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle case sensitivity correctly', () => { |
| 77 | + expect(getClerkLocale('EN')).toBe(enUS); |
| 78 | + expect(getClerkLocale('Fr')).toBe(enUS); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments