Instant.fromEpochSeconds.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.Instant.fromEpochSeconds).toHaveLength(1);
  4. });
  5. test("basic functionality", () => {
  6. expect(Temporal.Instant.fromEpochSeconds(0).epochSeconds).toBe(0);
  7. expect(Temporal.Instant.fromEpochSeconds(1).epochSeconds).toBe(1);
  8. expect(Temporal.Instant.fromEpochSeconds(999_999_999).epochSeconds).toBe(999_999_999);
  9. expect(Temporal.Instant.fromEpochSeconds(8_640_000_000_000).epochSeconds).toBe(
  10. 8_640_000_000_000
  11. );
  12. expect(Temporal.Instant.fromEpochSeconds(-0).epochSeconds).toBe(0);
  13. expect(Temporal.Instant.fromEpochSeconds(-1).epochSeconds).toBe(-1);
  14. expect(Temporal.Instant.fromEpochSeconds(-999_999_999).epochSeconds).toBe(-999_999_999);
  15. expect(Temporal.Instant.fromEpochSeconds(-8_640_000_000_000).epochSeconds).toBe(
  16. -8_640_000_000_000
  17. );
  18. });
  19. });
  20. describe("errors", () => {
  21. test("argument must be coercible to BigInt", () => {
  22. expect(() => {
  23. Temporal.Instant.fromEpochSeconds(1.23);
  24. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  25. // NOTE: ToNumber is called on the argument first, so this is effectively NaN.
  26. expect(() => {
  27. Temporal.Instant.fromEpochSeconds("foo");
  28. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  29. });
  30. test("out-of-range epoch seconds value", () => {
  31. expect(() => {
  32. Temporal.Instant.fromEpochSeconds(8_640_000_000_001);
  33. }).toThrowWithMessage(
  34. RangeError,
  35. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  36. );
  37. expect(() => {
  38. Temporal.Instant.fromEpochSeconds(-8_640_000_000_001);
  39. }).toThrowWithMessage(
  40. RangeError,
  41. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  42. );
  43. });
  44. });