Skip to content

Commit b4d777d

Browse files
🐛 Fix: Someday event in wrong location after creating with 'm' shortcut (#572)
* fix(web): Compute start-end date correctly for someday month * feat(web): Add unit tests for `createSomedayDraft`
1 parent 330236c commit b4d777d

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
});

packages/web/src/common/utils/draft/someday.draft.util.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ export const createSomedayDraft = async (
1313
activity: Activity_DraftEvent,
1414
dispatch: Dispatch,
1515
) => {
16-
const startDate = startOfView.format(YEAR_MONTH_DAY_FORMAT);
17-
const endDate = endOfView.format(YEAR_MONTH_DAY_FORMAT);
16+
let startDate: string;
17+
let endDate: string;
18+
19+
if (category === Categories_Event.SOMEDAY_WEEK) {
20+
startDate = startOfView.format(YEAR_MONTH_DAY_FORMAT);
21+
endDate = endOfView.format(YEAR_MONTH_DAY_FORMAT);
22+
} else {
23+
// Someday month
24+
startDate = startOfView.startOf("month").format(YEAR_MONTH_DAY_FORMAT);
25+
// `endDate` is the last day of the month, hence why we need to use `startOfView`, because
26+
// `endOfView` could be in the next month relative to `startOfView`
27+
endDate = startOfView.endOf("month").format(YEAR_MONTH_DAY_FORMAT);
28+
}
1829

1930
const event = await assembleDefaultEvent(category, startDate, endDate);
2031

0 commit comments

Comments
 (0)