Browse Source

feat: enhance test cases

yaavi 8 months ago
parent
commit
db3068268a

+ 4 - 2
src/solar_lunar/index.ts

@@ -84,7 +84,7 @@ const getLunarYearText = (lunarYear: number): string => {
  * @param endYear 结束农历年份
  * @returns 农历年份列表
  */
-const getLunarYears = (startYear: number, endYear: number) => {
+export const getLunarYears = (startYear: number, endYear: number) => {
   const years = [];
   for (let i = startYear; i <= endYear; i++) {
     years.push({
@@ -101,7 +101,7 @@ const getLunarYears = (startYear: number, endYear: number) => {
  * @param year 年份
  * @returns 农历闰月月份
  */
-const getYearLeapMonth = (year: number) => {
+export const getYearLeapMonth = (year: number) => {
   const leap = yearLeapMonth(year)
   return {
     year,
@@ -262,6 +262,8 @@ export const getSolarDateFromLunar = (lunarDate: ConfigType): {
 }
 
 export default {
+  getLunarYears,
+  getYearLeapMonth,
   getLunarDate,
   getLunarDatesInRange,
   getSolarDateFromLunar,

+ 60 - 2
test/holidays/index.test.ts

@@ -10,6 +10,12 @@ import {
 } from '../../src';
 
 describe('Holiday Functions', () => {
+  test('should throw an error for invalid date', () => {
+    expect(() => isHoliday('invalid-date')).toThrow(
+      'unsupported type object, expected type is Date or Dayjs'
+    );
+  });
+
   test('isHoliday should return correct boolean values', () => {
     const date1 = '2024-05-01';
     const date2 = '2024-05-06';
@@ -34,6 +40,28 @@ describe('Holiday Functions', () => {
     expect(isInLieu(date2)).toBe(true);
   });
 
+  test('getDayDetail should return correct details', () => {
+    const date = '2024-04-29';
+    const detail = getDayDetail(date);
+
+    expect(detail).toEqual({
+      date: '2024-04-29',
+      work: true,
+      name: "Monday",
+    });
+  });
+
+  test('getDayDetail should return correct details', () => {
+    const date = '2025-01-26';
+    const detail = getDayDetail(date);
+
+    expect(detail).toEqual({
+      date: '2025-01-26',
+      work: true,
+      name: "Spring Festival,春节,4",
+    });
+  });
+
   test('getDayDetail should return correct details', () => {
     const date = '2024-05-01';
     const detail = getDayDetail(date);
@@ -59,25 +87,55 @@ describe('Holiday Functions', () => {
   test('getHolidaysInRange should return correct holidays within a range', () => {
     const start = '2024-05-01';
     const end = '2024-05-31';
-    const holidaysInRange = getHolidaysInRange(start, end, true);
+    const holidaysInRange = getHolidaysInRange(start, end, false);
 
     expect(holidaysInRange).toContain('2024-05-01');
   });
 
+  test('getHolidaysInRange should return correct holidays within a range', () => {
+    const start = '2024-05-01';
+    const end = '2024-05-31';
+    const holidaysInRange = getHolidaysInRange(start, end, true);
+
+    expect(holidaysInRange).toContain('2024-05-12');
+  });
+
   test('getWorkdaysInRange should return correct workdays within a range', () => {
     const start = '2024-05-01';
     const end = '2024-05-31';
-    const workdaysInRange = getWorkdaysInRange(start, end, true);
+    const workdaysInRange = getWorkdaysInRange(start, end, false);
 
     expect(workdaysInRange).toContain('2024-05-06');
   });
 
+  test('getWorkdaysInRange should return correct workdays within a range', () => {
+    const start = '2024-05-01';
+    const end = '2024-05-31';
+    const workdaysInRange = getWorkdaysInRange(start, end, true);
+
+    expect(workdaysInRange).toContain('2024-05-11');
+  });
+
   test('findWorkday should return correct workday', () => {
     const date = '2024-05-01';
     const nextWorkday = findWorkday(1, date);
 
     expect(nextWorkday).toBe('2024-05-06');
   });
+
+  test('findWorkday should return correct workday', () => {
+    const date = '2024-05-11';
+    const nextWorkday = findWorkday(0, date);
+
+    expect(nextWorkday).toBe('2024-05-11');
+  });
+
+  test('findWorkday should return correct workday', () => {
+    const date = '2024-05-12';
+    const nextWorkday = findWorkday(0, date);
+
+    expect(nextWorkday).toBe('2024-05-13');
+  });
 });
 
 describe('Arrangement Class', () => {

+ 19 - 0
test/solar_lunar/index.test.ts

@@ -1,4 +1,6 @@
 import {
+  getLunarYears,
+  getYearLeapMonth,
   getLunarDate,
   getLunarDatesInRange,
   getSolarDateFromLunar,
@@ -70,6 +72,23 @@ describe("solar_lunar", () => {
     expect(result).toEqual({ date: "2001-04-27", leapMonthDate: "2001-05-27" });
   });
 
+  test("getLunarYears should return correct", () => {
+    let result = getLunarYears(2001, 2003);
+    expect(result).toEqual([
+      {"lunarYear": "辛巳年", "lunarYearCN": "二零零一", "year": 2001},
+      {"lunarYear": "壬午年", "lunarYearCN": "二零零二", "year": 2002},
+      {"lunarYear": "癸未年", "lunarYearCN": "二零零三", "year": 2003}
+    ]);
+  });
+
+  test("getYearLeapMonth should return correct", () => {
+    let result = getYearLeapMonth(2022);
+    expect(result).toEqual({"days": 0, "leapMonth": undefined, "leapMonthCN": undefined, "year": 2022});
+
+    result = getYearLeapMonth(2023);
+    expect(result).toEqual({"days": 29, "leapMonth": 2, "leapMonthCN": "闰二月", "year": 2023});
+  });
+
   test("getLunarDatesInRange should return correct lunar dates for a given solar date range", () => {
     let result = getLunarDatesInRange("2001-05-21", "2001-05-26");
     expect(result).toEqual([

+ 83 - 1
test/solar_terms/index.test.ts

@@ -1,10 +1,16 @@
 import dayjs from "../../src/utils/dayjs";
-import { getSolarTermDate, getSolarTerms, type SolarTerm } from "../../src";
+import { getSolarTermDate, getSolarTerms, getSolarTermsInRange, type SolarTerm } from "../../src";
 
 import type { SolarTermKey } from "../../src/solar_terms/constants";
 
 describe("Solar Terms", () => {
   describe("getSolarTermDate", () => {
+    it("should correctly calculate the solar term date for 'lesser_cold' in 1998", () => {
+      const term: SolarTermKey = "lesser_cold";
+      const date = getSolarTermDate(1998, 1, term);
+      expect(date).toBe("1998-01-05");
+    });
+
     it("should correctly calculate the solar term date for 'lesser_cold' in 2024", () => {
       const term: SolarTermKey = "lesser_cold";
       const date = getSolarTermDate(2024, 1, term);
@@ -71,5 +77,81 @@ describe("Solar Terms", () => {
       ];
       expect(terms).toEqual(expected);
     });
+
+    it("should handle a single day range", () => {
+      const date = dayjs("2024-01-06");
+      const terms = getSolarTerms(date);
+      const expected: SolarTerm[] = [
+        { date: "2024-01-06", term: "lesser_cold", name: "小寒", index: 1 },
+      ];
+      expect(terms).toEqual(expected);
+    });
+  });
+
+  describe('getSolarTermsInRange', () => {
+    test('should get solar terms within the specified date range', () => {
+      const start = dayjs('2024-01-04');
+      const end = dayjs('2024-01-07');
+  
+      const result = getSolarTermsInRange(start, end);
+      expect(result).toEqual([
+        {
+          date: '2024-01-04',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 14
+        },
+        {
+          date: '2024-01-05',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 15
+        },
+        { date: '2024-01-06', term: 'lesser_cold', name: '小寒', index: 1 },
+        { date: '2024-01-07', term: 'lesser_cold', name: '小寒', index: 2 }
+      ]);
+    });
+  
+    test('should get solar terms for the current day when end date is not provided', () => {
+      const start = dayjs('2024-01-20');
+  
+      const result = getSolarTermsInRange(start);
+  
+      expect(result).toEqual([{ date: '2024-01-20', term: 'greater_cold', name: '大寒', index: 1 }]);
+    });
+  
+    test('should handle date range spanning across the year boundary', () => {
+      const start = dayjs('2024-12-30');
+      const end = dayjs('2025-01-02');
+  
+      const result = getSolarTermsInRange(start, end);
+  
+      expect(result).toEqual([
+        {
+          date: '2024-12-30',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 10
+        },
+        {
+          date: '2024-12-31',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 11
+        },
+        {
+          date: '2025-01-01',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 12
+        },
+        {
+          date: '2025-01-02',
+          term: 'the_winter_solstice',
+          name: '冬至',
+          index: 13
+        }
+      ]);
+    });
   });
 });