index.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { getLunarFestivals } from "../../src";
  2. import dayjs from "../../src/utils/dayjs"; // Import dayjs
  3. describe("lunarFestivals", () => {
  4. test("getLunarFestivals should return fixed lunar festivals", () => {
  5. // 测试常规固定节日
  6. const result = getLunarFestivals("2025-01-29");
  7. expect(result).toEqual([
  8. {
  9. date: "2025-01-29",
  10. name: ["春节", "鸡日", "元始天尊诞辰"],
  11. }
  12. ]);
  13. });
  14. test("should handle solar term related festivals", () => {
  15. // 测试寒食节(清明前一日)
  16. const result = getLunarFestivals("2025-04-03");
  17. expect(result).toEqual([{
  18. date: "2025-04-03",
  19. name: ["寒食节"],
  20. }]);
  21. });
  22. test("should handle special festivals", () => {
  23. // 测试除夕(农历腊月最后一日)
  24. const result = getLunarFestivals("2025-01-28");
  25. expect(result).toEqual([{
  26. date: "2025-01-28",
  27. name: ["除夕", "封井", "祭井神", "贴春联", "迎财神"],
  28. }]);
  29. });
  30. test("should filter leap month festivals", () => {
  31. const result1 = getLunarFestivals("2025-06-30")
  32. expect(result1).toEqual([{
  33. date: "2025-06-30",
  34. name: ["晒衣节"],
  35. }]);
  36. // 测试闰月不返回节日
  37. const result2 = getLunarFestivals("2025-07-30") // 2025 has leap 6th month. 2025-07-30 is 闰六月初五.
  38. expect(result2).toEqual([]); // No festival for 闰六月初五
  39. });
  40. describe("Specific Special Festival Handlers (Based on current constants.ts)", () => {
  41. test("Mother's Day (solar 2024-05-12) should NOT be identified as handler is missing, and no fixed festival on actual L04-05", () => {
  42. // Solar "2024-05-12" is Lunar L2024-04-05 via getLunarDate. LUNAR_FESTIVAL_MAP[4][5] is undefined.
  43. expect(getLunarFestivals("2024-05-12", "2024-05-12")).toEqual([]);
  44. // Solar "2025-05-11" is Lunar L2025-04-14. LUNAR_FESTIVAL_MAP[4][14] is ["吕洞宾诞辰"].
  45. expect(getLunarFestivals("2025-05-11")).toEqual([{ date: "2025-05-11", name: ["吕洞宾诞辰"] }]);
  46. });
  47. test("Father's Day (solar 2024-06-16) should NOT be identified as handler is missing, and no fixed festival on actual lunar date", () => {
  48. // Solar "2024-06-16" is L2024-05-11. LUNAR_FESTIVAL_MAP[5][11] is undefined.
  49. expect(getLunarFestivals("2024-06-16", "2024-06-16")).toEqual([]);
  50. // Solar "2025-06-15" is L2025-05-20. LUNAR_FESTIVAL_MAP[5][20] is undefined.
  51. expect(getLunarFestivals("2025-06-15")).toEqual([]);
  52. });
  53. test("Thanksgiving Day (solar 2024-11-28) should NOT be identified as handler is missing, and no fixed festival on actual lunar date", () => {
  54. // Solar "2024-11-28" is L2024-10-28. LUNAR_FESTIVAL_MAP[10][28] is undefined.
  55. expect(getLunarFestivals("2024-11-28", "2024-11-28")).toEqual([]);
  56. // Solar "2025-11-27" is L2025-10-07. LUNAR_FESTIVAL_MAP[10][7] is undefined.
  57. expect(getLunarFestivals("2025-11-27")).toEqual([]);
  58. });
  59. });
  60. describe("Edge Cases and Other Scenarios", () => {
  61. test("should return empty array for a range with no festivals", () => {
  62. expect(getLunarFestivals("2024-01-02", "2024-01-03")).toEqual([]);
  63. });
  64. test("should return empty array if start date is after end date", () => {
  65. expect(getLunarFestivals("2024-01-10", "2024-01-01")).toEqual([]);
  66. });
  67. test("should handle date with multiple fixed festivals not overlapping with special ones", () => {
  68. // Example: 七月初七 (Qixi)
  69. // 2024-08-10 is L2024-七月初七
  70. const result = getLunarFestivals("2024-08-10");
  71. // Assuming LUNAR_FESTIVAL_MAP has '七夕节' and potentially others for 7/7
  72. // For this example, let's say it's just '七夕节' and '魁星诞辰'.
  73. // LUNAR_FESTIVAL_MAP[7][7] is ["乞巧节"] as per constants.ts
  74. expect(result).toEqual([
  75. { date: "2024-08-10", name: ["乞巧节"] }
  76. ]);
  77. });
  78. test("should handle a long range with multiple festivals", () => {
  79. // A range of about 2 months
  80. const results = getLunarFestivals("2024-08-01", "2024-09-30");
  81. // Check for presence of some key festivals, not exact list which could be long
  82. const festivalDates = results.map(r => r.date);
  83. expect(festivalDates).toContain("2024-08-10"); // 乞巧节 (L7/7)
  84. expect(festivalDates).toContain("2024-08-18"); // 中元节 (L7/15)
  85. expect(festivalDates).toContain("2024-09-17"); // 中秋节 (L8/15)
  86. const qixi = results.find(r => r.date === "2024-08-10");
  87. expect(qixi?.name).toEqual(expect.arrayContaining(["乞巧节"])); // Corrected name
  88. const zhongqiu = results.find(r => r.date === "2024-09-17");
  89. expect(zhongqiu?.name).toEqual(expect.arrayContaining(["中秋节"]));
  90. });
  91. test("should NOT find Dragon Boat Festival for solar 2028-05-30 as it's L05-07", () => {
  92. // Solar "2028-05-30" is L2028-05-07 (五月初七) according to getLunarDate.
  93. // LUNAR_FESTIVAL_MAP[5][7] is undefined.
  94. const dragonBoatResult = getLunarFestivals("2028-05-30");
  95. expect(dragonBoatResult).toEqual([]);
  96. });
  97. test("should find no fixed festival for solar 2028-05-26 as it's L05-03", () => {
  98. // Solar "2028-05-26" is L2028-05-03 according to getLunarDate.
  99. // LUNAR_FESTIVAL_MAP[5][3] is undefined.
  100. const result = getLunarFestivals("2028-05-26");
  101. expect(result).toEqual([]);
  102. // For context: The actual Dragon Boat Festival (L2028-05-05) would be on a different solar date.
  103. // The test for solar "2028-06-29" (which is L2028-06-07) correctly expects [].
  104. const leapTestDateResult = getLunarFestivals("2028-06-29"); // This was L2028-06-07.
  105. expect(leapTestDateResult).toEqual([]);
  106. });
  107. test("getLunarFestivals with undefined start/end should query for current day (mocked to solar 2024-05-12, which is L04-05)", () => {
  108. const mockToday = "2024-05-12"; // Solar "2024-05-12" is L2024-04-05.
  109. // LUNAR_FESTIVAL_MAP[4][5] is undefined. No fixed festival.
  110. // Mother's Day handler is missing.
  111. const originalDayjs = dayjs;
  112. // @ts-ignore
  113. dayjs = jest.fn((dateInput?: any) => {
  114. if (dateInput === undefined || dateInput === null || dateInput === '') {
  115. return originalDayjs(mockToday);
  116. }
  117. return originalDayjs(dateInput);
  118. });
  119. Object.assign(dayjs, originalDayjs);
  120. expect(getLunarFestivals()).toEqual([]); // Expect empty as L04-05 has no festival
  121. // @ts-ignore
  122. dayjs = originalDayjs; // Restore original dayjs
  123. });
  124. });
  125. test("should handle cross-year scenarios", () => {
  126. // 测试多天与跨年场景
  127. const result = getLunarFestivals("2024-11-15", "2025-01-30");
  128. expect(result).toEqual([
  129. {
  130. date: "2024-11-15",
  131. name: ["下元节", "水官诞辰"],
  132. },
  133. {
  134. date: "2025-01-07",
  135. name: ["腊八节"],
  136. },
  137. {
  138. date: "2025-01-22",
  139. name: ["官家送灶"],
  140. },
  141. {
  142. date: "2025-01-23",
  143. name: ["民间送灶"],
  144. },
  145. {
  146. date: "2025-01-24",
  147. name: ["接玉皇"],
  148. },
  149. {
  150. date: "2025-01-28",
  151. name: ["除夕", "封井", "祭井神", "贴春联", "迎财神"],
  152. },
  153. {
  154. date: "2025-01-29",
  155. name: ["春节", "鸡日", "元始天尊诞辰"],
  156. },
  157. {
  158. date: "2025-01-30",
  159. name: ["犬日"],
  160. },
  161. ]);
  162. });
  163. });