Skip to content

Commit 397a7ff

Browse files
fix(calendar-web): improve tests to handle startDateAttribute state and defaultDate propagation
1 parent c64d752 commit 397a7ff

2 files changed

Lines changed: 107 additions & 48 deletions

File tree

packages/pluggableWidgets/calendar-web/src/__tests__/Calendar.spec.tsx

Lines changed: 107 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from "@testing-library/react";
1+
import { render, screen } from "@testing-library/react";
22
import { dynamic, ListValueBuilder } from "@mendix/widget-plugin-test-utils";
33

44
import MxCalendar from "../Calendar";
@@ -10,37 +10,41 @@ jest.mock("react-big-calendar", () => {
1010
const originalModule = jest.requireActual("react-big-calendar");
1111
return {
1212
...originalModule,
13-
Calendar: ({
14-
children,
15-
defaultView,
16-
culture,
17-
resizable,
18-
selectable,
19-
showAllEvents,
20-
min,
21-
max,
22-
events,
23-
step,
24-
timeslots,
25-
...domProps
26-
}: any) => (
27-
<div
28-
data-testid="mock-calendar"
29-
data-default-view={defaultView}
30-
data-culture={culture}
31-
data-resizable={resizable}
32-
data-selectable={selectable}
33-
data-show-all-events={showAllEvents}
34-
data-min={min?.toISOString()}
35-
data-max={max?.toISOString()}
36-
data-events-count={events?.length ?? 0}
37-
data-step={step}
38-
data-timeslots={timeslots}
39-
{...domProps}
40-
>
41-
{children}
42-
</div>
43-
),
13+
Calendar: (mockProps: any) => {
14+
const {
15+
children,
16+
defaultView,
17+
defaultDate,
18+
culture,
19+
resizable,
20+
selectable,
21+
showAllEvents,
22+
min,
23+
max,
24+
events,
25+
step,
26+
timeslots,
27+
...domProps
28+
} = mockProps;
29+
30+
return (
31+
<div
32+
data-testid="mock-calendar"
33+
data-default-view={defaultView}
34+
data-default-date={defaultDate?.toISOString()}
35+
data-culture={culture}
36+
data-resizable={resizable}
37+
data-selectable={selectable}
38+
data-show-all-events={showAllEvents}
39+
data-events-count={events?.length ?? 0}
40+
data-step={step}
41+
data-timeslots={timeslots}
42+
{...domProps}
43+
>
44+
{children}
45+
</div>
46+
);
47+
},
4448
dateFnsLocalizer: () => ({
4549
format: jest.fn(),
4650
parse: jest.fn(),
@@ -58,7 +62,7 @@ jest.mock("react-big-calendar", () => {
5862
});
5963

6064
jest.mock("react-big-calendar/lib/addons/dragAndDrop", () => {
61-
return jest.fn((Component: any) => Component);
65+
return jest.fn(Component => Component);
6266
});
6367

6468
const customViewProps: CalendarContainerProps = {
@@ -97,11 +101,6 @@ const customViewProps: CalendarContainerProps = {
97101
topBarDateFormat: undefined
98102
};
99103

100-
const standardViewProps: CalendarContainerProps = {
101-
...customViewProps,
102-
view: "standard"
103-
};
104-
105104
beforeAll(() => {
106105
jest.useFakeTimers();
107106
jest.setSystemTime(new Date("2025-04-28T12:00:00Z"));
@@ -123,19 +122,78 @@ describe("Calendar", () => {
123122
expect(container.querySelector(".calendar-class")).toBeTruthy();
124123
});
125124

126-
it("does not render custom view button in standard view", () => {
127-
const { container } = render(<MxCalendar {...standardViewProps} />);
128-
expect(container).toBeTruthy();
129-
// Since we're mocking the calendar, we can't test for specific text content
130-
// but we can verify the component renders without errors
131-
});
132-
133125
it("passes step and timeslots to the calendar", () => {
134126
const { getByTestId } = render(<MxCalendar {...customViewProps} />);
135127
const calendar = getByTestId("mock-calendar");
136128
expect(calendar.getAttribute("data-step")).toBe("60");
137129
expect(calendar.getAttribute("data-timeslots")).toBe("2");
138130
});
131+
132+
it("renders loading bar when startDateAttribute is loading", () => {
133+
const props = {
134+
...customViewProps,
135+
startDateAttribute: {
136+
status: "loading"
137+
} as any
138+
};
139+
140+
const { container } = render(<MxCalendar {...props} />);
141+
142+
expect(container.querySelector(".widget-calendar-loading-bar")).toBeTruthy();
143+
expect(container.querySelector("progress.widget-calendar-loading-bar")).toBeTruthy();
144+
expect(screen.queryByTestId("mock-calendar")).toBeFalsy();
145+
});
146+
147+
it("renders calendar when startDateAttribute is available", () => {
148+
const props = {
149+
...customViewProps,
150+
startDateAttribute: {
151+
status: "available",
152+
value: new Date("2025-05-01T00:00:00.000Z")
153+
} as any
154+
};
155+
156+
render(<MxCalendar {...props} />);
157+
158+
expect(screen.getByTestId("mock-calendar")).toBeTruthy();
159+
expect(screen.queryByRole("progressbar")).toBeFalsy();
160+
});
161+
162+
it("renders calendar when startDateAttribute is unavailable", () => {
163+
const props = {
164+
...customViewProps,
165+
startDateAttribute: {
166+
status: "unavailable"
167+
} as any
168+
};
169+
170+
render(<MxCalendar {...props} />);
171+
172+
expect(screen.getByTestId("mock-calendar")).toBeTruthy();
173+
expect(screen.queryByRole("progressbar")).toBeFalsy();
174+
});
175+
176+
it("renders calendar when startDateAttribute is undefined", () => {
177+
render(<MxCalendar {...customViewProps} startDateAttribute={undefined} />);
178+
179+
expect(screen.getByTestId("mock-calendar")).toBeTruthy();
180+
expect(screen.queryByRole("progressbar")).toBeFalsy();
181+
});
182+
183+
it("passes defaultDate from startDateAttribute value", () => {
184+
const defaultDate = new Date("2025-06-10T08:30:00.000Z");
185+
const props = {
186+
...customViewProps,
187+
startDateAttribute: {
188+
status: "available",
189+
value: defaultDate
190+
} as any
191+
};
192+
193+
render(<MxCalendar {...props} />);
194+
195+
expect(screen.getByTestId("mock-calendar").getAttribute("data-default-date")).toBe("2025-06-10T08:30:00.000Z");
196+
});
139197
});
140198

141199
describe("CalendarPropsBuilder validation", () => {
@@ -147,7 +205,10 @@ describe("CalendarPropsBuilder validation", () => {
147205
messages: {}
148206
} as any;
149207

150-
const buildWithStepTimeslots = (step: number, timeslots: number) => {
208+
const buildWithStepTimeslots = (
209+
step: number,
210+
timeslots: number
211+
): ReturnType<typeof CalendarPropsBuilder.prototype.build> => {
151212
const props = { ...customViewProps, step, timeslots };
152213
const builder = new CalendarPropsBuilder(props);
153214
return builder.build(mockLocalizer, "en");

packages/pluggableWidgets/calendar-web/src/__tests__/__snapshots__/Calendar.spec.tsx.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ exports[`Calendar renders correctly with basic props 1`] = `
1010
data-culture="en-US"
1111
data-default-view="day"
1212
data-events-count="0"
13-
data-max="2025-04-28T23:59:59.000Z"
14-
data-min="2025-04-28T00:00:00.000Z"
1513
data-resizable="true"
1614
data-selectable="true"
1715
data-show-all-events="true"

0 commit comments

Comments
 (0)