Duration.from.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const expectDurationOneToTen = duration => {
  2. expect(duration.years).toBe(1);
  3. expect(duration.months).toBe(2);
  4. expect(duration.weeks).toBe(3);
  5. expect(duration.days).toBe(4);
  6. expect(duration.hours).toBe(5);
  7. expect(duration.minutes).toBe(6);
  8. expect(duration.seconds).toBe(7);
  9. expect(duration.milliseconds).toBe(8);
  10. expect(duration.microseconds).toBe(9);
  11. expect(duration.nanoseconds).toBe(10);
  12. };
  13. describe("correct behavior", () => {
  14. test("length is 1", () => {
  15. expect(Temporal.Duration.from).toHaveLength(1);
  16. });
  17. test("Duration instance argument", () => {
  18. const duration = Temporal.Duration.from(
  19. new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  20. );
  21. expectDurationOneToTen(duration);
  22. });
  23. test("Duration-like object argument", () => {
  24. const duration = Temporal.Duration.from({
  25. years: 1,
  26. months: 2,
  27. weeks: 3,
  28. days: 4,
  29. hours: 5,
  30. minutes: 6,
  31. seconds: 7,
  32. milliseconds: 8,
  33. microseconds: 9,
  34. nanoseconds: 10,
  35. });
  36. expectDurationOneToTen(duration);
  37. });
  38. // Un-skip once ParseTemporalDurationString is implemented
  39. test.skip("Duration string argument", () => {
  40. const duration = Temporal.Duration.from("TODO");
  41. expectDurationOneToTen(duration);
  42. });
  43. });
  44. describe("errors", () => {
  45. test("Invalid duration-like object", () => {
  46. expect(() => {
  47. Temporal.Duration.from({});
  48. }).toThrowWithMessage(TypeError, "Invalid duration-like object");
  49. });
  50. test("Invalid duration property value", () => {
  51. expect(() => {
  52. Temporal.Duration.from({ years: 1.23 });
  53. }).toThrowWithMessage(
  54. RangeError,
  55. "Invalid value for duration property 'years': must be an integer, got 1.2" // ...29999999999999 - let's not include that in the test :^)
  56. );
  57. expect(() => {
  58. Temporal.Duration.from({ years: "foo" });
  59. }).toThrowWithMessage(
  60. RangeError,
  61. "Invalid value for duration property 'years': must be an integer, got NaN"
  62. );
  63. });
  64. });