Skip to content

Commit

Permalink
Add tests for utils/getDateTimeRange helper
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Nov 18, 2024
1 parent 564f955 commit aa995ea
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion app/lib/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { getFiltersFromSearchParams } from "../utils";

Check warning on line 1 in app/lib/__tests__/utils.test.ts

View workflow job for this annotation

GitHub Actions / test

'/home/runner/work/counterscale/counterscale/app/lib/utils.ts' imported multiple times
import { describe, test, expect } from "vitest";
import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
import { getDateTimeRange } from "../utils";

Check warning on line 3 in app/lib/__tests__/utils.test.ts

View workflow job for this annotation

GitHub Actions / test

'/home/runner/work/counterscale/counterscale/app/lib/utils.ts' imported multiple times
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.extend(utc);
dayjs.extend(timezone);

// test this getfiltersfromsearchparams function
describe("getFiltersFromSearchParams", () => {
Expand All @@ -24,3 +31,63 @@ describe("getFiltersFromSearchParams", () => {
});
});
});

describe("getDateTimeRange", () => {
const tz = "America/Los_Angeles";
const mockDate = new Date("2024-01-15T15:00:00Z"); // 7am PST on Jan 15, 2024

beforeEach(() => {
vi.setSystemTime(mockDate);
});

afterEach(() => {
vi.restoreAllMocks();
});

test("handles 'today' interval", () => {
const { startDate, endDate } = getDateTimeRange("today", tz);

// Start date should be Jan 15, 2024 00:00 PST
expect(startDate).toEqual(new Date("2024-01-15T08:00:00Z")); // 00:00 PST = 08:00 UTC
expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z")); // Current time (7am PST = 15:00 UTC)
});

test("handles 'yesterday' interval", () => {
const { startDate, endDate } = getDateTimeRange("yesterday", tz);

expect(startDate).toEqual(new Date("2024-01-14T08:00:00Z"));
expect(endDate).toEqual(new Date("2024-01-15T08:00:00.001Z"));
});

test("handles '7d' interval", () => {
const { startDate, endDate } = getDateTimeRange("7d", tz);

expect(startDate).toEqual(new Date("2024-01-08T08:00:00Z"));
expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z"));
});

test("handles '1d' interval", () => {
const { startDate, endDate } = getDateTimeRange("1d", tz);

expect(startDate).toEqual(new Date("2024-01-14T15:00:00Z"));
expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z"));
});

test("works with different timezones", () => {
const tokyoTz = "Asia/Tokyo"; // UTC+9
const londonTz = "Europe/London"; // UTC+0 in January

// When it's 7am PST (15:00 UTC) on Jan 15:
// - Tokyo: 00:00 JST on Jan 16
// - London: 15:00 GMT on Jan 15
// - LA: 7:00 PST on Jan 15

const tokyo = getDateTimeRange("today", tokyoTz);
const london = getDateTimeRange("today", londonTz);

// Tokyo's start date should be Jan 15 15:00 UTC (00:00 JST Jan 16)
expect(tokyo.startDate).toEqual(new Date("2024-01-15T15:00:00Z"));
// London's start date should be Jan 15 00:00 UTC
expect(london.startDate).toEqual(new Date("2024-01-15T00:00:00Z"));
});
});

0 comments on commit aa995ea

Please sign in to comment.