Files
calendar/packages/features/insights/server/__tests__/events.test.ts
T
c28eb90928 chore: Upgrade prisma to 6.7.0 (#21264)
* chore: Upgrade prisma to 6.7.0

* Build fixes

* type fixes

Signed-off-by: Omar López <zomars@me.com>

* Update schema.prisma

* Patching

* Revert "Update schema.prisma"

This reverts commit 47d8618bf89ef4d007b30084df766f17281e21a1.

* Revert "Patching"

This reverts commit a1d2e3040e71690a44d4324db95d73b4d68c6adb.

* Revert schema changes

Signed-off-by: Omar López <zomars@me.com>

* WIP

Signed-off-by: Omar López <zomars@me.com>

* Update getPublicEvent.ts

* Update imports

Signed-off-by: Omar López <zomars@me.com>

* Update gitignore

Signed-off-by: Omar López <zomars@me.com>

* update remaining imports

Signed-off-by: Omar López <zomars@me.com>

* Delete .cursor/config.json

* Discard changes to packages/features/eventtypes/lib/getPublicEvent.ts

* Update _get.ts

* Update user.ts

* Update .gitignore

* update

* Update WorkflowStepContainer.tsx

* Update next-auth-custom-adapter.ts

* Update getPublicEvent.ts

* Update workflow.ts

* Update next-auth-custom-adapter.ts

* Update next-auth-options.ts

* Update bookingScenario.ts

* fix missing imports

* upgrades prismock

Signed-off-by: Omar López <zomars@me.com>

* patches prismock

Signed-off-by: Omar López <zomars@me.com>

* Update reschedule.test.ts

* Update prisma.ts

* patch prismock

Signed-off-by: Omar López <zomars@me.com>

* fix enums imports

Signed-off-by: Omar López <zomars@me.com>

* Revert "Update prisma.ts"

This reverts commit 64edcf8db54171ff4456c209d563b5d431d99619.

* Revert "patch prismock"

This reverts commit e95819113dc9d88e7130947aa120cd42710977c8.

* fix patch

* Fix test that overrun the boundary, it shouldn't test too much

* Move prisma import to changeSMSLockState

* Bring back broken test without illegal imports

* Merge with main and fix filter hosts by same round robin host

* Fixed buildDryRunBooking fn tests

* Fix and move ooo create or update handler test

* Fix packages/features/eventtypes/lib/isCurrentlyAvailable.test.ts

* Fix packages/trpc/server/routers/viewer/organizations/listMembers.handler.test.ts

* Mock @calcom/prisma

* Fix: verify-email.test.ts

* fix: Moved WebhookService test and fixed default import mock

* Fix: Added missing prisma mock, handleNewBooking uses that of course

* We're not testing createContext here

* fix: Prisma mock fix for listMembers.test.ts

* More fixes to broken testcases

* Forgot to remove borked test

* Prevent the need to mock a lot of dependencies by moving out buildBaseWhereCondition to its own file

* Temporarily skip getCalendarEvents, needs a rewrite

* Fix: turns out you can access protected in testcases

* fix further mocks

* Added packages/features/insights/server/buildBaseWhereCondition.ts, types

* Always great to have a mock and then not use it

* And one less again.

* fix: confirm.handler.test, didn't mock prisma

* fix: Address minor nit by @eunjae & fix ImpersonationProvider test

* Updated isPrismaAvailableCheck that doesn't crash on import

* fix: Get Prisma directly from the client, it usually involves the Validator and does not need 'local' inclusion

* Add zod-prisma-types without the generator enabled (commented out)

* Uncomment and see what happens

* Change method of import as imports did not work in Input Schemas

* Remove custom 'zod' booking model, it does not belong with Prisma

* Fix all other global Model imports

* Rewrite most schema includes AND remove barrel file

* Add bookingCreateBodySchema to features/bookings

* Flurry of type fixes for compatibility with new zod gen

* Refactor out the custom prisma type createEventTypeInput

* Work around nullable eventTypeLocations

* HandlePayment type fix

* More fixes, final fix remaining is CompleteEventType

* Should fix a bunch more booking related type errors

* Missed one

* Some props missing from BookingCreateBodySchema

* Fix location type in handleChildrenEventTypes

* Little bit hacky imo but it works

* Final type error \o/

* Forgot to include Prisma

* Do not include zod-utils in booker/types

* Oops, was already including Booker/types

* Fix membership type, also disallow updating createdAt/updatedAt, make part of patch/post

* Fix api v1 type errors

* Fix EventTypeDescription typings

* Remove getParserWithGeneric, use userBodySchema with UserSchema

* use centralized timeZoneSchema

* Implement feedback by @zomars

* Couple of WIP pushes

* Fix tests

* Type fixes in `handleChildrenEventTypes` test

* Try and parse metadata before use

* Change zod-prisma-types configuration for optimal performance

* Fix prisma validator error in `prisma/selects/credential`

* Disable seperate relations model, hits a bug

* Import absolute - this makes rollup work in @platform/libraries

* Attempt at removing resolutions override

* Refactor using `Prisma.validator` to `satisfies`

* Build atoms using @calcom/prisma/client

* Build atoms using @calcom/prisma/client

* fixes

* Update eventTypeSelect.ts

* Adjust `eventTypeMetaDataSchemaWithUntypedApps` from `unknown` to `record(any)`

* `EventTypeDescription` rely on `descriptionAsSafeHTML` instead of `description`

* Add `seatsPerTimeSlot` to event type public select

* Fix typing in `users-public-view` getServerSide props

* Add missing `schedulingType` to prop

* chore: bump platform libraries

* Function return type is illegal, not sure how this passed eslint (#21567)

* Merged with main

* Update updateTokenObject.ts

* Update handleResponse.ts

* Update index.ts

* Update handleChildrenEventTypes.ts

* Update booking-idempotency-key.ts

* Update WebhookService.test.ts

* Update events.test.ts

* Update queued-response.test.ts

* Update events.test.ts

* Update getRoutedUrl.test.ts

* fix: type checks

Signed-off-by: Omar López <zomars@me.com>

* fixes

Signed-off-by: Omar López <zomars@me.com>

* chore: bump platform libraries

* Update yarn.lock

* more fixes

Signed-off-by: Omar López <zomars@me.com>

* fixes

Signed-off-by: Omar López <zomars@me.com>

* biuld fixes

* chore: bump platform libraries

* Update conferencing.repository.ts

* Update conferencing.repository.ts

* Update getCalendarsEvents.test.ts

* Update vite.config.js

* chore: bump platform libraries

* Update users.ts

* Discard changes to docs/api-reference/v2/openapi.json

* Update vite.config.ts

* updated platform libraries

* Update get.handler.test.ts

* Update get.handler.test.ts

* Update schema.prisma

* Discard changes to docs/api-reference/v2/openapi.json

* Update next-auth-custom-adapter.ts

* Update team.ts

* Flurry of type fixes

* Fix majority of insight related type errors

* Type fixes for unlink of account

* Make user nullable again

* Fixed a bunch of unit tests and one type error

* Attempted mock fix

* Attempted fix for Attribute type

* Ensure default import becomes prisma, but not direct usage

* Import default as prisma in prisma.module

* Add attributeOption to attribute type

* Fix calcom/prisma mock

* Refactor Prisma client imports to @calcom/prisma/client

Updated all imports from '@prisma/client' to '@calcom/prisma/client' across tests and repository files for consistency and to use the correct Prisma client package. This change improves maintainability and ensures the correct client is referenced throughout the codebase.

* Undo removal of max-warnings=0 to get main to merge

* Remove unit tests for e2e fixtures, provide new prisma mock

* Mock @calcom/prisma in event manager

* Mock @calcom/prisma in event manager

* Add correct format even with --no-verify

* Mock prisma in CalendarManager

* Add mock for permission-check.service

* Better injection in PrismaApiKeyRepository imports

* More mock fixes :)

* Fix listMembers.handler.test

* Fix User import

* Appropriately adjust all types to be imported as types, there were a lot of types imported as normal deps

* Why was this a thing?

* Strictly speaking; Not using prismock anymore

* Ditched patch file for prismock

* Fix output.service.ts platform type imports, need concrete for plainToClass

* Better typing and tests for unlinkConnectedAccount.handler

* Small type fix

* Disable calendar cache tests as they are dependent on prismock

* chore: bump platform lib

* getRoutedUrl test remove of unused import

* Extract select to external const on getEventTypesFromDB

* Direct select of userSelect from selects/user

* fix type error from merging 23653

* Fixed integration tests by removing hardcoded values that were possible due to mocking, but as its now directly hitting the db no longer

* fix: vite config atoms prisma client type location

* revert: example app prisma client

* revert: example app prisma client

* bump platform libs

* fix: use class instead of type for DI of PlatformBookingsService

* update platform libs

* remove unused variable

* chore: generate prisma client for api v2

* fix: api v2 e2e

* fix: atoms e2e

* fix: atoms e2e

* fix: atoms e2e

* fix: api v2 e2e

* fix: tsconfig apiv2 enums

* publish libraries

* Simplify check for existence teamId

---------

Signed-off-by: Omar López <zomars@me.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com>
Co-authored-by: supalarry <laurisskraucis@gmail.com>
Co-authored-by: cal.com <morgan@cal.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
2025-09-11 15:27:50 +01:00

1090 lines
35 KiB
TypeScript

import "../../../../../tests/libs/__mocks__/prisma";
import { describe, expect, it } from "vitest";
import dayjs from "@calcom/dayjs";
import { getTimeView, getDateRanges, formatPeriod } from "../insightsDateUtils";
describe("EventsInsights", () => {
describe("getDateRanges", () => {
describe("UTC timezone", () => {
const timeZone = "UTC";
it("should handle daily ranges", () => {
const startDate = "2025-05-01T00:00:00.000Z"; // Beginning of May 1st UTC
const endDate = "2025-05-03T23:59:59.999Z"; // End of May 3rd UTC
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-01T23:59:59.999Z",
formattedDate: "May 1",
formattedDateFull: "May 1",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-02T00:00:00.000Z",
endDate: "2025-05-02T23:59:59.999Z",
formattedDate: "2",
formattedDateFull: "May 2",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-03T00:00:00.000Z",
endDate: "2025-05-03T23:59:59.999Z",
formattedDate: "3",
formattedDateFull: "May 3",
});
});
it("should handle weekly ranges starting mid-week", () => {
const startDate = "2025-05-01T00:00:00.000Z"; // Thursday
const endDate = "2025-05-25T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(5);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-03T23:59:59.999Z",
formattedDate: "May 1 - 3",
formattedDateFull: "May 1 - May 3",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-04T00:00:00.000Z",
endDate: "2025-05-10T23:59:59.999Z",
formattedDate: "May 4 - 10",
formattedDateFull: "May 4 - May 10",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-11T00:00:00.000Z",
endDate: "2025-05-17T23:59:59.999Z",
formattedDate: "May 11 - 17",
formattedDateFull: "May 11 - May 17",
});
expect(ranges[3]).toEqual({
startDate: "2025-05-18T00:00:00.000Z",
endDate: "2025-05-24T23:59:59.999Z",
formattedDate: "May 18 - 24",
formattedDateFull: "May 18 - May 24",
});
expect(ranges[4]).toEqual({
startDate: "2025-05-25T00:00:00.000Z",
endDate: "2025-05-25T23:59:59.999Z",
formattedDate: "May 25 - 25",
formattedDateFull: "May 25 - May 25",
});
});
it("should handle monthly ranges", () => {
const startDate = "2025-05-15T00:00:00.000Z";
const endDate = "2025-07-15T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "month",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-15T00:00:00.000Z",
endDate: "2025-05-31T23:59:59.999Z",
formattedDate: "May",
formattedDateFull: "May",
});
expect(ranges[1]).toEqual({
startDate: "2025-06-01T00:00:00.000Z",
endDate: "2025-06-30T23:59:59.999Z",
formattedDate: "Jun",
formattedDateFull: "Jun",
});
expect(ranges[2]).toEqual({
startDate: "2025-07-01T00:00:00.000Z",
endDate: "2025-07-15T23:59:59.999Z",
formattedDate: "Jul",
formattedDateFull: "Jul",
});
});
it("should handle yearly ranges", () => {
const startDate = "2025-06-15T00:00:00.000Z";
const endDate = "2027-03-15T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "year",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-06-15T00:00:00.000Z",
endDate: "2025-12-31T23:59:59.999Z",
formattedDate: "2025",
formattedDateFull: "2025",
});
expect(ranges[1]).toEqual({
startDate: "2026-01-01T00:00:00.000Z",
endDate: "2026-12-31T23:59:59.999Z",
formattedDate: "2026",
formattedDateFull: "2026",
});
expect(ranges[2]).toEqual({
startDate: "2027-01-01T00:00:00.000Z",
endDate: "2027-03-15T23:59:59.999Z",
formattedDate: "2027",
formattedDateFull: "2027",
});
});
it("should handle same day ranges", () => {
const startDate = "2025-05-01T00:00:00.000Z";
const endDate = "2025-05-01T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(1);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-01T23:59:59.999Z",
formattedDate: "May 1",
formattedDateFull: "May 1",
});
});
});
describe("Europe/Paris timezone", () => {
const timeZone = "Europe/Paris";
it("should handle daily ranges across DST", () => {
const startDate = "2025-03-29T23:00:00.000Z"; // March 30th 00:00 Paris time
const endDate = "2025-03-31T21:59:59.999Z"; // March 31st 23:59:59 Paris time
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
// DST begins on Sunday, March 30, 2025, at 3:00 AM local time
expect(ranges[0]).toEqual({
startDate: "2025-03-29T23:00:00.000Z", // March 30th 00:00 Paris time
endDate: "2025-03-30T21:59:59.999Z", // March 30th 23:59:59 Paris time
formattedDate: "Mar 30",
formattedDateFull: "Mar 30",
});
expect(new Date(ranges[0].endDate).getTime() - new Date(ranges[0].startDate).getTime()).toBeLessThan(
23 * 60 * 60 * 1000
); // 1 hour is skipped
expect(ranges[1]).toEqual({
startDate: "2025-03-30T22:00:00.000Z", // March 31st 00:00 Paris time
endDate: "2025-03-31T21:59:59.999Z", // March 31st 23:59:59 Paris time
formattedDate: "31",
formattedDateFull: "Mar 31",
});
});
it("should handle weekly ranges", () => {
const startDate = "2025-05-15T22:00:00.000Z"; // May 16th 00:00 Paris time
const endDate = "2025-05-29T21:59:59.999Z"; // May 29th 23:59:59 Paris time
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-15T22:00:00.000Z",
endDate: "2025-05-17T21:59:59.999Z",
formattedDate: "May 16 - 17",
formattedDateFull: "May 16 - May 17",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-17T22:00:00.000Z",
endDate: "2025-05-24T21:59:59.999Z",
formattedDate: "May 18 - 24",
formattedDateFull: "May 18 - May 24",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-24T22:00:00.000Z",
endDate: "2025-05-29T21:59:59.999Z",
formattedDate: "May 25 - 29",
formattedDateFull: "May 25 - May 29",
});
});
it("should handle monthly ranges", () => {
const startDate = "2025-05-31T22:00:00.000Z"; // June 1st 00:00 Paris time
const endDate = "2025-07-31T21:59:59.999Z"; // July 31st 23:59:59 Paris time
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "month",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
expect(ranges[0]).toEqual({
startDate: "2025-05-31T22:00:00.000Z",
endDate: "2025-06-30T21:59:59.999Z",
formattedDate: "Jun",
formattedDateFull: "Jun",
});
expect(ranges[1]).toEqual({
startDate: "2025-06-30T22:00:00.000Z",
endDate: "2025-07-31T21:59:59.999Z",
formattedDate: "Jul",
formattedDateFull: "Jul",
});
});
});
describe("Asia/Seoul timezone", () => {
const timeZone = "Asia/Seoul";
it("should handle daily ranges", () => {
const startDate = "2025-05-14T15:00:00.000Z"; // May 15th 00:00 Seoul time
const endDate = "2025-05-16T14:59:59.999Z"; // May 16th 23:59:59 Seoul time
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
expect(ranges[0]).toEqual({
startDate: "2025-05-14T15:00:00.000Z",
endDate: "2025-05-15T14:59:59.999Z",
formattedDate: "May 15",
formattedDateFull: "May 15",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-15T15:00:00.000Z",
endDate: "2025-05-16T14:59:59.999Z",
formattedDate: "16",
formattedDateFull: "May 16",
});
});
it("should handle weekly ranges", () => {
const startDate = "2025-05-11T15:00:00.000Z"; // May 12th 00:00 Seoul time (Monday)
const endDate = "2025-05-25T14:59:59.999Z"; // May 25th 23:59:59 Seoul time (Sunday)
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-11T15:00:00.000Z",
endDate: "2025-05-17T14:59:59.999Z",
formattedDate: "May 12 - 17",
formattedDateFull: "May 12 - May 17",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-17T15:00:00.000Z",
endDate: "2025-05-24T14:59:59.999Z",
formattedDate: "May 18 - 24",
formattedDateFull: "May 18 - May 24",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-24T15:00:00.000Z",
endDate: "2025-05-25T14:59:59.999Z",
formattedDate: "May 25 - 25",
formattedDateFull: "May 25 - May 25",
});
});
it("should handle monthly ranges", () => {
const startDate = "2025-04-30T15:00:00.000Z"; // May 1st 00:00 Seoul time
const endDate = "2025-06-30T14:59:59.999Z"; // June 30th 23:59:59 Seoul time
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "month",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
expect(ranges[0]).toEqual({
startDate: "2025-04-30T15:00:00.000Z",
endDate: "2025-05-31T14:59:59.999Z",
formattedDate: "May",
formattedDateFull: "May",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-31T15:00:00.000Z",
endDate: "2025-06-30T14:59:59.999Z",
formattedDate: "Jun",
formattedDateFull: "Jun",
});
});
});
it("should return empty array for invalid timeView", () => {
const startDate = "2025-05-01T00:00:00.000Z";
const endDate = "2025-05-03T23:59:59.999Z";
const timeZone = "UTC";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
weekStart: "Sunday",
// @ts-expect-error - Testing invalid timeView value
timeView: "invalid",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toEqual([]);
});
describe("Week start variations", () => {
const timeZone = "UTC";
it("should handle weekly ranges with Monday start", () => {
const startDate = "2025-05-01T00:00:00.000Z"; // Thursday
const endDate = "2025-05-14T23:59:59.999Z"; // Wednesday
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-04T23:59:59.999Z",
formattedDate: "May 1 - 4",
formattedDateFull: "May 1 - May 4",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-05T00:00:00.000Z",
endDate: "2025-05-11T23:59:59.999Z",
formattedDate: "May 5 - 11",
formattedDateFull: "May 5 - May 11",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-12T00:00:00.000Z",
endDate: "2025-05-14T23:59:59.999Z",
formattedDate: "May 12 - 14",
formattedDateFull: "May 12 - May 14",
});
});
it("should handle weekly ranges with Sunday start", () => {
const startDate = "2025-05-01T00:00:00.000Z"; // Thursday
const endDate = "2025-05-14T23:59:59.999Z"; // Wednesday
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Sunday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-03T23:59:59.999Z",
formattedDate: "May 1 - 3",
formattedDateFull: "May 1 - May 3",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-04T00:00:00.000Z",
endDate: "2025-05-10T23:59:59.999Z",
formattedDate: "May 4 - 10",
formattedDateFull: "May 4 - May 10",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-11T00:00:00.000Z",
endDate: "2025-05-14T23:59:59.999Z",
formattedDate: "May 11 - 14",
formattedDateFull: "May 11 - May 14",
});
});
it("should handle weekly ranges with Saturday start", () => {
const startDate = "2025-05-01T00:00:00.000Z"; // Thursday
const endDate = "2025-05-14T23:59:59.999Z"; // Wednesday
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Saturday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
expect(ranges[0]).toEqual({
startDate: "2025-05-01T00:00:00.000Z",
endDate: "2025-05-02T23:59:59.999Z",
formattedDate: "May 1 - 2",
formattedDateFull: "May 1 - May 2",
});
expect(ranges[1]).toEqual({
startDate: "2025-05-03T00:00:00.000Z",
endDate: "2025-05-09T23:59:59.999Z",
formattedDate: "May 3 - 9",
formattedDateFull: "May 3 - May 9",
});
expect(ranges[2]).toEqual({
startDate: "2025-05-10T00:00:00.000Z",
endDate: "2025-05-14T23:59:59.999Z",
formattedDate: "May 10 - 14",
formattedDateFull: "May 10 - May 14",
});
});
});
});
describe("formatPeriod", () => {
describe("Day View", () => {
describe("Beginning of data (wholeStart === start)", () => {
it("should always show month for the first day when same year", () => {
const result = formatPeriod({
start: dayjs("2024-01-15"),
end: dayjs("2024-01-15"),
timeView: "day",
wholeStart: dayjs("2024-01-15"), // Same as start
wholeEnd: dayjs("2024-01-20"),
});
expect(result).toBe("Jan 15");
});
it("should always show month for the first day when different years", () => {
const result = formatPeriod({
start: dayjs("2024-01-15"),
end: dayjs("2024-01-15"),
timeView: "day",
wholeStart: dayjs("2024-01-15"), // Same as start
wholeEnd: dayjs("2025-02-01"),
});
expect(result).toBe("Jan 15, 2024");
});
});
describe("First day of month (start.date() === 1)", () => {
it("should show month for 1st day of month when same year", () => {
const result = formatPeriod({
start: dayjs("2024-02-01"),
end: dayjs("2024-02-01"),
timeView: "day",
wholeStart: dayjs("2024-01-15"),
wholeEnd: dayjs("2024-02-10"),
});
expect(result).toBe("Feb 1");
});
it("should show month for 1st day of month when different years", () => {
const result = formatPeriod({
start: dayjs("2024-02-01"),
end: dayjs("2024-02-01"),
timeView: "day",
wholeStart: dayjs("2023-12-15"),
wholeEnd: dayjs("2024-03-01"),
});
expect(result).toBe("Feb 1, 2024");
});
});
describe("Regular days (not first day, not 1st of month)", () => {
it("should omit month for regular days when same year", () => {
const result = formatPeriod({
start: dayjs("2024-01-16"),
end: dayjs("2024-01-16"),
timeView: "day",
wholeStart: dayjs("2024-01-15"),
wholeEnd: dayjs("2024-01-20"),
});
expect(result).toBe("16");
});
it("should omit month but show year for regular days when different years", () => {
const result = formatPeriod({
start: dayjs("2024-01-16"),
end: dayjs("2024-01-16"),
timeView: "day",
wholeStart: dayjs("2023-12-15"),
wholeEnd: dayjs("2024-02-01"),
});
expect(result).toBe("16, 2024");
});
});
describe("Edge cases", () => {
it("should show month when start is both beginning of data AND 1st of month", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-01"),
timeView: "day",
wholeStart: dayjs("2024-01-01"), // Same as start AND 1st of month
wholeEnd: dayjs("2024-01-05"),
});
expect(result).toBe("Jan 1");
});
it("should show month for last day of month if it's 1st of month (edge case)", () => {
// This tests the case where start.date() === 1 takes precedence
const result = formatPeriod({
start: dayjs("2024-03-01"),
end: dayjs("2024-03-01"),
timeView: "day",
wholeStart: dayjs("2024-02-28"),
wholeEnd: dayjs("2024-03-05"),
});
expect(result).toBe("Mar 1");
});
it("should omit month for end of month when not 1st and not beginning", () => {
const result = formatPeriod({
start: dayjs("2024-01-31"),
end: dayjs("2024-01-31"),
timeView: "day",
wholeStart: dayjs("2024-01-15"),
wholeEnd: dayjs("2024-02-05"),
});
expect(result).toBe("31");
});
});
describe("Real-world scenarios", () => {
it("should format a range from Jan 15 to Jan 20 correctly", () => {
// Simulating how getDateRanges would call formatPeriod for each day
const wholeStart = dayjs("2024-01-15");
const wholeEnd = dayjs("2024-01-20");
const results: string[] = [];
for (let i = 0; i <= 5; i++) {
const currentDay = wholeStart.add(i, "day");
results.push(
formatPeriod({
start: currentDay,
end: currentDay,
timeView: "day",
wholeStart,
wholeEnd,
})
);
}
expect(results).toEqual(["Jan 15", "16", "17", "18", "19", "20"]);
});
it("should format a range from Jan 30 to Feb 3 correctly", () => {
// Simulating month transition
const wholeStart = dayjs("2024-01-30");
const wholeEnd = dayjs("2024-02-03");
const results: string[] = [];
let currentDay = wholeStart;
while (currentDay.isBefore(wholeEnd) || currentDay.isSame(wholeEnd)) {
results.push(
formatPeriod({
start: currentDay,
end: currentDay,
timeView: "day",
wholeStart,
wholeEnd,
})
);
currentDay = currentDay.add(1, "day");
}
expect(results).toEqual(["Jan 30", "31", "Feb 1", "2", "3"]);
});
it("should format a range from Dec 30, 2023 to Jan 2, 2024 correctly", () => {
// Simulating year transition with different years
const wholeStart = dayjs("2023-12-30");
const wholeEnd = dayjs("2024-01-02");
const results: string[] = [];
let currentDay = wholeStart;
while (currentDay.isBefore(wholeEnd) || currentDay.isSame(wholeEnd)) {
results.push(
formatPeriod({
start: currentDay,
end: currentDay,
timeView: "day",
wholeStart,
wholeEnd,
})
);
currentDay = currentDay.add(1, "day");
}
expect(results).toEqual(["Dec 30, 2023", "31, 2023", "Jan 1, 2024", "2, 2024"]);
});
it("should format a single day range correctly", () => {
const result = formatPeriod({
start: dayjs("2024-01-15"),
end: dayjs("2024-01-15"),
timeView: "day",
wholeStart: dayjs("2024-01-15"),
wholeEnd: dayjs("2024-01-15"),
});
expect(result).toBe("Jan 15");
});
});
});
describe("Week View", () => {
describe("Same month", () => {
it("should format dates without year when wholeStart and wholeEnd are same year", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-07"),
timeView: "week",
wholeStart: dayjs("2024-01-01"),
wholeEnd: dayjs("2024-12-31"),
});
expect(result).toBe("Jan 1 - 7");
});
it("should format dates with year when wholeStart and wholeEnd are different years", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-07"),
timeView: "week",
wholeStart: dayjs("2023-12-01"),
wholeEnd: dayjs("2024-02-01"),
});
expect(result).toBe("Jan 1 - 7, 2024");
});
});
describe("Different months", () => {
it("should format dates without year when wholeStart and wholeEnd are same year", () => {
const result = formatPeriod({
start: dayjs("2024-01-29"),
end: dayjs("2024-02-04"),
timeView: "week",
wholeStart: dayjs("2024-01-01"),
wholeEnd: dayjs("2024-12-31"),
});
expect(result).toBe("Jan 29 - Feb 4");
});
it("should format dates with year when wholeStart and wholeEnd are different years", () => {
const result = formatPeriod({
start: dayjs("2024-01-29"),
end: dayjs("2024-02-04"),
timeView: "week",
wholeStart: dayjs("2023-12-01"),
wholeEnd: dayjs("2024-03-01"),
});
expect(result).toBe("Jan 29 - Feb 4, 2024");
});
});
describe("Different years", () => {
it("should format dates with respective years when start and end span different years", () => {
const result = formatPeriod({
start: dayjs("2023-12-31"),
end: dayjs("2024-01-06"),
timeView: "week",
wholeStart: dayjs("2023-12-01"),
wholeEnd: dayjs("2024-01-31"),
});
expect(result).toBe("Dec 31 , 2023 - Jan 6, 2024");
});
});
});
describe("Month View", () => {
it("should format month without year when wholeStart and wholeEnd are same year", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-31"),
timeView: "month",
wholeStart: dayjs("2024-01-01"),
wholeEnd: dayjs("2024-12-31"),
});
expect(result).toBe("Jan");
});
it("should format month with year when wholeStart and wholeEnd are different years", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-31"),
timeView: "month",
wholeStart: dayjs("2023-12-01"),
wholeEnd: dayjs("2024-02-01"),
});
expect(result).toBe("Jan 2024");
});
});
describe("Year View", () => {
it("should format year regardless of wholeStart and wholeEnd values", () => {
const resultWithSameYear = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-12-31"),
timeView: "year",
wholeStart: dayjs("2024-01-01"),
wholeEnd: dayjs("2024-12-31"),
});
expect(resultWithSameYear).toBe("2024");
const resultWithDifferentYears = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-12-31"),
timeView: "year",
wholeStart: dayjs("2023-12-01"),
wholeEnd: dayjs("2025-01-01"),
});
expect(resultWithDifferentYears).toBe("2024");
});
});
describe("Invalid View", () => {
it("should return empty string for invalid timeView", () => {
const result = formatPeriod({
start: dayjs("2024-01-01"),
end: dayjs("2024-01-01"),
timeView: "invalid" as any,
wholeStart: dayjs("2024-01-01"),
wholeEnd: dayjs("2024-12-31"),
});
expect(result).toBe("");
});
});
});
describe("getTimeView", () => {
it("should return year for ranges over 365 days", () => {
const result = getTimeView("2024-01-01T00:00:00.000Z", "2025-02-01T00:00:00.000Z");
expect(result).toBe("year");
});
it("should return month for ranges between 90 and 365 days", () => {
const result = getTimeView("2024-01-01T00:00:00.000Z", "2024-05-01T00:00:00.000Z");
expect(result).toBe("month");
});
it("should return week for ranges between 14 and 90 days", () => {
const result = getTimeView("2024-01-01T00:00:00.000Z", "2024-02-01T00:00:00.000Z");
expect(result).toBe("week");
});
it("should return day for ranges under 14 days", () => {
const result = getTimeView("2024-01-01T00:00:00.000Z", "2024-01-10T00:00:00.000Z");
expect(result).toBe("day");
});
it("should handle same day range", () => {
const result = getTimeView("2024-01-01T00:00:00.000Z", "2024-01-01T23:59:59.999Z");
expect(result).toBe("day");
});
});
describe("getDateRanges - Formatting Comparison", () => {
const timeZone = "UTC";
describe("Daily view formatting differences", () => {
it("should show smart vs full formatting for consecutive days", () => {
const startDate = "2024-01-15T00:00:00.000Z";
const endDate = "2024-01-19T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(5);
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 15", "16", "17", "18", "19"]);
expect(formattedDatesFull).toEqual(["Jan 15", "Jan 16", "Jan 17", "Jan 18", "Jan 19"]);
});
it("should show formatting differences across month boundaries", () => {
const startDate = "2024-01-30T00:00:00.000Z";
const endDate = "2024-02-02T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(4);
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 30", "31", "Feb 1", "2"]);
expect(formattedDatesFull).toEqual(["Jan 30", "Jan 31", "Feb 1", "Feb 2"]);
});
});
describe("Weekly view formatting differences", () => {
it("should show smart vs full formatting for weekly ranges", () => {
const startDate = "2024-01-15T00:00:00.000Z"; // Monday
const endDate = "2024-01-28T23:59:59.999Z"; // Sunday
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 15 - 21", "Jan 22 - 28"]);
expect(formattedDatesFull).toEqual(["Jan 15 - Jan 21", "Jan 22 - Jan 28"]);
});
it("should show formatting differences for cross-month weekly ranges", () => {
const startDate = "2024-01-29T00:00:00.000Z"; // Monday
const endDate = "2024-02-11T23:59:59.999Z"; // Sunday
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "week",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(2);
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 29 - Feb 4", "Feb 5 - 11"]);
expect(formattedDatesFull).toEqual(["Jan 29 - Feb 4", "Feb 5 - Feb 11"]);
});
});
describe("Monthly view formatting differences", () => {
it("should show consistent formatting for monthly ranges", () => {
const startDate = "2024-01-15T00:00:00.000Z";
const endDate = "2024-03-15T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "month",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
expect(ranges).toHaveLength(3);
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan", "Feb", "Mar"]);
expect(formattedDatesFull).toEqual(["Jan", "Feb", "Mar"]);
});
});
describe("Real-world usage examples", () => {
it("should demonstrate practical differences for chart display", () => {
// Simulate a common analytics scenario: 7 days of data
const startDate = "2024-01-15T00:00:00.000Z";
const endDate = "2024-01-21T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 15", "16", "17", "18", "19", "20", "21"]);
expect(formattedDatesFull).toEqual([
"Jan 15",
"Jan 16",
"Jan 17",
"Jan 18",
"Jan 19",
"Jan 20",
"Jan 21",
]);
});
it("should show benefits of full formatting for data export", () => {
// Simulate data that might be exported or used in external systems
const startDate = "2024-01-30T00:00:00.000Z";
const endDate = "2024-02-05T23:59:59.999Z";
const ranges = getDateRanges({
startDate,
endDate,
timeZone,
timeView: "day",
weekStart: "Monday",
});
if (!ranges) {
throw new Error("Expected ranges to be defined");
}
const formattedDates = ranges.map((r) => r.formattedDate);
const formattedDatesFull = ranges.map((r) => r.formattedDateFull);
expect(formattedDates).toEqual(["Jan 30", "31", "Feb 1", "2", "3", "4", "5"]);
expect(formattedDatesFull).toEqual(["Jan 30", "Jan 31", "Feb 1", "Feb 2", "Feb 3", "Feb 4", "Feb 5"]);
});
});
});
});