Files
calendar/packages/features/bookings/Booker/utils/isMonthViewPrefetchEnabled.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

66 lines
2.2 KiB
TypeScript

import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { isMonthViewPrefetchEnabled } from "./isMonthViewPrefetchEnabled";
describe("isMonthViewPrefetchEnabled", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
describe("when current time is after 2 weeks into the month", () => {
it("should return true when date is invalid and same month", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
expect(isMonthViewPrefetchEnabled("invalid-date", "2024-01-01")).toBe(true);
});
it("should return true when date is valid but same month", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
expect(isMonthViewPrefetchEnabled("2024-02-15", "2024-01-01")).toBe(true);
});
it("should return false when date is valid and different month", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
expect(isMonthViewPrefetchEnabled("2024-02-15", "2024-02-01")).toBe(false);
});
});
describe("when current time is before 2 weeks threshold", () => {
it("should return false regardless of date validity", () => {
vi.setSystemTime(new Date("2024-01-10T12:00:00Z"));
expect(isMonthViewPrefetchEnabled("invalid-date", "2024-01-01")).toBe(false);
expect(isMonthViewPrefetchEnabled("2024-02-15", "2024-01-01")).toBe(false);
});
});
describe("edge cases", () => {
it("should handle null month parameter", () => {
vi.setSystemTime(new Date("2024-01-20T12:00:00Z"));
const result = isMonthViewPrefetchEnabled("2024-02-15", null);
expect(result).toBe(false);
});
it("should handle exactly at 2 weeks threshold", () => {
vi.setSystemTime(new Date("2024-01-15T00:00:00Z"));
const result = isMonthViewPrefetchEnabled("invalid-date", "2024-01-01");
expect(result).toBe(false);
});
it("should work with different months throughout the year", () => {
vi.setSystemTime(new Date("2024-06-20T12:00:00Z"));
expect(isMonthViewPrefetchEnabled("invalid-date", "2024-06-01")).toBe(true);
expect(isMonthViewPrefetchEnabled("2024-07-15", "2024-06-01")).toBe(true);
});
});
});