Now.plainDate.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.Now.plainDate).toHaveLength(1);
  4. });
  5. test("basic functionality", () => {
  6. const calendar = new Temporal.Calendar("iso8601");
  7. const plainDate = Temporal.Now.plainDate(calendar);
  8. expect(plainDate).toBeInstanceOf(Temporal.PlainDate);
  9. expect(plainDate.calendar).toBe(calendar);
  10. });
  11. test("custom time zone", () => {
  12. const calendar = new Temporal.Calendar("iso8601");
  13. const timeZone = {
  14. getOffsetNanosecondsFor() {
  15. return 86399999999999;
  16. },
  17. };
  18. const plainDate = Temporal.Now.plainDate(calendar, "UTC");
  19. const plainDateWithOffset = Temporal.Now.plainDate(calendar, timeZone);
  20. if (plainDate.dayOfYear === plainDate.daysInYear) {
  21. expect(plainDateWithOffset.year).toBe(plainDate.year + 1);
  22. expect(plainDateWithOffset.month).toBe(1);
  23. expect(plainDateWithOffset.day).toBe(1);
  24. } else {
  25. expect(plainDateWithOffset.year).toBe(plainDate.year);
  26. if (plainDate.day === plainDate.daysInMonth) {
  27. expect(plainDateWithOffset.month).toBe(plainDate.month + 1);
  28. expect(plainDateWithOffset.day).toBe(1);
  29. } else {
  30. expect(plainDateWithOffset.month).toBe(plainDate.month);
  31. expect(plainDateWithOffset.day).toBe(plainDate.day + 1);
  32. }
  33. }
  34. });
  35. test("cannot have a time zone with more than a day", () => {
  36. [86400000000000, -86400000000000, 86400000000001, 86400000000002].forEach(offset => {
  37. const calendar = new Temporal.Calendar("iso8601");
  38. const timeZone = {
  39. getOffsetNanosecondsFor() {
  40. return offset;
  41. },
  42. };
  43. expect(() => Temporal.Now.plainDate(calendar, timeZone)).toThrowWithMessage(
  44. RangeError,
  45. "Invalid offset nanoseconds value, must be in range -86400 * 10^9 + 1 to 86400 * 10^9 - 1"
  46. );
  47. });
  48. });
  49. });
  50. describe("errors", () => {
  51. test("custom time zone doesn't have a getOffsetNanosecondsFor function", () => {
  52. expect(() => {
  53. Temporal.Now.plainDate({}, {});
  54. }).toThrowWithMessage(TypeError, "getOffsetNanosecondsFor is undefined");
  55. });
  56. });