|
| 1 | +import type React from "react"; |
| 2 | +import Month from "../month"; |
| 3 | +import { KeyType, newDate } from "../date_utils"; |
| 4 | + |
| 5 | +type MonthComponentProps = React.ComponentProps<typeof Month>; |
| 6 | + |
| 7 | +const buildProps = ( |
| 8 | + override: Partial<MonthComponentProps> = {}, |
| 9 | +): MonthComponentProps => |
| 10 | + ({ |
| 11 | + day: newDate("2024-01-01"), |
| 12 | + onDayClick: jest.fn(), |
| 13 | + onDayMouseEnter: jest.fn(), |
| 14 | + onMouseLeave: jest.fn(), |
| 15 | + setPreSelection: jest.fn(), |
| 16 | + preSelection: newDate("2024-01-01"), |
| 17 | + showFourColumnMonthYearPicker: false, |
| 18 | + showTwoColumnMonthYearPicker: false, |
| 19 | + disabledKeyboardNavigation: false, |
| 20 | + ...override, |
| 21 | + }) as MonthComponentProps; |
| 22 | + |
| 23 | +describe("Month logic helpers", () => { |
| 24 | + it("short-circuits keyboard navigation when there is no preSelection", () => { |
| 25 | + const props = buildProps({ preSelection: undefined }); |
| 26 | + const instance = new Month(props); |
| 27 | + const getVerticalOffsetSpy = jest.spyOn(instance, "getVerticalOffset"); |
| 28 | + |
| 29 | + instance.handleKeyboardNavigation( |
| 30 | + { |
| 31 | + preventDefault: jest.fn(), |
| 32 | + } as unknown as React.KeyboardEvent<HTMLDivElement>, |
| 33 | + KeyType.ArrowRight, |
| 34 | + 1, |
| 35 | + ); |
| 36 | + |
| 37 | + expect(getVerticalOffsetSpy).not.toHaveBeenCalled(); |
| 38 | + expect(props.setPreSelection).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("prevents quarter navigation when the destination date is disabled", () => { |
| 42 | + const props = buildProps(); |
| 43 | + const instance = new Month(props); |
| 44 | + jest.spyOn(instance, "isDisabled").mockReturnValue(true); |
| 45 | + jest.spyOn(instance, "isExcluded").mockReturnValue(false); |
| 46 | + |
| 47 | + instance.handleQuarterNavigation(2, newDate("2024-04-01")); |
| 48 | + |
| 49 | + expect(props.setPreSelection).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("does not handle quarter arrow keys without a preSelection value", () => { |
| 53 | + const props = buildProps({ preSelection: undefined }); |
| 54 | + const instance = new Month(props); |
| 55 | + const navigationSpy = jest.spyOn(instance, "handleQuarterNavigation"); |
| 56 | + |
| 57 | + instance.onQuarterKeyDown( |
| 58 | + { |
| 59 | + key: KeyType.ArrowRight, |
| 60 | + preventDefault: jest.fn(), |
| 61 | + } as unknown as React.KeyboardEvent<HTMLDivElement>, |
| 62 | + 2, |
| 63 | + ); |
| 64 | + |
| 65 | + instance.onQuarterKeyDown( |
| 66 | + { |
| 67 | + key: KeyType.ArrowLeft, |
| 68 | + preventDefault: jest.fn(), |
| 69 | + } as unknown as React.KeyboardEvent<HTMLDivElement>, |
| 70 | + 2, |
| 71 | + ); |
| 72 | + |
| 73 | + expect(navigationSpy).not.toHaveBeenCalled(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments