File size: 9,210 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import {
  addYears,
  endOfMonth,
  endOfYear,
  startOfDay,
  startOfMonth,
  startOfYear,
} from "date-fns";
import type { DayPickerProps } from "react-day-picker/types";

import { defaultDateLib } from "../classes/DateLib";

import { getNavMonths } from "./getNavMonth";

describe('when "startMonth" is not set', () => {
  test('"startMonth" should be undefined', () => {
    const [navStartMonth] = getNavMonths({}, defaultDateLib);
    expect(navStartMonth).toBeUndefined();
  });
});
describe('when "startMonth" is set', () => {
  const startMonth = new Date(2021, 4, 3);
  const props: DayPickerProps = { startMonth };
  test('"startMonth" should be the start of that month', () => {
    const [navStartMonth] = getNavMonths(props, defaultDateLib);
    const startOfThatMonth = startOfMonth(startMonth);
    expect(navStartMonth).toEqual(startOfThatMonth);
  });
  describe('when "fromYear" is set', () => {
    const fromYear = 2021;
    test('"startMonth" should be the start of that month', () => {
      const [navStartMonth] = getNavMonths(
        { ...props, fromYear },
        defaultDateLib,
      );
      const startOfThatMonth = startOfMonth(startMonth);
      expect(navStartMonth).toEqual(startOfThatMonth);
    });
  });
});
describe('when "fromYear" is set', () => {
  const fromYear = 2021;
  const props: DayPickerProps = { fromYear };
  test('"startMonth" should be the start of that year', () => {
    const [navStartMonth] = getNavMonths(props, defaultDateLib);
    const startOfThatYear = new Date(fromYear, 0, 1);
    expect(navStartMonth).toEqual(startOfThatYear);
  });
});
describe('when "endMonth" is set', () => {
  const endMonth = new Date(2021, 4, 3);
  const props: DayPickerProps = { endMonth };
  test('"endMonth" should be the end of that month', () => {
    const [, navEndMonth] = getNavMonths(props, defaultDateLib);
    const endOfThatMonth = startOfDay(endOfMonth(endMonth));
    expect(navEndMonth).toEqual(endOfThatMonth);
  });
  describe('when "fromYear" is set', () => {
    const fromYear = 2021;
    test('"endMonth" should be the end of that month', () => {
      const [, navEndMonth] = getNavMonths(
        { ...props, fromYear },
        defaultDateLib,
      );
      const endOfThatMonth = startOfDay(endOfMonth(endMonth));
      expect(navEndMonth).toEqual(endOfThatMonth);
    });
  });
});

describe('when "toYear" is set', () => {
  const toYear = 2021;
  const props: DayPickerProps = { toYear };
  test('"endMonth" should be the end of that year', () => {
    const [, navEndMonth] = getNavMonths(props, defaultDateLib);
    expect(navEndMonth).toEqual(new Date(toYear, 11, 31));
  });
});

describe.each([
  ["dropdown" as const],
  ["dropdown-years" as const],
])('when "captionLayout" is "%s"', (captionLayout) => {
  const today = new Date(2024, 4, 3);
  const props: DayPickerProps = { captionLayout, today };

  test('"startMonth" should be 100 years ago', () => {
    const [navStartMonth] = getNavMonths(props, defaultDateLib);
    const startOf100YearsAgo = startOfYear(addYears(today, -100));
    expect(navStartMonth).toEqual(startOf100YearsAgo);
  });
  test('"endMonth" should be the end of this year', () => {
    const [, navEndMonth] = getNavMonths(props, defaultDateLib);
    const endOfThisYear = startOfDay(endOfYear(today));
    expect(navEndMonth).toEqual(endOfThisYear);
  });

  describe('when "startMonth" is set', () => {
    const today = new Date(2024, 4, 3);
    const startMonth = new Date(2021, 4, 3);
    const props: DayPickerProps = { captionLayout, startMonth, today };
    test('"startMonth" should be the start of that month', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      const startOfThatMonth = startOfMonth(startMonth);
      expect(navStartMonth).toEqual(startOfThatMonth);
    });
    test('"endMonth" should be the end of this year', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      const endOfThisYear = startOfDay(endOfYear(today));
      expect(navEndMonth).toEqual(endOfThisYear);
    });
  });

  describe('when "endMonth" is set', () => {
    const today = new Date(2021, 4, 3);
    const endMonth = new Date(2022, 4, 3);
    const props: DayPickerProps = { captionLayout, endMonth, today };

    test('"startMonth" should be 100 years ago', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      const startOf100YearsAgo = startOfYear(addYears(today, -100));
      expect(navStartMonth).toEqual(startOf100YearsAgo);
    });
    test('"endMonth" should be the end of that month', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      expect(navEndMonth).toEqual(startOfDay(endOfMonth(endMonth)));
    });
  });

  describe('when "fromYear" is set', () => {
    const today = new Date(2024, 4, 3);
    const fromYear = 2022;
    const props: DayPickerProps = { captionLayout, fromYear, today };

    test('"startMonth" should be equal to the "fromYear"', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      const startOfThatYear = new Date(fromYear, 0, 1);
      expect(navStartMonth).toEqual(startOfThatYear);
    });
    test('"endMonth" should be the end of this year', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      const endOfThisYear = startOfDay(endOfYear(today));
      expect(navEndMonth).toEqual(endOfThisYear);
    });
  });

  describe('when "toYear" is set', () => {
    const today = new Date(2021, 4, 3);
    const toYear = 2022;
    const props: DayPickerProps = { captionLayout, toYear, today };

    test('"startMonth" should be 100 years ago', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      expect(navStartMonth).toEqual(startOfYear(addYears(today, -100)));
    });
    test('"endMonth" should be equal the last day of the year', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      expect(navEndMonth).toEqual(new Date(toYear, 11, 31));
    });
  });
});

describe('when "captionLayout" is "dropdown-months"', () => {
  const today = new Date(2024, 4, 3);
  const props: DayPickerProps = {
    captionLayout: "dropdown-months",
    today,
  };
  test('"startMonth" should be undefined', () => {
    const [navStartMonth] = getNavMonths(props, defaultDateLib);
    expect(navStartMonth).toBeUndefined();
  });
  test('"endMonth" should be undefined', () => {
    const [, navEndMonth] = getNavMonths(props, defaultDateLib);
    expect(navEndMonth).toBeUndefined();
  });

  describe('when "startMonth" is set', () => {
    const today = new Date(2024, 4, 3);
    const startMonth = new Date(2021, 4, 3);
    const props: DayPickerProps = {
      captionLayout: "dropdown-months",
      startMonth,
      today,
    };
    test('"startMonth" should be the start of that month', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      const startOfThatMonth = startOfMonth(startMonth);
      expect(navStartMonth).toEqual(startOfThatMonth);
    });
    test('"endMonth" should be undefined', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      expect(navEndMonth).toBeUndefined();
    });
  });

  describe('when "endMonth" is set', () => {
    const today = new Date(2021, 4, 3);
    const endMonth = new Date(2022, 4, 3);
    const props: DayPickerProps = {
      captionLayout: "dropdown-months",
      endMonth,
      today,
    };
    test('"startMonth" should be undefined', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      expect(navStartMonth).toBeUndefined();
    });
    test('"endMonth" should be the end of that month', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      const endOfThatMonth = startOfDay(endOfMonth(endMonth));
      expect(navEndMonth).toEqual(endOfThatMonth);
    });
  });

  describe('when "fromYear" is set', () => {
    const today = new Date(2024, 4, 3);
    const fromYear = 2022;
    const props: DayPickerProps = {
      captionLayout: "dropdown-months",
      fromYear,
      today,
    };
    test('"startMonth" should be equal to the start of that year', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      const startOfThatYear = new Date(fromYear, 0, 1);
      expect(navStartMonth).toEqual(startOfThatYear);
    });
    test('"endMonth" should be undefined', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      expect(navEndMonth).toBeUndefined();
    });
  });

  describe('when "toYear" is set', () => {
    const today = new Date(2021, 4, 3);
    const toYear = 2022;
    const props: DayPickerProps = {
      captionLayout: "dropdown-months",
      toYear,
      today,
    };
    test('"startMonth" should be undefined', () => {
      const [navStartMonth] = getNavMonths(props, defaultDateLib);
      expect(navStartMonth).toBeUndefined();
    });
    test('"endMonth" should be equal to the end of that year', () => {
      const [, navEndMonth] = getNavMonths(props, defaultDateLib);
      const endOfThatYear = new Date(2022, 11, 31);
      expect(navEndMonth).toEqual(endOfThatYear);
    });
  });
});