|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import { Categories_Event } from "@core/types/event.types"; |
| 3 | +import { draftSlice } from "@web/ducks/events/slices/draft.slice"; |
| 4 | +import { Activity_DraftEvent } from "@web/ducks/events/slices/draft.slice.types"; |
| 5 | +import { assembleDefaultEvent } from "../event.util"; |
| 6 | +import { createSomedayDraft } from "./someday.draft.util"; |
| 7 | + |
| 8 | +// Mock assembleDefaultEvent since it makes external calls |
| 9 | +jest.mock("../event.util", () => ({ |
| 10 | + assembleDefaultEvent: jest |
| 11 | + .fn() |
| 12 | + .mockImplementation(async (category, startDate, endDate) => ({ |
| 13 | + _id: "mock-id", |
| 14 | + user: "mock-user", |
| 15 | + title: "", |
| 16 | + startDate, |
| 17 | + endDate, |
| 18 | + isAllDay: false, |
| 19 | + isSomeday: true, |
| 20 | + })), |
| 21 | +})); |
| 22 | + |
| 23 | +describe("createSomedayDraft", () => { |
| 24 | + const mockDispatch = jest.fn(); |
| 25 | + const mockActivity: Activity_DraftEvent = "sidebarClick"; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should set correct dates for week category", async () => { |
| 32 | + const startOfView = dayjs("2024-03-10"); // A Sunday |
| 33 | + const endOfView = startOfView.add(6, "days"); // Saturday |
| 34 | + |
| 35 | + await createSomedayDraft( |
| 36 | + Categories_Event.SOMEDAY_WEEK, |
| 37 | + startOfView, |
| 38 | + endOfView, |
| 39 | + mockActivity, |
| 40 | + mockDispatch, |
| 41 | + ); |
| 42 | + |
| 43 | + const expectedStart = "2024-03-10"; |
| 44 | + const expectedEnd = "2024-03-16"; |
| 45 | + |
| 46 | + expect(assembleDefaultEvent).toHaveBeenCalledWith( |
| 47 | + Categories_Event.SOMEDAY_WEEK, |
| 48 | + expectedStart, |
| 49 | + expectedEnd, |
| 50 | + ); |
| 51 | + |
| 52 | + expect(mockDispatch).toHaveBeenCalledWith( |
| 53 | + draftSlice.actions.start({ |
| 54 | + activity: mockActivity, |
| 55 | + eventType: Categories_Event.SOMEDAY_WEEK, |
| 56 | + event: { |
| 57 | + _id: "mock-id", |
| 58 | + user: "mock-user", |
| 59 | + title: "", |
| 60 | + startDate: expectedStart, |
| 61 | + endDate: expectedEnd, |
| 62 | + isAllDay: false, |
| 63 | + isSomeday: true, |
| 64 | + }, |
| 65 | + }), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should set correct dates for month category", async () => { |
| 70 | + const startOfView = dayjs("2024-02-29"); // Leap year February |
| 71 | + const endOfView = startOfView.add(6, "days"); // Crosses into March but should be ignored |
| 72 | + |
| 73 | + await createSomedayDraft( |
| 74 | + Categories_Event.SOMEDAY_MONTH, |
| 75 | + startOfView, |
| 76 | + endOfView, |
| 77 | + mockActivity, |
| 78 | + mockDispatch, |
| 79 | + ); |
| 80 | + |
| 81 | + const expectedStart = "2024-02-01"; |
| 82 | + const expectedEnd = "2024-02-29"; |
| 83 | + |
| 84 | + expect(assembleDefaultEvent).toHaveBeenCalledWith( |
| 85 | + Categories_Event.SOMEDAY_MONTH, |
| 86 | + expectedStart, |
| 87 | + expectedEnd, |
| 88 | + ); |
| 89 | + |
| 90 | + expect(mockDispatch).toHaveBeenCalledWith( |
| 91 | + draftSlice.actions.start({ |
| 92 | + activity: mockActivity, |
| 93 | + eventType: Categories_Event.SOMEDAY_MONTH, |
| 94 | + event: { |
| 95 | + _id: "mock-id", |
| 96 | + user: "mock-user", |
| 97 | + title: "", |
| 98 | + startDate: expectedStart, |
| 99 | + endDate: expectedEnd, |
| 100 | + isAllDay: false, |
| 101 | + isSomeday: true, |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + }); |
| 106 | +}); |
0 commit comments