Intl.getCanonicalLocales.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. describe("errors", () => {
  2. test("invalid tag", () => {
  3. expect(() => {
  4. Intl.getCanonicalLocales("EN_US");
  5. }).toThrowWithMessage(RangeError, "EN_US is not a structurally valid language tag");
  6. });
  7. test("root tag", () => {
  8. expect(() => {
  9. Intl.getCanonicalLocales("root");
  10. }).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
  11. });
  12. test("no language tag", () => {
  13. expect(() => {
  14. Intl.getCanonicalLocales("Latn");
  15. }).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
  16. });
  17. test("duplicate variant subtags", () => {
  18. expect(() => {
  19. Intl.getCanonicalLocales("en-posix-POSIX");
  20. }).toThrowWithMessage(
  21. RangeError,
  22. "en-posix-POSIX is not a structurally valid language tag"
  23. );
  24. expect(() => {
  25. Intl.getCanonicalLocales("en-POSIX-POSIX");
  26. }).toThrowWithMessage(
  27. RangeError,
  28. "en-POSIX-POSIX is not a structurally valid language tag"
  29. );
  30. });
  31. test("improperly placed separator", () => {
  32. expect(() => {
  33. Intl.getCanonicalLocales("en-");
  34. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  35. expect(() => {
  36. Intl.getCanonicalLocales("-en");
  37. }).toThrowWithMessage(RangeError, "-en is not a structurally valid language tag");
  38. expect(() => {
  39. Intl.getCanonicalLocales("en--US");
  40. }).toThrowWithMessage(RangeError, "en--US is not a structurally valid language tag");
  41. });
  42. test("non string or object locale", () => {
  43. expect(() => {
  44. Intl.getCanonicalLocales([true]);
  45. }).toThrowWithMessage(TypeError, "true is neither an object nor a string");
  46. });
  47. test("duplicate extension components", () => {
  48. expect(() => {
  49. Intl.getCanonicalLocales("en-u-aa-U-aa");
  50. }).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
  51. expect(() => {
  52. Intl.getCanonicalLocales("en-t-aa-T-aa");
  53. }).toThrowWithMessage(RangeError, "en-t-aa-T-aa is not a structurally valid language tag");
  54. expect(() => {
  55. Intl.getCanonicalLocales("en-z-aa-Z-aa");
  56. }).toThrowWithMessage(RangeError, "en-z-aa-Z-aa is not a structurally valid language tag");
  57. });
  58. test("duplicate transformed extension variant subtags", () => {
  59. expect(() => {
  60. Intl.getCanonicalLocales("en-t-en-POSIX-POSIX");
  61. }).toThrowWithMessage(
  62. RangeError,
  63. "en-t-en-POSIX-POSIX is not a structurally valid language tag"
  64. );
  65. });
  66. });
  67. describe("normal behavior", () => {
  68. test("length is 1", () => {
  69. expect(Intl.getCanonicalLocales).toHaveLength(1);
  70. });
  71. test("valid locales", () => {
  72. expect(Intl.getCanonicalLocales([])).toEqual([]);
  73. expect(Intl.getCanonicalLocales("EN-US")).toEqual(["en-US"]);
  74. expect(Intl.getCanonicalLocales(["EN-US"])).toEqual(["en-US"]);
  75. expect(Intl.getCanonicalLocales(["EN-US", "Fr"])).toEqual(["en-US", "fr"]);
  76. expect(Intl.getCanonicalLocales("EN-lATN-US")).toEqual(["en-Latn-US"]);
  77. expect(Intl.getCanonicalLocales("EN-US-POSIX")).toEqual(["en-US-posix"]);
  78. expect(Intl.getCanonicalLocales("EN-LATN-US-POSIX")).toEqual(["en-Latn-US-posix"]);
  79. });
  80. test("duplicate locales", () => {
  81. expect(Intl.getCanonicalLocales(["EN-US", "en-US", "en-us"])).toEqual(["en-US"]);
  82. });
  83. test("non-array object", () => {
  84. expect(Intl.getCanonicalLocales({})).toEqual([]);
  85. expect(Intl.getCanonicalLocales({ en: 123 })).toEqual([]);
  86. expect(Intl.getCanonicalLocales(undefined)).toEqual([]);
  87. expect(Intl.getCanonicalLocales(true)).toEqual([]);
  88. expect(Intl.getCanonicalLocales(123)).toEqual([]);
  89. });
  90. test("duplicate Unicode locale extension attributes", () => {
  91. expect(Intl.getCanonicalLocales("en-us-u-aaa-aaa")).toEqual(["en-US-u-aaa"]);
  92. expect(Intl.getCanonicalLocales("en-us-u-aaa-bbb-aaa")).toEqual(["en-US-u-aaa-bbb"]);
  93. });
  94. test("duplicate Unicode locale extension keywords", () => {
  95. expect(Intl.getCanonicalLocales("en-us-u-1k-aaa-1k-bbb")).toEqual(["en-US-u-1k-aaa"]);
  96. expect(Intl.getCanonicalLocales("en-us-u-1k-aaa-2k-ccc-1k-bbb")).toEqual([
  97. "en-US-u-1k-aaa-2k-ccc",
  98. ]);
  99. });
  100. test("canonicalize locale objects", () => {
  101. const en = new Intl.Locale("en", { script: "Latn" });
  102. expect(Intl.getCanonicalLocales(en)).toEqual(["en-Latn"]);
  103. const es = new Intl.Locale("es", { region: "419" });
  104. expect(Intl.getCanonicalLocales([en, es])).toEqual(["en-Latn", "es-419"]);
  105. });
  106. });