dayjs.test.ts 3.1 KB

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