index.test.ts 2.4 KB

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