1
1
import { renderCUI } from "@/utils/test-utils" ;
2
2
import { DateRangePicker } from "./DateRangePicker" ;
3
3
import userEvent from "@testing-library/user-event" ;
4
+ import { getPredefinedMonthsByNumber } from "./utils" ;
5
+ import { fireEvent } from "@testing-library/dom" ;
4
6
5
7
describe ( "DateRangePicker" , ( ) => {
6
8
it ( "opens the calendar on click" , async ( ) => {
@@ -149,6 +151,28 @@ describe("DateRangePicker", () => {
149
151
150
152
expect ( handleSelectDate ) . not . toHaveBeenCalled ( ) ;
151
153
} ) ;
154
+
155
+ it ( "allows restricting the max range length" , async ( ) => {
156
+ const startDate = new Date ( "07-04-2020" ) ;
157
+ const handleSelectDate = vi . fn ( ) ;
158
+ const user = userEvent . setup ( { advanceTimers : vi . advanceTimersByTime } ) ;
159
+
160
+ const { getByTestId, findByText } = renderCUI (
161
+ < DateRangePicker
162
+ startDate = { startDate }
163
+ onSelectDateRange = { handleSelectDate }
164
+ maxRangeLength = { 15 }
165
+ />
166
+ ) ;
167
+
168
+ user . click ( getByTestId ( "daterangepicker-input" ) ) ;
169
+ user . click ( await findByText ( "25" ) ) ;
170
+
171
+ expect ( handleSelectDate ) . not . toHaveBeenCalled ( ) ;
172
+
173
+ fireEvent . click ( await findByText ( "15" ) ) ;
174
+ expect ( handleSelectDate ) . toHaveBeenCalled ( ) ;
175
+ } ) ;
152
176
} ) ;
153
177
154
178
describe ( "predefined date ranges" , ( ) => {
@@ -160,7 +184,7 @@ describe("DateRangePicker", () => {
160
184
vi . useRealTimers ( ) ;
161
185
} ) ;
162
186
163
- it ( "doesn't show any preselected dates if the value isn't set" , async ( ) => {
187
+ it ( "doesn't show any preselected dates if the predefined dates list isn't set" , async ( ) => {
164
188
const handleSelectDate = vi . fn ( ) ;
165
189
166
190
const { getByTestId, queryByTestId } = renderCUI (
@@ -172,33 +196,58 @@ describe("DateRangePicker", () => {
172
196
expect ( queryByTestId ( "predefined-dates-list" ) ) . not . toBeInTheDocument ( ) ;
173
197
} ) ;
174
198
175
- it ( "shows dates in the past if the value is negative" , async ( ) => {
199
+ it ( "shows dates in the past if getPredefinedMonthsByNumber's value is negative" , async ( ) => {
176
200
const handleSelectDate = vi . fn ( ) ;
177
201
202
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
178
203
const { getByTestId, getByText } = renderCUI (
179
204
< DateRangePicker
180
205
onSelectDateRange = { handleSelectDate }
181
- predefinedDatesCount = { - 6 }
206
+ predefinedDatesList = { predefinedDatesList }
182
207
/>
183
208
) ;
184
209
185
210
await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
186
211
187
- expect ( getByText ( "Jul 2020" ) ) . toBeInTheDocument ( ) ;
212
+ expect ( getByText ( "Jul 01, 2020 - Jul 04, 2020" ) ) . toBeInTheDocument ( ) ;
213
+ expect ( getByText ( "Jun 2020" ) ) . toBeInTheDocument ( ) ;
214
+ expect ( getByText ( "May 2020" ) ) . toBeInTheDocument ( ) ;
215
+ expect ( getByText ( "Apr 2020" ) ) . toBeInTheDocument ( ) ;
216
+ expect ( getByText ( "Mar 2020" ) ) . toBeInTheDocument ( ) ;
217
+ expect ( getByText ( "Feb 2020" ) ) . toBeInTheDocument ( ) ;
218
+ } ) ;
219
+
220
+ it ( "doesn't show a range of a single day when using the past six months and its the first of the month" , async ( ) => {
221
+ vi . setSystemTime ( new Date ( "07-01-2020" ) ) ;
222
+
223
+ const handleSelectDate = vi . fn ( ) ;
224
+
225
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
226
+ const { getByTestId, getByText, queryByText } = renderCUI (
227
+ < DateRangePicker
228
+ onSelectDateRange = { handleSelectDate }
229
+ predefinedDatesList = { predefinedDatesList }
230
+ />
231
+ ) ;
232
+
233
+ await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
234
+
235
+ expect ( queryByText ( "Jul" ) ) . not . toBeInTheDocument ( ) ;
188
236
expect ( getByText ( "Jun 2020" ) ) . toBeInTheDocument ( ) ;
189
237
expect ( getByText ( "May 2020" ) ) . toBeInTheDocument ( ) ;
190
238
expect ( getByText ( "Apr 2020" ) ) . toBeInTheDocument ( ) ;
191
239
expect ( getByText ( "Mar 2020" ) ) . toBeInTheDocument ( ) ;
192
240
expect ( getByText ( "Feb 2020" ) ) . toBeInTheDocument ( ) ;
193
241
} ) ;
194
242
195
- it ( "shows dates in the future if the value is positive" , async ( ) => {
243
+ it ( "shows dates in the future if getPredefinedMonthsByNumber's value is positive" , async ( ) => {
196
244
const handleSelectDate = vi . fn ( ) ;
197
245
246
+ const predefinedDatesList = getPredefinedMonthsByNumber ( 6 ) ;
198
247
const { getByTestId, getByText } = renderCUI (
199
248
< DateRangePicker
200
249
onSelectDateRange = { handleSelectDate }
201
- predefinedDatesCount = { 6 }
250
+ predefinedDatesList = { predefinedDatesList }
202
251
/>
203
252
) ;
204
253
@@ -212,69 +261,72 @@ describe("DateRangePicker", () => {
212
261
expect ( getByText ( "Dec 2020" ) ) . toBeInTheDocument ( ) ;
213
262
} ) ;
214
263
215
- it ( "allows showing the full calendar" , async ( ) => {
264
+ it ( "shows the current current month if it's the first day of month if getPredefinedMonthsByNumber's value is positive" , async ( ) => {
265
+ vi . setSystemTime ( new Date ( "07-01-2020" ) ) ;
216
266
const handleSelectDate = vi . fn ( ) ;
217
267
218
- const { getByTestId, getByText, queryByTestId } = renderCUI (
268
+ const predefinedDatesList = getPredefinedMonthsByNumber ( 6 ) ;
269
+ const { getByTestId, getByText } = renderCUI (
219
270
< DateRangePicker
220
271
onSelectDateRange = { handleSelectDate }
221
- predefinedDatesCount = { - 12 }
272
+ predefinedDatesList = { predefinedDatesList }
222
273
/>
223
274
) ;
224
275
225
- expect ( queryByTestId ( "datepicker-calendar-container" ) ) . not . toBeInTheDocument ( ) ;
226
-
227
276
await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
228
- await userEvent . click ( getByText ( "Custom time period" ) ) ;
229
277
230
- expect ( getByTestId ( "datepicker-calendar-container" ) ) . toBeInTheDocument ( ) ;
278
+ expect ( getByText ( "Jul 2020" ) ) . toBeInTheDocument ( ) ;
279
+ expect ( getByText ( "Aug 2020" ) ) . toBeInTheDocument ( ) ;
280
+ expect ( getByText ( "Sep 2020" ) ) . toBeInTheDocument ( ) ;
281
+ expect ( getByText ( "Oct 2020" ) ) . toBeInTheDocument ( ) ;
282
+ expect ( getByText ( "Nov 2020" ) ) . toBeInTheDocument ( ) ;
283
+ expect ( getByText ( "Dec 2020" ) ) . toBeInTheDocument ( ) ;
231
284
} ) ;
232
285
233
- it ( "shows at maximum six dates in the past or future " , async ( ) => {
286
+ it ( "allows showing the full calendar " , async ( ) => {
234
287
const handleSelectDate = vi . fn ( ) ;
235
288
236
- const { getByTestId, getByText, queryByText } = renderCUI (
289
+ const predefinedDatesList = getPredefinedMonthsByNumber ( 6 ) ;
290
+ const { getByTestId, getByText, queryByTestId } = renderCUI (
237
291
< DateRangePicker
238
292
onSelectDateRange = { handleSelectDate }
239
- predefinedDatesCount = { - 12 }
293
+ predefinedDatesList = { predefinedDatesList }
240
294
/>
241
295
) ;
242
296
243
- await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
297
+ expect ( queryByTestId ( "datepicker-calendar-container" ) ) . not . toBeInTheDocument ( ) ;
244
298
245
- expect ( getByText ( "Jul 2020" ) ) . toBeInTheDocument ( ) ;
246
- expect ( getByText ( "Jun 2020" ) ) . toBeInTheDocument ( ) ;
247
- expect ( getByText ( "May 2020" ) ) . toBeInTheDocument ( ) ;
248
- expect ( getByText ( "Apr 2020" ) ) . toBeInTheDocument ( ) ;
249
- expect ( getByText ( "Mar 2020" ) ) . toBeInTheDocument ( ) ;
250
- expect ( getByText ( "Feb 2020" ) ) . toBeInTheDocument ( ) ;
299
+ await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
300
+ await userEvent . click ( getByText ( "Custom time period" ) ) ;
251
301
252
- expect ( queryByText ( "Jan 2020 ") ) . not . toBeInTheDocument ( ) ;
302
+ expect ( getByTestId ( "datepicker-calendar-container ") ) . toBeInTheDocument ( ) ;
253
303
} ) ;
254
304
255
305
it ( "selects up to the current date if the current month is selected" , async ( ) => {
256
306
const handleSelectDate = vi . fn ( ) ;
257
307
308
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
258
309
const { getByTestId, getByText } = renderCUI (
259
310
< DateRangePicker
260
311
onSelectDateRange = { handleSelectDate }
261
- predefinedDatesCount = { - 6 }
312
+ predefinedDatesList = { predefinedDatesList }
262
313
/>
263
314
) ;
264
315
265
316
await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
266
317
267
- await userEvent . click ( getByText ( "Jul 2020" ) ) ;
318
+ await userEvent . click ( getByTestId ( "Jul 01, 2020 - Jul 04, 2020" ) ) ;
268
319
expect ( getByText ( "Jul 01, 2020 – Jul 04, 2020" ) ) . toBeInTheDocument ( ) ;
269
320
} ) ;
270
321
271
322
it ( "selects the full month if a date in the past or future is selected" , async ( ) => {
272
323
const handleSelectDate = vi . fn ( ) ;
273
324
325
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
274
326
const { getByTestId, getByText } = renderCUI (
275
327
< DateRangePicker
276
328
onSelectDateRange = { handleSelectDate }
277
- predefinedDatesCount = { - 6 }
329
+ predefinedDatesList = { predefinedDatesList }
278
330
/>
279
331
) ;
280
332
@@ -287,10 +339,11 @@ describe("DateRangePicker", () => {
287
339
it ( "shows the selected month if an entire month is manually selected" , async ( ) => {
288
340
const handleSelectDate = vi . fn ( ) ;
289
341
342
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
290
343
const { getByTestId, getAllByText, getByText } = renderCUI (
291
344
< DateRangePicker
292
345
onSelectDateRange = { handleSelectDate }
293
- predefinedDatesCount = { - 6 }
346
+ predefinedDatesList = { predefinedDatesList }
294
347
/>
295
348
) ;
296
349
@@ -311,16 +364,17 @@ describe("DateRangePicker", () => {
311
364
vi . setSystemTime ( new Date ( "03-04-2020" ) ) ;
312
365
const handleSelectDate = vi . fn ( ) ;
313
366
367
+ const predefinedDatesList = getPredefinedMonthsByNumber ( - 6 ) ;
314
368
const { getByTestId, getByText } = renderCUI (
315
369
< DateRangePicker
316
370
onSelectDateRange = { handleSelectDate }
317
- predefinedDatesCount = { - 6 }
371
+ predefinedDatesList = { predefinedDatesList }
318
372
/>
319
373
) ;
320
374
321
375
await userEvent . click ( getByTestId ( "daterangepicker-input" ) ) ;
322
376
323
- expect ( getByText ( "Mar 2020" ) ) . toBeInTheDocument ( ) ;
377
+ expect ( getByText ( "Mar 01, 2020 - Mar 04, 2020" ) ) . toBeInTheDocument ( ) ;
324
378
expect ( getByText ( "Feb 2020" ) ) . toBeInTheDocument ( ) ;
325
379
expect ( getByText ( "Jan 2020" ) ) . toBeInTheDocument ( ) ;
326
380
expect ( getByText ( "Dec 2019" ) ) . toBeInTheDocument ( ) ;
0 commit comments