UNPKG

3.57 kBTypeScriptView Raw
1/* eslint-disable @typescript-eslint/no-explicit-any */
2import { DateRange, DayPickerProps } from "react-day-picker/types";
3
4import { act, renderHook } from "@/test/render";
5
6import { dateLib } from "../lib";
7
8import { useRange } from "./useRange";
9
10describe("useRange", () => {
11 test("initialize with initiallySelected date range", () => {
12 const initiallySelected = {
13 from: new Date(2023, 6, 1),
14 to: new Date(2023, 6, 5)
15 };
16 const { result } = renderHook(() =>
17 useRange(
18 { mode: "range", selected: initiallySelected, required: false },
19 dateLib
20 )
21 );
22
23 expect(result.current.selected).toEqual(initiallySelected);
24 });
25
26 test("update the selected range when the initially selected value changes", () => {
27 const initiallySelected: DateRange = {
28 from: new Date(2023, 6, 1),
29 to: new Date(2023, 6, 5)
30 };
31 const { result, rerender } = renderHook(
32 (props) => useRange(props, dateLib),
33 {
34 initialProps: {
35 mode: "range",
36 selected: initiallySelected,
37 required: false
38 } as DayPickerProps
39 }
40 );
41
42 rerender({
43 mode: "range",
44 selected: undefined,
45 required: false
46 });
47
48 expect(result.current.selected).toEqual(undefined);
49 });
50
51 test("update the selected range on select", () => {
52 const initiallySelected = {
53 from: new Date(2023, 6, 1),
54 to: new Date(2023, 6, 5)
55 };
56 const { result } = renderHook(() =>
57 useRange(
58 { mode: "range", selected: initiallySelected, required: false },
59 dateLib
60 )
61 );
62
63 act(() => {
64 result.current.select?.(new Date(2023, 6, 10), {}, {} as any);
65 });
66
67 expect(result.current.selected).toEqual({
68 from: new Date(2023, 6, 1),
69 to: new Date(2023, 6, 10)
70 });
71 });
72
73 test("reset range if new range exceeds max days", () => {
74 const { result } = renderHook(() =>
75 useRange(
76 {
77 mode: "range",
78 selected: undefined,
79 required: false,
80 max: 5
81 },
82 dateLib
83 )
84 );
85
86 act(() => {
87 result.current.select?.(new Date(2023, 6, 1), {}, {} as any);
88 result.current.select?.(new Date(2023, 6, 10), {}, {} as any);
89 });
90
91 expect(result.current.selected).toEqual({
92 from: new Date(2023, 6, 10),
93 to: new Date(2023, 6, 10)
94 });
95 });
96
97 test("reset range if new range is less than min days", () => {
98 const { result } = renderHook(() =>
99 useRange(
100 { mode: "range", selected: undefined, required: false, min: 5 },
101 dateLib
102 )
103 );
104
105 act(() => {
106 result.current.select?.(new Date(2023, 6, 1), {}, {} as any);
107 result.current.select?.(new Date(2023, 6, 3), {}, {} as any);
108 });
109
110 expect(result.current.selected).toEqual({
111 from: new Date(2023, 6, 3),
112 to: undefined
113 });
114 });
115
116 test("exclude disabled dates when selecting range", () => {
117 const disabled = [{ from: new Date(2023, 6, 5), to: new Date(2023, 6, 7) }];
118 const { result } = renderHook(() =>
119 useRange(
120 {
121 mode: "range",
122 selected: undefined,
123 required: false,
124 excludeDisabled: true,
125 disabled
126 },
127 dateLib
128 )
129 );
130
131 act(() => {
132 result.current.select?.(new Date(2023, 6, 1), {}, {} as any);
133 result.current.select?.(new Date(2023, 6, 10), {}, {} as any);
134 });
135
136 expect(result.current.selected).toEqual({
137 from: new Date(2023, 6, 10),
138 to: new Date(2023, 6, 10)
139 });
140 });
141});