dayjs.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import simpleDayjs from "../../src/utils/dayjs";
  2. describe("SimpleDayjs", () => {
  3. it("should return true for a valid date", () => {
  4. let date = simpleDayjs();
  5. expect(date.isValid()).toBe(true);
  6. date = simpleDayjs("2024-02-10");
  7. expect(date.isValid()).toBe(true);
  8. });
  9. it("should return false for an invalid date", () => {
  10. const date = simpleDayjs("invalid-date");
  11. expect(date.isValid()).toBe(false);
  12. });
  13. it("should calculate the difference", () => {
  14. const date1 = simpleDayjs("2024-02-10");
  15. const date2 = simpleDayjs("2000-01-01");
  16. expect(date1.diff(date2, "year")).toBe(24);
  17. expect(date1.diff(date2, "month")).toBe(289);
  18. expect(date1.diff(date2, "day")).toBe(8806);
  19. expect(date1.diff(date2)).toBe(8806);
  20. });
  21. it("should format the start of the year", () => {
  22. const date = simpleDayjs("2024-02-10");
  23. expect(date.startOf("year").format("YYYY-MM-DD")).toBe("2024-01-01");
  24. });
  25. it("should format the end of the ...", () => {
  26. const date = simpleDayjs("2024-02-10");
  27. expect(date.endOf("year").format("YYYY-MM-DD HH:mm:ss")).toBe("2024-12-31 23:59:59");
  28. expect(date.endOf("month").format("YYYY-MM-DD HH:mm:ss")).toBe("2024-02-29 23:59:59");
  29. expect(date.endOf("day").format("YYYY-MM-DD HH:mm:ss")).toBe("2024-02-10 23:59:59");
  30. });
  31. it("should add one month to the date", () => {
  32. const date = simpleDayjs("2024-02-10");
  33. expect(date.add(1, "month").format("YYYY-MM-DD")).toBe("2024-03-10");
  34. });
  35. it("should get and set the year", () => {
  36. const date = simpleDayjs("2024-02-10");
  37. expect(date.year()).toBe(2024);
  38. expect(date.year(2025).format("YYYY-MM-DD")).toBe("2025-02-10");
  39. });
  40. it("should get and set the month", () => {
  41. const date = simpleDayjs("2024-02-10");
  42. expect(date.month()).toBe(1); // 注意月份是从0开始的
  43. expect(date.month(5).format("YYYY-MM-DD")).toBe("2024-06-10");
  44. });
  45. it("should get and set the date", () => {
  46. const date = simpleDayjs("2024-02-10");
  47. expect(date.date()).toBe(10);
  48. expect(date.date(15).format("YYYY-MM-DD")).toBe("2024-02-15");
  49. });
  50. it("should return the day of the week", () => {
  51. const date = simpleDayjs("2024-02-10");
  52. expect(date.day()).toBe(6); // 6 表示星期六
  53. });
  54. it("should set the day of the week and return the new date", () => {
  55. const date = simpleDayjs("2024-02-10");
  56. expect(date.day(1).format("YYYY-MM-DD")).toBe("2024-02-05"); // 将日期调整到下一个星期一
  57. });
  58. it("should format the date with day of the week", () => {
  59. const date = simpleDayjs("2024-02-10");
  60. expect(date.format("dddd, YYYY-MM-DD")).toBe("Saturday, 2024-02-10");
  61. });
  62. it("should check if a date is before another date", () => {
  63. const date1 = simpleDayjs("2024-02-10");
  64. const date2 = simpleDayjs("2023-01-01");
  65. expect(date1.isBefore(date2)).toBe(false);
  66. });
  67. it("should check if a date is after another date", () => {
  68. const date1 = simpleDayjs("2024-02-10");
  69. const date2 = simpleDayjs("2023-01-01");
  70. expect(date1.isAfter(date2)).toBe(true);
  71. });
  72. it("should check if two dates are the same (day)", () => {
  73. const date1 = simpleDayjs("2024-02-10");
  74. const date2 = simpleDayjs("2024-02-10");
  75. expect(date1.isSame(date2, "day")).toBe(true);
  76. });
  77. it("should check if two dates are the same (month)", () => {
  78. const date1 = simpleDayjs("2024-02-10");
  79. const date2 = simpleDayjs("2024-02-01");
  80. expect(date1.isSame(date2, "month")).toBe(true);
  81. });
  82. it("should check if two dates are the same (year)", () => {
  83. const date1 = simpleDayjs("2024-02-10");
  84. const date2 = simpleDayjs("2024-01-01");
  85. expect(date1.isSame(date2, "year")).toBe(true);
  86. });
  87. });