Intl.getCanonicalLocales.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. });
  25. test("improperly placed separator", () => {
  26. expect(() => {
  27. Intl.getCanonicalLocales("en-");
  28. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  29. expect(() => {
  30. Intl.getCanonicalLocales("-en");
  31. }).toThrowWithMessage(RangeError, "-en is not a structurally valid language tag");
  32. expect(() => {
  33. Intl.getCanonicalLocales("en--US");
  34. }).toThrowWithMessage(RangeError, "en--US is not a structurally valid language tag");
  35. });
  36. test("non string or object locale", () => {
  37. expect(() => {
  38. Intl.getCanonicalLocales([true]);
  39. }).toThrowWithMessage(TypeError, "true is neither an object nor a string");
  40. });
  41. test("duplicate extension components", () => {
  42. expect(() => {
  43. Intl.getCanonicalLocales("en-u-aa-U-aa");
  44. }).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
  45. expect(() => {
  46. Intl.getCanonicalLocales("en-t-aa-T-aa");
  47. }).toThrowWithMessage(RangeError, "en-t-aa-T-aa is not a structurally valid language tag");
  48. expect(() => {
  49. Intl.getCanonicalLocales("en-z-aa-Z-aa");
  50. }).toThrowWithMessage(RangeError, "en-z-aa-Z-aa is not a structurally valid language tag");
  51. });
  52. test("duplicate transformed extension variant subtags", () => {
  53. expect(() => {
  54. Intl.getCanonicalLocales("en-t-en-POSIX-POSIX");
  55. }).toThrowWithMessage(
  56. RangeError,
  57. "en-t-en-POSIX-POSIX is not a structurally valid language tag"
  58. );
  59. });
  60. });
  61. describe("normal behavior", () => {
  62. test("length is 1", () => {
  63. expect(Intl.getCanonicalLocales).toHaveLength(1);
  64. });
  65. test("valid locales", () => {
  66. expect(Intl.getCanonicalLocales([])).toEqual([]);
  67. expect(Intl.getCanonicalLocales("EN-US")).toEqual(["en-US"]);
  68. expect(Intl.getCanonicalLocales(["EN-US"])).toEqual(["en-US"]);
  69. expect(Intl.getCanonicalLocales(["EN-US", "Fr"])).toEqual(["en-US", "fr"]);
  70. expect(Intl.getCanonicalLocales("EN-lATN-US")).toEqual(["en-Latn-US"]);
  71. expect(Intl.getCanonicalLocales("EN-US-POSIX")).toEqual(["en-US-posix"]);
  72. expect(Intl.getCanonicalLocales("EN-LATN-US-POSIX")).toEqual(["en-Latn-US-posix"]);
  73. });
  74. test("duplicate locales", () => {
  75. expect(Intl.getCanonicalLocales(["EN-US", "en-US", "en-us"])).toEqual(["en-US"]);
  76. });
  77. test("non-array object", () => {
  78. expect(Intl.getCanonicalLocales({})).toEqual([]);
  79. expect(Intl.getCanonicalLocales({ en: 123 })).toEqual([]);
  80. expect(Intl.getCanonicalLocales(undefined)).toEqual([]);
  81. expect(Intl.getCanonicalLocales(true)).toEqual([]);
  82. expect(Intl.getCanonicalLocales(123)).toEqual([]);
  83. });
  84. });