DurationFormat.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. describe("errors", () => {
  2. test("called without new", () => {
  3. expect(() => {
  4. Intl.DurationFormat();
  5. }).toThrowWithMessage(
  6. TypeError,
  7. "Intl.DurationFormat constructor must be called with 'new'"
  8. );
  9. });
  10. test("structurally invalid tag", () => {
  11. expect(() => {
  12. new Intl.DurationFormat("root");
  13. }).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
  14. expect(() => {
  15. new Intl.DurationFormat("en-");
  16. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  17. expect(() => {
  18. new Intl.DurationFormat("Latn");
  19. }).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
  20. expect(() => {
  21. new Intl.DurationFormat("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.DurationFormat("en", null);
  27. }).toThrowWithMessage(TypeError, "Options is not an object");
  28. });
  29. test("localeMatcher option is invalid", () => {
  30. expect(() => {
  31. new Intl.DurationFormat("en", { localeMatcher: "hello!" });
  32. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
  33. });
  34. test("style option is invalid", () => {
  35. expect(() => {
  36. new Intl.DurationFormat("en", { style: "hello!" });
  37. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option style");
  38. });
  39. test("years option is invalid", () => {
  40. expect(() => {
  41. new Intl.DurationFormat("en", { years: "hello!" });
  42. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option years");
  43. });
  44. test("yearsDisplay option is invalid", () => {
  45. expect(() => {
  46. new Intl.DurationFormat("en", { yearsDisplay: "hello!" });
  47. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option yearsDisplay");
  48. });
  49. test("months option is invalid", () => {
  50. expect(() => {
  51. new Intl.DurationFormat("en", { months: "hello!" });
  52. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option months");
  53. });
  54. test("monthsDisplay option is invalid", () => {
  55. expect(() => {
  56. new Intl.DurationFormat("en", { monthsDisplay: "hello!" });
  57. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option monthsDisplay");
  58. });
  59. test("weeks option is invalid", () => {
  60. expect(() => {
  61. new Intl.DurationFormat("en", { weeks: "hello!" });
  62. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option weeks");
  63. });
  64. test("weeksDisplay option is invalid", () => {
  65. expect(() => {
  66. new Intl.DurationFormat("en", { weeksDisplay: "hello!" });
  67. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option weeksDisplay");
  68. });
  69. test("days option is invalid", () => {
  70. expect(() => {
  71. new Intl.DurationFormat("en", { days: "hello!" });
  72. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option days");
  73. });
  74. test("daysDisplay option is invalid", () => {
  75. expect(() => {
  76. new Intl.DurationFormat("en", { daysDisplay: "hello!" });
  77. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option daysDisplay");
  78. });
  79. test("hours option is invalid", () => {
  80. expect(() => {
  81. new Intl.DurationFormat("en", { hours: "hello!" });
  82. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option hours");
  83. });
  84. test("hoursDisplay option is invalid", () => {
  85. expect(() => {
  86. new Intl.DurationFormat("en", { hoursDisplay: "hello!" });
  87. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option hoursDisplay");
  88. });
  89. test("minutes option is invalid", () => {
  90. expect(() => {
  91. new Intl.DurationFormat("en", { minutes: "hello!" });
  92. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option minutes");
  93. });
  94. test("minutesDisplay option is invalid", () => {
  95. expect(() => {
  96. new Intl.DurationFormat("en", { minutesDisplay: "hello!" });
  97. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option minutesDisplay");
  98. });
  99. test("seconds option is invalid", () => {
  100. expect(() => {
  101. new Intl.DurationFormat("en", { seconds: "hello!" });
  102. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option seconds");
  103. });
  104. test("secondsDisplay option is invalid", () => {
  105. expect(() => {
  106. new Intl.DurationFormat("en", { secondsDisplay: "hello!" });
  107. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option secondsDisplay");
  108. });
  109. test("milliseconds option is invalid", () => {
  110. expect(() => {
  111. new Intl.DurationFormat("en", { milliseconds: "hello!" });
  112. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option milliseconds");
  113. });
  114. test("millisecondsDisplay option is invalid", () => {
  115. expect(() => {
  116. new Intl.DurationFormat("en", { millisecondsDisplay: "hello!" });
  117. }).toThrowWithMessage(
  118. RangeError,
  119. "hello! is not a valid value for option millisecondsDisplay"
  120. );
  121. });
  122. test("microseconds option is invalid", () => {
  123. expect(() => {
  124. new Intl.DurationFormat("en", { microseconds: "hello!" });
  125. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option microseconds");
  126. });
  127. test("microsecondsDisplay option is invalid", () => {
  128. expect(() => {
  129. new Intl.DurationFormat("en", { microsecondsDisplay: "hello!" });
  130. }).toThrowWithMessage(
  131. RangeError,
  132. "hello! is not a valid value for option microsecondsDisplay"
  133. );
  134. });
  135. test("nanoseconds option is invalid", () => {
  136. expect(() => {
  137. new Intl.DurationFormat("en", { nanoseconds: "hello!" });
  138. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option nanoseconds");
  139. });
  140. test("nanosecondsDisplay option is invalid", () => {
  141. expect(() => {
  142. new Intl.DurationFormat("en", { nanosecondsDisplay: "hello!" });
  143. }).toThrowWithMessage(
  144. RangeError,
  145. "hello! is not a valid value for option nanosecondsDisplay"
  146. );
  147. });
  148. });
  149. describe("normal behavior", () => {
  150. test("length is 0", () => {
  151. expect(Intl.DurationFormat).toHaveLength(0);
  152. });
  153. test("all valid localeMatcher options", () => {
  154. ["lookup", "best fit"].forEach(localeMatcher => {
  155. expect(() => {
  156. new Intl.DurationFormat("en", { localeMatcher: localeMatcher });
  157. }).not.toThrow();
  158. });
  159. });
  160. test("all valid style options", () => {
  161. ["long", "short", "narrow", "digital"].forEach(style => {
  162. expect(() => {
  163. new Intl.DurationFormat("en", { style: style });
  164. }).not.toThrow();
  165. });
  166. });
  167. test("all valid years options", () => {
  168. ["long", "short", "narrow"].forEach(years => {
  169. expect(() => {
  170. new Intl.DurationFormat("en", { years: years });
  171. }).not.toThrow();
  172. });
  173. });
  174. test("all valid yearsDisplay options", () => {
  175. ["always", "auto"].forEach(yearsDisplay => {
  176. expect(() => {
  177. new Intl.DurationFormat("en", { yearsDisplay: yearsDisplay });
  178. }).not.toThrow();
  179. });
  180. });
  181. test("all valid months options", () => {
  182. ["long", "short", "narrow"].forEach(months => {
  183. expect(() => {
  184. new Intl.DurationFormat("en", { months: months });
  185. }).not.toThrow();
  186. });
  187. });
  188. test("all valid monthsDisplay options", () => {
  189. ["always", "auto"].forEach(monthsDisplay => {
  190. expect(() => {
  191. new Intl.DurationFormat("en", { monthsDisplay: monthsDisplay });
  192. }).not.toThrow();
  193. });
  194. });
  195. test("all valid weeks options", () => {
  196. ["long", "short", "narrow"].forEach(weeks => {
  197. expect(() => {
  198. new Intl.DurationFormat("en", { weeks: weeks });
  199. }).not.toThrow();
  200. });
  201. });
  202. test("all valid weeksDisplay options", () => {
  203. ["always", "auto"].forEach(weeksDisplay => {
  204. expect(() => {
  205. new Intl.DurationFormat("en", { weeksDisplay: weeksDisplay });
  206. }).not.toThrow();
  207. });
  208. });
  209. test("all valid days options", () => {
  210. ["long", "short", "narrow"].forEach(days => {
  211. expect(() => {
  212. new Intl.DurationFormat("en", { days: days });
  213. }).not.toThrow();
  214. });
  215. });
  216. test("all valid daysDisplay options", () => {
  217. ["always", "auto"].forEach(daysDisplay => {
  218. expect(() => {
  219. new Intl.DurationFormat("en", { daysDisplay: daysDisplay });
  220. }).not.toThrow();
  221. });
  222. });
  223. test("all valid hours options", () => {
  224. ["long", "short", "narrow"].forEach(hours => {
  225. expect(() => {
  226. new Intl.DurationFormat("en", { hours: hours });
  227. }).not.toThrow();
  228. });
  229. ["numeric", "2-digit"].forEach(seconds => {
  230. expect(() => {
  231. new Intl.DurationFormat("en", { hours: seconds });
  232. }).not.toThrow();
  233. expect(() => {
  234. new Intl.DurationFormat("en", { style: "digital", hours: seconds });
  235. }).not.toThrow();
  236. });
  237. });
  238. test("all valid hoursDisplay options", () => {
  239. ["always", "auto"].forEach(hoursDisplay => {
  240. expect(() => {
  241. new Intl.DurationFormat("en", { hoursDisplay: hoursDisplay });
  242. }).not.toThrow();
  243. });
  244. });
  245. test("all valid minutes options", () => {
  246. ["long", "short", "narrow"].forEach(minutes => {
  247. expect(() => {
  248. new Intl.DurationFormat("en", { minutes: minutes });
  249. }).not.toThrow();
  250. });
  251. ["numeric", "2-digit"].forEach(seconds => {
  252. expect(() => {
  253. new Intl.DurationFormat("en", { minutes: seconds });
  254. }).not.toThrow();
  255. expect(() => {
  256. new Intl.DurationFormat("en", { style: "digital", minutes: seconds });
  257. }).not.toThrow();
  258. });
  259. });
  260. test("all valid minutesDisplay options", () => {
  261. ["always", "auto"].forEach(minutesDisplay => {
  262. expect(() => {
  263. new Intl.DurationFormat("en", { minutesDisplay: minutesDisplay });
  264. }).not.toThrow();
  265. });
  266. });
  267. test("all valid seconds options", () => {
  268. ["long", "short", "narrow"].forEach(seconds => {
  269. expect(() => {
  270. new Intl.DurationFormat("en", { seconds: seconds });
  271. }).not.toThrow();
  272. });
  273. ["numeric", "2-digit"].forEach(seconds => {
  274. expect(() => {
  275. new Intl.DurationFormat("en", { seconds: seconds });
  276. }).not.toThrow();
  277. expect(() => {
  278. new Intl.DurationFormat("en", { style: "digital", seconds: seconds });
  279. }).not.toThrow();
  280. });
  281. });
  282. test("all valid secondsDisplay options", () => {
  283. ["always", "auto"].forEach(secondsDisplay => {
  284. expect(() => {
  285. new Intl.DurationFormat("en", { secondsDisplay: secondsDisplay });
  286. }).not.toThrow();
  287. });
  288. });
  289. test("all valid milliseconds options", () => {
  290. ["long", "short", "narrow"].forEach(milliseconds => {
  291. expect(() => {
  292. new Intl.DurationFormat("en", { milliseconds: milliseconds });
  293. }).not.toThrow();
  294. });
  295. expect(() => {
  296. new Intl.DurationFormat("en", { milliseconds: "numeric" });
  297. }).not.toThrow();
  298. expect(() => {
  299. new Intl.DurationFormat("en", { style: "digital", milliseconds: "numeric" });
  300. }).not.toThrow();
  301. });
  302. test("all valid millisecondsDisplay options", () => {
  303. ["always", "auto"].forEach(millisecondsDisplay => {
  304. expect(() => {
  305. new Intl.DurationFormat("en", { millisecondsDisplay: millisecondsDisplay });
  306. }).not.toThrow();
  307. });
  308. });
  309. test("all valid microseconds options", () => {
  310. ["long", "short", "narrow"].forEach(microseconds => {
  311. expect(() => {
  312. new Intl.DurationFormat("en", { microseconds: microseconds });
  313. }).not.toThrow();
  314. });
  315. expect(() => {
  316. new Intl.DurationFormat("en", { microseconds: "numeric" });
  317. }).not.toThrow();
  318. expect(() => {
  319. new Intl.DurationFormat("en", { style: "digital", microseconds: "numeric" });
  320. }).not.toThrow();
  321. });
  322. test("all valid microsecondsDisplay options", () => {
  323. ["always", "auto"].forEach(microsecondsDisplay => {
  324. expect(() => {
  325. new Intl.DurationFormat("en", { microsecondsDisplay: microsecondsDisplay });
  326. }).not.toThrow();
  327. });
  328. });
  329. test("all valid nanoseconds options", () => {
  330. ["long", "short", "narrow", "numeric"].forEach(nanoseconds => {
  331. expect(() => {
  332. new Intl.DurationFormat("en", { nanoseconds: nanoseconds });
  333. }).not.toThrow();
  334. });
  335. expect(() => {
  336. new Intl.DurationFormat("en", { nanoseconds: "numeric" });
  337. }).not.toThrow();
  338. expect(() => {
  339. new Intl.DurationFormat("en", { style: "digital", nanoseconds: "numeric" });
  340. }).not.toThrow();
  341. });
  342. test("all valid nanosecondsDisplay options", () => {
  343. ["always", "auto"].forEach(nanosecondsDisplay => {
  344. expect(() => {
  345. new Intl.DurationFormat("en", { nanosecondsDisplay: nanosecondsDisplay });
  346. }).not.toThrow();
  347. });
  348. });
  349. test("all valid fractionalDigits options", () => {
  350. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(fractionalDigits => {
  351. expect(() => {
  352. new Intl.DurationFormat("en", { fractionalDigits: fractionalDigits });
  353. }).not.toThrow();
  354. });
  355. });
  356. });