index.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import dayjs from "../../src/utils/dayjs";
  2. import { getSolarTermDate, getSolarTerms, getSolarTermsInRange, 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 1998", () => {
  7. const term: SolarTermKey = "lesser_cold";
  8. const date = getSolarTermDate(1998, 1, term);
  9. expect(date).toBe("1998-01-05");
  10. });
  11. it("should correctly calculate the solar term date for 'lesser_cold' in 2024", () => {
  12. const term: SolarTermKey = "lesser_cold";
  13. const date = getSolarTermDate(2024, 1, term);
  14. expect(date).toBe("2024-01-06");
  15. });
  16. it("should correctly calculate the solar term date for 'rain_water' in 2026 with delta adjustment", () => {
  17. const term: SolarTermKey = "rain_water";
  18. const date = getSolarTermDate(2026, 2, term);
  19. expect(date).toBe("2026-02-18");
  20. });
  21. it("should handle the case where there is no delta adjustment", () => {
  22. const term: SolarTermKey = "the_beginning_of_spring";
  23. const date = getSolarTermDate(2024, 2, term);
  24. expect(date).toBe("2024-02-04");
  25. });
  26. });
  27. describe("getSolarTerms", () => {
  28. it("should return the solar terms within the date range in 2024", () => {
  29. const start = dayjs("2024-01-01");
  30. const end = dayjs("2024-02-29");
  31. const terms = getSolarTerms(start, end);
  32. const expected: SolarTerm[] = [
  33. { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
  34. { date: "2024-01-20", term: "greater_cold", name: "大寒", index: 1 },
  35. {
  36. date: "2024-02-04",
  37. term: "the_beginning_of_spring",
  38. name: "立春",
  39. index: 1,
  40. },
  41. { date: "2024-02-19", term: "rain_water", name: "雨水", index: 1 },
  42. ];
  43. expect(terms).toEqual(expected);
  44. });
  45. it("should return an empty array if no solar terms fall within the date range", () => {
  46. const start = dayjs("2024-03-01");
  47. const end = dayjs("2024-03-31");
  48. const terms = getSolarTerms(start, end);
  49. expect(terms).toEqual([
  50. {
  51. date: "2024-03-05",
  52. name: "惊蛰",
  53. term: "the_waking_of_insects",
  54. index: 1,
  55. },
  56. {
  57. date: "2024-03-20",
  58. name: "春分",
  59. term: "the_spring_equinox",
  60. index: 1,
  61. },
  62. ]);
  63. });
  64. it("should handle a single day range", () => {
  65. const date = dayjs("2024-01-06");
  66. const terms = getSolarTerms(date, date);
  67. const expected: SolarTerm[] = [
  68. { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
  69. ];
  70. expect(terms).toEqual(expected);
  71. });
  72. it("should handle a single day range", () => {
  73. const date = dayjs("2024-01-06");
  74. const terms = getSolarTerms(date);
  75. const expected: SolarTerm[] = [
  76. { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
  77. ];
  78. expect(terms).toEqual(expected);
  79. });
  80. });
  81. describe('getSolarTermsInRange', () => {
  82. test('should get solar terms within the specified date range', () => {
  83. const start = dayjs('2024-01-04');
  84. const end = dayjs('2024-01-07');
  85. const result = getSolarTermsInRange(start, end);
  86. expect(result).toEqual([
  87. {
  88. date: '2024-01-04',
  89. term: 'the_winter_solstice',
  90. name: '冬至',
  91. index: 14
  92. },
  93. {
  94. date: '2024-01-05',
  95. term: 'the_winter_solstice',
  96. name: '冬至',
  97. index: 15
  98. },
  99. { date: '2024-01-06', term: 'lesser_cold', name: '小寒', index: 1 },
  100. { date: '2024-01-07', term: 'lesser_cold', name: '小寒', index: 2 }
  101. ]);
  102. });
  103. test('should get solar terms for the current day when end date is not provided', () => {
  104. const start = dayjs('2024-01-20');
  105. const result = getSolarTermsInRange(start);
  106. expect(result).toEqual([{ date: '2024-01-20', term: 'greater_cold', name: '大寒', index: 1 }]);
  107. });
  108. test('should handle date range spanning across the year boundary', () => {
  109. const start = dayjs('2024-12-30');
  110. const end = dayjs('2025-01-02');
  111. const result = getSolarTermsInRange(start, end);
  112. expect(result).toEqual([
  113. {
  114. date: '2024-12-30',
  115. term: 'the_winter_solstice',
  116. name: '冬至',
  117. index: 10
  118. },
  119. {
  120. date: '2024-12-31',
  121. term: 'the_winter_solstice',
  122. name: '冬至',
  123. index: 11
  124. },
  125. {
  126. date: '2025-01-01',
  127. term: 'the_winter_solstice',
  128. name: '冬至',
  129. index: 12
  130. },
  131. {
  132. date: '2025-01-02',
  133. term: 'the_winter_solstice',
  134. name: '冬至',
  135. index: 13
  136. }
  137. ]);
  138. });
  139. });
  140. });