|
1 | 1 | import { render, act, waitFor, fireEvent } from "@testing-library/react"; |
2 | 2 | import { userEvent } from "@testing-library/user-event"; |
3 | 3 | import { enUS, enGB } from "date-fns/locale"; |
4 | | -import React from "react"; |
| 4 | +import React, { useState } from "react"; |
5 | 5 |
|
6 | 6 | import { |
7 | 7 | KeyType, |
@@ -4243,4 +4243,44 @@ describe("DatePicker", () => { |
4243 | 4243 | }); |
4244 | 4244 | }); |
4245 | 4245 | }); |
| 4246 | + |
| 4247 | + describe("input reset", () => { |
| 4248 | + const renderDatePickerInput = () => { |
| 4249 | + const WrapperComponent = () => { |
| 4250 | + const [date, setDate] = useState<Date | null>(new Date()); |
| 4251 | + return <DatePicker open={false} selected={date} onChange={setDate} />; |
| 4252 | + }; |
| 4253 | + |
| 4254 | + return render(<WrapperComponent />); |
| 4255 | + }; |
| 4256 | + |
| 4257 | + it("should reset the date input element with the previously entered value element on blur even when the calendar open is false", () => { |
| 4258 | + const { container } = renderDatePickerInput(); |
| 4259 | + const input = safeQuerySelector(container, "input") as HTMLInputElement; |
| 4260 | + |
| 4261 | + if (!input) { |
| 4262 | + throw new Error("Input element not found"); |
| 4263 | + } |
| 4264 | + |
| 4265 | + fireEvent.click(input); |
| 4266 | + const DATE_VALUE = "02/22/2025"; |
| 4267 | + fireEvent.change(input, { |
| 4268 | + target: { |
| 4269 | + value: DATE_VALUE, |
| 4270 | + }, |
| 4271 | + }); |
| 4272 | + fireEvent.blur(input); |
| 4273 | + expect(input.value).toBe(DATE_VALUE); |
| 4274 | + |
| 4275 | + fireEvent.click(input); |
| 4276 | + const INVALID_DATE_VALUE = "2025-02-45"; |
| 4277 | + fireEvent.change(input, { |
| 4278 | + target: { |
| 4279 | + value: INVALID_DATE_VALUE, |
| 4280 | + }, |
| 4281 | + }); |
| 4282 | + fireEvent.blur(input); |
| 4283 | + expect(input.value).toBe(DATE_VALUE); |
| 4284 | + }); |
| 4285 | + }); |
4246 | 4286 | }); |
0 commit comments