index.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import dayjs from "../../src/utils/dayjs";
  2. import { getSolarTermDate, getSolarTerms, type SolarTerm } from "../../src";
  3. import type { SolarTermKey } from "../../src/solar_terms/constants";
  4. describe("Solar Terms", () => {
  5. describe("getSolarTermDate", () => {
  6. it("should correctly calculate the solar term date for 'lesser_cold' in 2024", () => {
  7. const term: SolarTermKey = "lesser_cold";
  8. const date = getSolarTermDate(2024, 1, term);
  9. expect(date).toBe("2024-01-06");
  10. });
  11. it("should correctly calculate the solar term date for 'rain_water' in 2026 with delta adjustment", () => {
  12. const term: SolarTermKey = "rain_water";
  13. const date = getSolarTermDate(2026, 2, term);
  14. expect(date).toBe("2026-02-18");
  15. });
  16. it("should handle the case where there is no delta adjustment", () => {
  17. const term: SolarTermKey = "the_beginning_of_spring";
  18. const date = getSolarTermDate(2024, 2, term);
  19. expect(date).toBe("2024-02-04");
  20. });
  21. });
  22. describe("getSolarTerms", () => {
  23. it("should return the solar terms within the date range in 2024", () => {
  24. const start = dayjs("2024-01-01");
  25. const end = dayjs("2024-02-29");
  26. const terms = getSolarTerms(start, end);
  27. const expected: SolarTerm[] = [
  28. { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
  29. { date: "2024-01-20", term: "greater_cold", name: "大寒", index: 1 },
  30. {
  31. date: "2024-02-04",
  32. term: "the_beginning_of_spring",
  33. name: "立春",
  34. index: 1,
  35. },
  36. { date: "2024-02-19", term: "rain_water", name: "雨水", index: 1 },
  37. ];
  38. expect(terms).toEqual(expected);
  39. });
  40. it("should return an empty array if no solar terms fall within the date range", () => {
  41. const start = dayjs("2024-03-01");
  42. const end = dayjs("2024-03-31");
  43. const terms = getSolarTerms(start, end);
  44. expect(terms).toEqual([
  45. {
  46. date: "2024-03-05",
  47. name: "惊蛰",
  48. term: "the_waking_of_insects",
  49. index: 1,
  50. },
  51. {
  52. date: "2024-03-20",
  53. name: "春分",
  54. term: "the_spring_equinox",
  55. index: 1,
  56. },
  57. ]);
  58. });
  59. it("should handle a single day range", () => {
  60. const date = dayjs("2024-01-06");
  61. const terms = getSolarTerms(date, date);
  62. const expected: SolarTerm[] = [
  63. { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
  64. ];
  65. expect(terms).toEqual(expected);
  66. });
  67. });
  68. });