locale-utils.spec.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { enUS } from 'date-fns/locale/en-US';
  2. import { fr } from 'date-fns/locale/fr';
  3. import { ja } from 'date-fns/locale/ja';
  4. import { ko } from 'date-fns/locale/ko';
  5. import { zhCN } from 'date-fns/locale/zh-CN';
  6. import { describe, expect, it } from 'vitest';
  7. import { getLocale } from './locale-utils';
  8. describe('getLocale', () => {
  9. it.each([
  10. // Base codes
  11. ['en', enUS],
  12. ['ja', ja],
  13. ['fr', fr],
  14. ['ko', ko],
  15. ['zh', zhCN],
  16. // Hyphenated variants
  17. ['en-US', enUS],
  18. ['ja-JP', ja],
  19. ['fr-FR', fr],
  20. ['ko-KR', ko],
  21. ['zh-CN', zhCN],
  22. // Underscore variants
  23. ['en_US', enUS],
  24. ['ja_JP', ja],
  25. ['fr_FR', fr],
  26. ['ko_KR', ko],
  27. ['zh_CN', zhCN],
  28. ])('should return the correct locale for "%s"', (langCode, expected) => {
  29. expect(getLocale(langCode)).toBe(expected);
  30. });
  31. it('should fall back to base code when hyphenated variant is unknown', () => {
  32. expect(getLocale('en-GB')).toBe(enUS);
  33. });
  34. it('should default to enUS for unknown locale', () => {
  35. expect(getLocale('unknown')).toBe(enUS);
  36. });
  37. it('should default to enUS for empty string', () => {
  38. expect(getLocale('')).toBe(enUS);
  39. });
  40. });