UNPKG

2.8 kBPlain TextView Raw
1import { DayPickerProps } from "react-day-picker";
2
3import { CalendarMonth } from "../classes";
4import { dateLib } from "../lib";
5
6import { getMonths } from "./getMonths";
7
8const mockDates = [
9 new Date(2023, 4, 27), // May 1, 2023
10 new Date(2023, 5, 1), // June 1, 2023
11 new Date(2023, 5, 8), // June 2, 2023
12 new Date(2023, 5, 15), // June 15, 2023
13 new Date(2023, 5, 22), // June 22, 2023
14 new Date(2023, 5, 30), // June 30, 2023
15 new Date(2023, 6, 6) // June 30, 2023
16];
17
18const mockProps: Pick<
19 DayPickerProps,
20 | "fixedWeeks"
21 | "ISOWeek"
22 | "locale"
23 | "weekStartsOn"
24 | "reverseMonths"
25 | "firstWeekContainsDate"
26> = {
27 fixedWeeks: false,
28 ISOWeek: false,
29 locale: undefined,
30 weekStartsOn: 0, // Sunday
31 reverseMonths: false,
32 firstWeekContainsDate: 1
33};
34
35it("should return the correct months without ISO weeks and reverse months", () => {
36 const displayMonths = [new Date(2023, 5, 1)]; // June 2023
37
38 const result = getMonths(displayMonths, mockDates, mockProps, dateLib);
39
40 expect(result).toHaveLength(1);
41 expect(result[0]).toBeInstanceOf(CalendarMonth);
42 expect(result[0].weeks).toHaveLength(5); // June 2023 has 5 weeks
43});
44
45it("should handle ISO weeks", () => {
46 const displayMonths = [new Date(2023, 5, 1)]; // June 2023
47
48 const isoProps = { ...mockProps, ISOWeek: true };
49
50 const result = getMonths(displayMonths, mockDates, isoProps, dateLib);
51
52 expect(result).toHaveLength(1);
53 expect(result[0]).toBeInstanceOf(CalendarMonth);
54 expect(result[0].weeks).toHaveLength(5); // June 2023 has 5 ISO weeks
55});
56
57it("should handle reverse months", () => {
58 const displayMonths = [
59 new Date(2023, 4, 1), // May 2023
60 new Date(2023, 5, 1) // June 2023
61 ];
62
63 const reverseProps = { ...mockProps, reverseMonths: true };
64
65 const result = getMonths(displayMonths, mockDates, reverseProps, dateLib);
66
67 expect(result).toHaveLength(2);
68 expect(result[0].date).toEqual(new Date(2023, 5, 1)); // June 2023
69 expect(result[1].date).toEqual(new Date(2023, 4, 1)); // May 2023
70});
71
72it("should handle fixed weeks", () => {
73 const displayMonths = [new Date(2023, 5, 1)]; // June 2023
74
75 const fixedWeeksProps = { ...mockProps, fixedWeeks: true };
76
77 const result = getMonths(displayMonths, mockDates, fixedWeeksProps, dateLib);
78
79 expect(result).toHaveLength(1);
80 expect(result[0]).toBeInstanceOf(CalendarMonth);
81 expect(result[0].weeks).toHaveLength(6); // Fixed weeks should ensure 6 weeks in the month view
82});
83
84it("should handle months with no dates", () => {
85 const displayMonths = [new Date(2023, 5, 1)]; // June 2023
86
87 const result = getMonths(displayMonths, [], mockProps, dateLib);
88
89 expect(result).toHaveLength(1);
90 expect(result[0]).toBeInstanceOf(CalendarMonth);
91 expect(result[0].weeks).toHaveLength(0); // No dates should result in no weeks
92});