-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathmultiple_selected_dates.test.tsx
More file actions
137 lines (113 loc) · 3.91 KB
/
multiple_selected_dates.test.tsx
File metadata and controls
137 lines (113 loc) · 3.91 KB
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
import { render } from "@testing-library/react";
import React from "react";
import DatePicker from "../";
import type { DatePickerProps } from "../index";
// see https://github.com/microsoft/TypeScript/issues/31501
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type OmitUnion<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
describe("Multiple Dates Selected", function () {
function getDatePicker(
extraProps: Partial<
Pick<
DatePickerProps,
"shouldCloseOnSelect" | "disabledKeyboardNavigation" | "onSelect"
>
> &
OmitUnion<
DatePickerProps,
| "selectsMultiple"
| "onChange"
| "shouldCloseOnSelect"
| "disabledKeyboardNavigation"
| "onSelect"
| "selectsRange"
> & {
selectsMultiple?: true;
},
) {
return render(
<DatePicker
selectsMultiple
onChange={() => {}}
shouldCloseOnSelect={false}
disabledKeyboardNavigation
onSelect={() => {}}
{...extraProps}
/>,
);
}
it("should handle text format for no selected date", () => {
const { container: datePicker } = getDatePicker({
selectsMultiple: true,
selectedDates: [],
});
const input = datePicker.querySelector("input");
expect(input).not.toBeNull();
expect(input?.value).toBe("");
});
it("should handle text format for one selected date", () => {
const { container: datePicker } = getDatePicker({
selectsMultiple: true,
selectedDates: [new Date("2024/01/01")],
});
const input = datePicker.querySelector("input");
expect(input).not.toBeNull();
expect(input?.value).toBe("01/01/2024");
});
it("should handle text format for two selected dates", () => {
const { container: datePicker } = getDatePicker({
selectsMultiple: true,
selectedDates: [new Date("2024/01/01"), new Date("2024/01/15")],
});
const input = datePicker.querySelector("input");
expect(input).not.toBeNull();
expect(input?.value).toBe("01/01/2024, 01/15/2024");
});
it("should handle text format for more than two selected dates", () => {
const { container: datePicker } = getDatePicker({
selectsMultiple: true,
selectedDates: [
new Date("2024/01/01"),
new Date("2024/01/15"),
new Date("2024/03/15"),
],
});
const input = datePicker.querySelector("input");
expect(input).not.toBeNull();
expect(input?.value).toBe("01/01/2024 (+2)");
});
it("should override default format when formatMultipleDates is provided", () => {
const { container: datePicker } = getDatePicker({
selectsMultiple: true,
selectedDates: [
new Date("2024/01/01"),
new Date("2024/01/15"),
new Date("2024/03/15"),
],
formatMultipleDates: (dates, formatDate) =>
dates.map(formatDate).join(" | "),
});
const input = datePicker.querySelector("input");
expect(input).not.toBeNull();
expect(input?.value).toBe("01/01/2024 | 01/15/2024 | 03/15/2024");
});
it("should pass correct arguments to formatMultipleDates", () => {
const selectedDates = [new Date("2024/01/01"), new Date("2024/01/15")];
const mockFormatter = jest.fn(
(dates: Date[], formatDate: (d: Date) => string) =>
dates.map(formatDate).join(", "),
);
getDatePicker({
selectsMultiple: true,
selectedDates,
formatMultipleDates: mockFormatter,
});
expect(mockFormatter).toHaveBeenCalledTimes(1);
const [receivedDates, receivedFormatDate] = mockFormatter.mock.calls[0]!;
expect(receivedDates).toHaveLength(2);
expect(receivedDates[0]?.getTime()).toBe(selectedDates[0]?.getTime());
expect(receivedDates[1]?.getTime()).toBe(selectedDates[1]?.getTime());
expect(typeof receivedFormatDate).toBe("function");
expect(receivedFormatDate(new Date("2024/01/01"))).toBe("01/01/2024");
});
});