Files
cal.diy/packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts
T
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

95 lines
3.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { BookerLayouts } from "@calcom/prisma/zod-utils";
import { getPrefetchMonthCount } from "./getPrefetchMonthCount";
describe("getPrefetchMonthCount", () => {
describe("COLUMN_VIEW layout", () => {
it("should return 2 when months are different", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 1, 2);
expect(result).toBe(2);
});
it("should return undefined when months are the same", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, 5);
expect(result).toBe(undefined);
});
it("should return 2 regardless of bookerState when months are different", () => {
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4)).toBe(2);
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "booking", 7, 8)).toBe(2);
});
});
describe("WEEK_VIEW layout", () => {
it("should return undefined when state is selecting_time", () => {
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_time", 1, 2);
expect(result).toBe(undefined);
});
it("should return undefined when state is not selecting_time", () => {
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_date", 1, 2);
expect(result).toBe(undefined);
});
it("should return undefined even with different months", () => {
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "booking", 0, 11);
expect(result).toBe(undefined);
});
});
describe("MONTH_VIEW layout with selecting_time state", () => {
it("should return 2 when months are different", () => {
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4);
expect(result).toBe(2);
});
it("should return undefined when months are the same", () => {
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 6, 6);
expect(result).toBe(undefined);
});
});
describe("MONTH_VIEW layout with other states", () => {
it("should return undefined when state is selecting_date", () => {
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_date", 1, 2);
expect(result).toBe(undefined);
});
it("should return undefined when state is booking", () => {
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "booking", 1, 2);
expect(result).toBe(undefined);
});
});
describe("invalid month inputs", () => {
it("should return undefined when first month is NaN", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", NaN, 5);
expect(result).toBe(undefined);
});
it("should return undefined when second month is NaN", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, NaN);
expect(result).toBe(undefined);
});
it("should return undefined when both months are invalid", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", Infinity, -Infinity);
expect(result).toBe(undefined);
});
});
describe("edge cases", () => {
it("should handle month transitions (11 to 0)", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 11, 0);
expect(result).toBe(2);
});
it("should handle negative month numbers if they are different", () => {
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", -1, 5);
expect(result).toBe(2);
});
});
});