RelativeTimeFormat.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. describe("errors", () => {
  2. test("called without new", () => {
  3. expect(() => {
  4. Intl.RelativeTimeFormat();
  5. }).toThrowWithMessage(
  6. TypeError,
  7. "Intl.RelativeTimeFormat constructor must be called with 'new'"
  8. );
  9. });
  10. test("structurally invalid tag", () => {
  11. expect(() => {
  12. new Intl.RelativeTimeFormat("root");
  13. }).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
  14. expect(() => {
  15. new Intl.RelativeTimeFormat("en-");
  16. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  17. expect(() => {
  18. new Intl.RelativeTimeFormat("Latn");
  19. }).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
  20. expect(() => {
  21. new Intl.RelativeTimeFormat("en-u-aa-U-aa");
  22. }).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
  23. });
  24. test("options is an invalid type", () => {
  25. expect(() => {
  26. new Intl.RelativeTimeFormat("en", null);
  27. }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
  28. });
  29. test("localeMatcher option is invalid", () => {
  30. expect(() => {
  31. new Intl.RelativeTimeFormat("en", { localeMatcher: "hello!" });
  32. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
  33. });
  34. test("numberingSystem option is invalid", () => {
  35. expect(() => {
  36. new Intl.RelativeTimeFormat("en", { numberingSystem: "hello!" });
  37. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option numberingSystem");
  38. });
  39. test("style option is invalid", () => {
  40. expect(() => {
  41. new Intl.RelativeTimeFormat("en", { style: "hello!" });
  42. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option style");
  43. });
  44. test("numeric option is invalid", () => {
  45. expect(() => {
  46. new Intl.RelativeTimeFormat("en", { numeric: "hello!" });
  47. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option numeric");
  48. });
  49. });
  50. describe("normal behavior", () => {
  51. test("length is 0", () => {
  52. expect(Intl.RelativeTimeFormat).toHaveLength(0);
  53. });
  54. test("all valid localeMatcher options", () => {
  55. ["lookup", "best fit"].forEach(localeMatcher => {
  56. expect(() => {
  57. new Intl.RelativeTimeFormat("en", { localeMatcher: localeMatcher });
  58. }).not.toThrow();
  59. });
  60. });
  61. test("valid numberingSystem options", () => {
  62. ["latn", "arab", "abc-def-ghi"].forEach(numberingSystem => {
  63. expect(() => {
  64. new Intl.RelativeTimeFormat("en", { numberingSystem: numberingSystem });
  65. }).not.toThrow();
  66. });
  67. });
  68. test("all valid style options", () => {
  69. ["long", "short", "narrow"].forEach(style => {
  70. expect(() => {
  71. new Intl.RelativeTimeFormat("en", { style: style });
  72. }).not.toThrow();
  73. });
  74. });
  75. test("all valid numeric options", () => {
  76. ["always", "auto"].forEach(numeric => {
  77. expect(() => {
  78. new Intl.RelativeTimeFormat("en", { numeric: numeric });
  79. }).not.toThrow();
  80. });
  81. });
  82. });