Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions web/src/utils/determine-time-range.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import dayjs from "dayjs";
import { InsightRange } from "~/queries/insight/insight.config";
import { InsightResolution } from "~~/model/insight_pb";
import { determineTimeRange } from "./determine-time-range";

beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date("2024-06-15T12:00:00.000Z"));
});

afterAll(() => {
jest.useRealTimers();
});

describe("determineTimeRange", () => {
describe("DAILY resolution", () => {
it("determines time range for LAST_1_WEEK", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_WEEK,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2024-06-09T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_1_MONTH", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_MONTH,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2024-05-16T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_3_MONTHS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_3_MONTHS,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2024-03-16T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_6_MONTHS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_6_MONTHS,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2023-12-16T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_1_YEAR", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_YEAR,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2023-06-16T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_2_YEARS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_2_YEARS,
InsightResolution.DAILY
);

expect(dayjs(from).toISOString()).toBe("2022-06-16T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});
});

describe("MONTHLY resolution", () => {
it("determines time range for LAST_1_WEEK (maps to month-to-date)", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_WEEK,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2024-06-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_1_MONTH (maps to month-to-date)", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_MONTH,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2024-06-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_3_MONTHS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_3_MONTHS,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2024-04-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_6_MONTHS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_6_MONTHS,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2024-01-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_1_YEAR", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_1_YEAR,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2023-07-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});

it("determines time range for LAST_2_YEARS", () => {
const [from, to] = determineTimeRange(
InsightRange.LAST_2_YEARS,
InsightResolution.MONTHLY
);

expect(dayjs(from).toISOString()).toBe("2022-07-01T00:00:00.000Z");
expect(dayjs(to).toISOString()).toBe("2024-06-15T23:59:59.999Z");
});
});
});
Loading