Instant.from.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.Instant.from).toHaveLength(1);
  4. });
  5. test("Instant instance argument", () => {
  6. const instant = new Temporal.Instant(123n);
  7. expect(Temporal.Instant.from(instant).epochNanoseconds).toBe(123n);
  8. });
  9. test("ZonedDateTime instance argument", () => {
  10. const timeZone = new Temporal.TimeZone("UTC");
  11. const zonedDateTime = new Temporal.ZonedDateTime(123n, timeZone);
  12. expect(Temporal.Instant.from(zonedDateTime).epochNanoseconds).toBe(123n);
  13. });
  14. test("Instant string argument", () => {
  15. expect(Temporal.Instant.from("1975-02-02T14:25:36.123456789Z").epochNanoseconds).toBe(
  16. 160583136123456789n
  17. );
  18. // Time zone is not validated
  19. expect(
  20. Temporal.Instant.from("1975-02-02T14:25:36.123456789Z[Custom/TimeZone]")
  21. .epochNanoseconds
  22. ).toBe(160583136123456789n);
  23. });
  24. });
  25. describe("errors", () => {
  26. test("invalid instant string", () => {
  27. expect(() => {
  28. Temporal.Instant.from("foo");
  29. }).toThrowWithMessage(RangeError, "Invalid instant string 'foo'");
  30. });
  31. test("invalid epoch nanoseconds", () => {
  32. // Test cases from https://github.com/tc39/proposal-temporal/commit/baead4d85bc3e9ecab1e9824c3d3fe4fdd77fc3a
  33. expect(() => {
  34. Temporal.Instant.from("-271821-04-20T00:00:00+00:01");
  35. }).toThrowWithMessage(
  36. RangeError,
  37. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  38. );
  39. expect(() => {
  40. Temporal.Instant.from("+275760-09-13T00:00:00-00:01");
  41. }).toThrowWithMessage(
  42. RangeError,
  43. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  44. );
  45. });
  46. });