forked from GovAlta/ui-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate-picker.spec.ts
More file actions
220 lines (172 loc) · 6.47 KB
/
date-picker.spec.ts
File metadata and controls
220 lines (172 loc) · 6.47 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
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
import DatePicker from "./DatePicker.svelte";
import {
createEvent,
fireEvent,
render,
waitFor,
} from "@testing-library/svelte";
import { addDays, format, addMonths, addYears } from "date-fns";
import { it, expect, vi } from "vitest";
it("it renders", async () => {
const { container } = render(DatePicker);
const popover = container.querySelector("goa-popover");
const input = container.querySelector("goa-input");
const calendar = container.querySelector("goa-calendar");
expect(popover).toBeTruthy();
expect(input).toBeTruthy();
expect(calendar).toBeTruthy();
});
it("renders with props", async () => {
const value = addDays(new Date(), -10);
const { container } = render(DatePicker, { value });
const input = container.querySelector("goa-input");
expect(input?.getAttribute("value")).toBe(format(value, "PPP"));
});
it("shows an error state", async () => {
const value = new Date();
const error = "true";
const { container } = render(DatePicker, { value, error });
const input = container.querySelector("goa-input");
expect(input?.getAttribute("error")).toBe("true");
});
it("dispatches a value on date selection", async () => {
const { container } = render(DatePicker);
const popover = container.querySelector("goa-popover");
const input = container.querySelector("goa-input");
const selectedDate = new Date();
const handler = vi.fn();
expect(popover).toBeTruthy();
expect(input).toBeTruthy();
popover?.addEventListener("_change", (e: Event) => {
const ce = e as CustomEvent;
handler();
expect(ce.detail.value).toBe(selectedDate);
});
input?.dispatchEvent(
new CustomEvent("_change", {
composed: true,
bubbles: true,
detail: { type: "date", value: selectedDate },
}),
);
await waitFor(() => {
expect(handler).toBeCalled();
});
});
describe("DatePicker Keyboard Navigation", () => {
const setupTest = () => {
const inputDate = new Date();
const currentDate = new Date(
inputDate.getFullYear(),
inputDate.getMonth(),
inputDate.getDate(),
);
const { container } = render(DatePicker, { value: inputDate });
const input = container.querySelector("goa-input");
expect(input).toBeTruthy();
const handler = vi.fn();
container?.addEventListener("_change", (e: Event) => {
const ce = e as CustomEvent;
handler(ce.detail.value);
});
return { input, currentDate, handler };
};
it("navigates to the previous day when left arrow is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const arrowLeftEvent = createEvent.keyDown(input!, { key: "ArrowLeft" });
await fireEvent(input!, arrowLeftEvent);
const expectedDate = addDays(currentDate, -1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the next day when right arrow is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const arrowRightEvent = createEvent.keyDown(input!, { key: "ArrowRight" });
await fireEvent(input!, arrowRightEvent);
const expectedDate = addDays(currentDate, 1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the previous week when up arrow is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const arrowUpEvent = createEvent.keyDown(input!, { key: "ArrowUp" });
await fireEvent(input!, arrowUpEvent);
const expectedDate = addDays(currentDate, -7);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the next week when down arrow is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const arrowDownEvent = createEvent.keyDown(input!, { key: "ArrowDown" });
await fireEvent(input!, arrowDownEvent);
const expectedDate = addDays(currentDate, 7);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the previous month when PageUp is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const pageUpEvent = createEvent.keyDown(input!, { key: "PageUp" });
await fireEvent(input!, pageUpEvent);
const expectedDate = addMonths(currentDate, -1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the next month when PageDown is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const pageDownEvent = createEvent.keyDown(input!, { key: "PageDown" });
await fireEvent(input!, pageDownEvent);
const expectedDate = addMonths(currentDate, 1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the previous year when Shift+PageUp is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const shiftPageUpEvent = createEvent.keyDown(input!, {
key: "PageUp",
shiftKey: true,
});
await fireEvent(input!, shiftPageUpEvent);
const expectedDate = addYears(currentDate, -1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
it("navigates to the next year when Shift+PageDown is pressed", async () => {
const { input, currentDate, handler } = setupTest();
const shiftPageDownEvent = createEvent.keyDown(input!, {
key: "PageDown",
shiftKey: true,
});
await fireEvent(input!, shiftPageDownEvent);
const expectedDate = addYears(currentDate, 1);
await waitFor(() => {
expect(handler).toHaveBeenCalledWith(expectedDate);
});
});
});
it("renders with disabled prop", async () => {
const { container } = render(DatePicker, { disabled: "true" });
const input = container.querySelector("goa-input");
const popover = container.querySelector("goa-popover");
expect(input?.getAttribute("disabled")).toBe("true");
expect(popover?.getAttribute("disabled")).toBe("true");
});
it("prevents interaction when disabled", async () => {
const { container } = render(DatePicker, { disabled: "true" });
const input = container.querySelector("goa-input");
expect(input).toBeTruthy();
if (!input) return;
const clickEvent = createEvent.click(input);
const handler = vi.fn();
container?.addEventListener("_change", handler);
await fireEvent(input, clickEvent);
await waitFor(() => {
expect(handler).not.toBeCalled();
});
});