Skip to content

Commit 55f49a0

Browse files
committed
test: cover early return cases
1 parent f5c95b6 commit 55f49a0

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/test/datepicker_test.test.tsx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3231,6 +3231,7 @@ describe("DatePicker", () => {
32313231
startDate={undefined}
32323232
endDate={undefined}
32333233
onChange={onChangeSpy}
3234+
excludeDates={[newDate("2024-01-01")]}
32343235
ref={(node) => {
32353236
instance = node;
32363237
}}
@@ -3245,7 +3246,21 @@ describe("DatePicker", () => {
32453246
},
32463247
});
32473248

3248-
expect(onChangeSpy).toHaveBeenCalled();
3249+
// cover `if (startDateNew && isDayDisabled(startDateNew, this.props))` block
3250+
fireEvent.change(input, {
3251+
target: {
3252+
value: "01/01/2024-05/06/2024",
3253+
},
3254+
});
3255+
3256+
// cover `if (endDateNew && isDayDisabled(endDateNew, this.props))` block
3257+
fireEvent.change(input, {
3258+
target: {
3259+
value: "03/04/2023-01/01/2024",
3260+
},
3261+
});
3262+
3263+
expect(onChangeSpy).toHaveBeenCalledTimes(1);
32493264
expect(Array.isArray(onChangeSpy.mock.calls[0][0])).toBe(true);
32503265
expect(onChangeSpy.mock.calls[0][0][0]).toBeTruthy();
32513266
expect(onChangeSpy.mock.calls[0][0][1]).toBeTruthy();
@@ -3256,6 +3271,34 @@ describe("DatePicker", () => {
32563271
"05/06/2024",
32573272
);
32583273
});
3274+
3275+
it("should not fire onChange a second time if user edits text box without the parsing result changing", () => {
3276+
const onChangeSpy = jest.fn();
3277+
let instance: DatePicker | null = null;
3278+
const { container } = render(
3279+
<DatePicker
3280+
selectsRange
3281+
startDate={newDate("2024-03-04")}
3282+
endDate={newDate("2024-05-06")}
3283+
onChange={onChangeSpy}
3284+
ref={(node) => {
3285+
instance = node;
3286+
}}
3287+
/>,
3288+
);
3289+
3290+
expect(instance).toBeTruthy();
3291+
const input = safeQuerySelector<HTMLInputElement>(container, "input");
3292+
3293+
// cover `if (!startChanged && !endChanged)` block
3294+
fireEvent.change(input, {
3295+
target: {
3296+
value: "03/04/2024-05/06/2024",
3297+
},
3298+
});
3299+
3300+
expect(onChangeSpy).not.toHaveBeenCalled();
3301+
});
32593302
});
32603303

32613304
describe("duplicate dates when multiple months", () => {

0 commit comments

Comments
 (0)