4.77 kBPlain TextView Raw
1import { dateLib } from "../lib";
2
3import { getDates } from "./getDates";
4
5describe("when the first month and the last month are the same", () => {
6 describe("when the month has 6 weeks", () => {
7 const month = new Date(2023, 11, 1);
8 describe("when not using fixed weeks", () => {
9 it("should return 42 dates", () => {
10 const dates = getDates(
11 [month],
12 undefined,
13 {
14 fixedWeeks: false
15 },
16 dateLib
17 );
18 expect(dates).toHaveLength(42);
19 expect(dates[0]).toEqual(new Date(2023, 10, 26));
20 expect(dates[dates.length - 1]).toEqual(new Date(2024, 0, 6));
21 });
22 });
23 describe("when using fixed weeks", () => {
24 it("should return 42 dates", () => {
25 const dates = getDates(
26 [month],
27 undefined,
28 {
29 fixedWeeks: true
30 },
31 dateLib
32 );
33 expect(dates).toHaveLength(42);
34 expect(dates[0]).toEqual(new Date(2023, 10, 26));
35 expect(dates[dates.length - 1]).toEqual(new Date(2024, 0, 6));
36 });
37 });
38 });
39 describe("when the month has 5 weeks", () => {
40 const month = new Date(2023, 4, 1);
41 describe("when not using fixed weeks", () => {
42 it("should return 35 dates", () => {
43 const dates = getDates(
44 [month],
45 undefined,
46 {
47 fixedWeeks: false
48 },
49 dateLib
50 );
51 expect(dates).toHaveLength(35);
52 expect(dates[0]).toEqual(new Date(2023, 3, 30));
53 expect(dates[dates.length - 1]).toEqual(new Date(2023, 5, 3));
54 });
55 });
56 describe("when using fixed weeks", () => {
57 it("should return 42 dates", () => {
58 const dates = getDates(
59 [month],
60 undefined,
61 {
62 fixedWeeks: true
63 },
64 dateLib
65 );
66 expect(dates).toHaveLength(42);
67 expect(dates[0]).toEqual(new Date(2023, 3, 30));
68 expect(dates[dates.length - 1]).toEqual(new Date(2023, 5, 10));
69 });
70 });
71 });
72 describe("when using Monday as first day of the week", () => {
73 const month = new Date(2023, 4, 1);
74 it("the first day should be Monday", () => {
75 const dates = getDates(
76 [month],
77 undefined,
78 {
79 weekStartsOn: 1
80 },
81 dateLib
82 );
83 expect(dates[0]).toBeMonday();
84 expect(dates[0]).toEqual(new Date(2023, 4, 1));
85 expect(dates[dates.length - 1]).toEqual(new Date(2023, 5, 4));
86 });
87 });
88 describe("when using a max date", () => {
89 const month = new Date(2023, 4, 1);
90 const maxDate = new Date(2023, 4, 15);
91
92 it("the last day should be the max date", () => {
93 const dates = getDates([month], maxDate, { weekStartsOn: 1 }, dateLib);
94 expect(dates).toHaveLength(15);
95 expect(dates[dates.length - 1]).toEqual(maxDate);
96 });
97 });
98 describe("when using ISO weeks", () => {
99 const month = new Date(2023, 4, 1);
100 it("the first day should be Monday", () => {
101 const dates = getDates([month], undefined, { ISOWeek: true }, dateLib);
102 expect(dates[0]).toBeMonday();
103 expect(dates[0]).toEqual(new Date(2023, 4, 1));
104 expect(dates[dates.length - 1]).toEqual(new Date(2023, 5, 4));
105 });
106 });
107});
108
109describe("when the first month and the last month are different", () => {
110 const firstMonth = new Date(2023, 4, 1);
111 const lastMonth = new Date(2023, 11, 1);
112 describe("when not using fixed weeks", () => {
113 it("should return an array of dates", () => {
114 const dates = getDates(
115 [firstMonth, lastMonth],
116 undefined,
117 {
118 fixedWeeks: false
119 },
120 dateLib
121 );
122 expect(dates).toHaveLength(252);
123 expect(dates[0]).toEqual(new Date(2023, 3, 30));
124 expect(dates[dates.length - 1]).toEqual(new Date(2024, 0, 6));
125 });
126 });
127 describe("when using a max date", () => {
128 const firstMonth = new Date(2023, 4, 1);
129 const lastMonth = new Date(2023, 11, 1);
130 const maxDate = new Date(2023, 5, 15);
131
132 it("the last day should be the max date", () => {
133 const dates = getDates(
134 [firstMonth, lastMonth],
135 maxDate,
136 {
137 weekStartsOn: 1
138 },
139 dateLib
140 );
141 expect(dates).toHaveLength(46);
142 expect(dates[dates.length - 1]).toEqual(maxDate);
143 });
144 });
145 describe("when using ISO weeks", () => {
146 const month = new Date(2023, 4, 1);
147 it("the first day should be Monday", () => {
148 const dates = getDates([month], undefined, { ISOWeek: true }, dateLib);
149 expect(dates[0]).toBeMonday();
150 expect(dates[0]).toEqual(new Date(2023, 4, 1));
151 expect(dates[dates.length - 1]).toEqual(new Date(2023, 5, 4));
152 });
153 });
154});