Files
calendar/packages/features/bookings/Booker/utils/isPrefetchNextMonthEnabled.test.ts
T
Rajiv SahalGitHubrajiv@cal.com <sahalrajiv6900@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>Alex van Andel
1dc20e3aec refactor: usePrefetch hook (#24123)
* init: refactor usePrefetch hook to make it readable

* fix: move over logic to determine prefetch next month into its own function

* update parameters

* fix: better naming

* fix: type check

* test: add comprehensive test coverage for usePrefetch utility functions

- Add tests for areDifferentValidMonths: validates month comparison logic with edge cases
- Add tests for isLastWeekOfMonth: covers week boundaries and date edge cases
- Add tests for isMonthViewPrefetchEnabled: tests time-based prefetch logic with mocked time
- Add tests for getPrefetchMonthCount: validates conditional month count logic for different layouts
- Add tests for isPrefetchNextMonthEnabled: integration tests for orchestration function

All tests use vitest framework with 51 test cases covering key scenarios and edge cases.
Tests follow existing patterns from Cal.com codebase and use integration approach.

Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com>

* fix: implement coderabbit feedback

* fix: tests

* fix: use isoWeek plugin from day to maintain consistency

* fix: type errors

* fix: dont use isoWeek from dayjs

* revert isoWeek import for dayjs

* add helper function

* fix: bring back the orginal logic we had in usePrefetch but in a simplified way

* remove unused logic

* test: update tests for usePrefetch refactoring

- Add tests for new isMonthChange helper function
- Update isPrefetchNextMonthEnabled tests to match new signature
- Tests now use pre-calculated month values instead of date strings

All 39 tests passing locally

Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
2025-10-13 22:51:50 +09:00

209 lines
6.6 KiB
TypeScript

import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import dayjs from "@calcom/dayjs";
import { BookerLayouts } from "@calcom/prisma/zod-utils";
import { isPrefetchNextMonthEnabled } from "./isPrefetchNextMonthEnabled";
describe("isPrefetchNextMonthEnabled", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
describe("WEEK_VIEW layout", () => {
it("should return true when extraDays is provided and months change", () => {
const date = "2024-01-28";
const dateMonth = dayjs(date).month();
const extraDays = 7;
const monthAfterAddingExtraDays = dayjs(date).add(extraDays, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.WEEK_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01",
extraDays
);
expect(result).toBe(true);
});
it("should return false when extraDays is provided but months don't change", () => {
const date = "2024-01-15";
const dateMonth = dayjs(date).month();
const extraDays = 7;
const monthAfterAddingExtraDays = dayjs(date).add(extraDays, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.WEEK_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01",
extraDays
);
expect(result).toBe(false);
});
it("should return false when extraDays is not provided", () => {
const date = "2024-01-28";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.WEEK_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01",
undefined
);
expect(result).toBe(false);
});
it("should return false when extraDays is 0", () => {
const date = "2024-01-28";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.WEEK_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01",
0
);
expect(result).toBe(false);
});
});
describe("COLUMN_VIEW layout", () => {
it("should return true when months change after adding columnView extra days", () => {
const date = "2024-01-30";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.COLUMN_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(true);
});
it("should return false when months don't change after adding columnView extra days", () => {
const date = "2024-01-10";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.COLUMN_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(false);
});
});
describe("MONTH_VIEW layout", () => {
it("should return true when conditions for month view prefetch are met", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
const date = "invalid-date";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.MONTH_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(true);
});
it("should return false when current time is before 2 weeks threshold", () => {
vi.setSystemTime(new Date("2024-01-10T12:00:00Z"));
const date = "invalid-date";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
BookerLayouts.MONTH_VIEW,
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(false);
});
});
describe("mobile layout", () => {
it("should return true when conditions for month view prefetch are met", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
const date = "invalid-date";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
"mobile",
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(true);
});
});
describe("unknown layout", () => {
it("should return false for unrecognized layout", () => {
const date = "2024-01-15";
const dateMonth = dayjs(date).month();
const monthAfterAddingExtraDays = dayjs(date).add(7, "day").month();
const monthAfterAddingExtraDaysColumnView = dayjs(date).add(5, "day").month();
const result = isPrefetchNextMonthEnabled(
"unknown-layout",
date,
dateMonth,
monthAfterAddingExtraDays,
monthAfterAddingExtraDaysColumnView,
"2024-01-01"
);
expect(result).toBe(false);
});
});
});