Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

18.9 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
import { DateTime } from "luxon";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { RangePanel } from "./RangePanel";
import { Comparison, DEFAULT_COMPARISON, Time } from "./types";

vi.mock("next-intl", () => ({
  useExtracted: () => (message: string, values?: Record<string, string | number>) =>
    values
      ? message
          .replace(/\{(\w+), plural, one \{([^}]*)\} other \{([^}]*)\}\}/g, (_, key, one, other) =>
            String(values[key] === 1 ? one : other).replace("#", String(values[key]))
          )
          .replace(/\{(\w+)\}/g, (_, key) => String(values[key] ?? `{${key}}`))
      : message,
}));

const ZONE = "America/New_York";

const onApply = vi.fn();
const onCancel = vi.fn();
const setTimezone = vi.fn();

const renderPanel = (time: Time, props: { pastMinutesEnabled?: boolean; comparison?: Comparison } = {}) =>
  render(
    <RangePanel
      time={time}
      comparison={props.comparison ?? DEFAULT_COMPARISON}
      zone={ZONE}
      timezone="America/New_York"
      setTimezone={setTimezone}
      pastMinutesEnabled={props.pastMinutesEnabled ?? true}
      onApply={onApply}
      onCancel={onCancel}
    />
  );

const DATE_RANGE: Time = { mode: "range", startDate: "2024-03-08", endDate: "2024-03-14" };

/** react-day-picker tags each day button with a locale date string. */
const day = (label: string) => document.querySelector<HTMLButtonElement>(`[data-day="${label}"]`)!;

beforeEach(() => {
  vi.stubGlobal(
    "matchMedia",
    (query: string) =>
      ({
        matches: true,
        media: query,
        addEventListener: vi.fn(),
        removeEventListener: vi.fn(),
      }) as unknown as MediaQueryList
  );
  vi.stubGlobal(
    "ResizeObserver",
    class {
      observe() {}
      unobserve() {}
      disconnect() {}
    }
  );
  Element.prototype.scrollIntoView = vi.fn();
});

afterEach(() => {
  cleanup();
  vi.clearAllMocks();
  vi.unstubAllGlobals();
});

describe("RangePanel presets", () => {
  it("rules the preset groups apart instead of running them into one flat list", () => {
    const { container } = renderPanel(DATE_RANGE);

    // realtime | relative | calendar
    expect(container.querySelectorAll('[data-slot="command-separator"]').length).toBe(2);
  });

  it("keeps the list to the ten presets people learn, not the seventeen they scanned", () => {
    renderPanel(DATE_RANGE);

    expect(within(screen.getAllByRole("listbox")[0]).getAllByRole("option").length).toBe(10);
    expect(screen.getByText("This Week")).toBeTruthy();
    expect(screen.queryByText("Last 14 Days")).toBeNull();
    expect(screen.queryByText("Last Week")).toBeNull();
  });

  it("applies a preset immediately — the fast path does not wait for Apply", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(screen.getByText("Last 30 Days"));

    expect(onApply).toHaveBeenCalledTimes(1);
    expect(onApply.mock.calls[0][0]).toMatchObject({ mode: "range", wellKnown: "last-30-days" });
  });

  it("filters the list as you search", async () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "week" } });

    await waitFor(() => expect(screen.queryByText("Last 30 Days")).toBeNull());
    expect(screen.getByText("This Week")).toBeTruthy();
  });

  it("says so when nothing matches", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "quarter" } });

    expect(screen.getByText("No matching range")).toBeTruthy();
  });

  it("hides the realtime group when a caller disables past-minutes windows", () => {
    const { container } = renderPanel(DATE_RANGE, { pastMinutesEnabled: false });

    expect(screen.queryByText("Last 30 Minutes")).toBeNull();
    expect(screen.getByText("Today")).toBeTruthy();
    expect(container.querySelectorAll('[data-slot="command-separator"]').length).toBe(1);
  });
});

describe("RangePanel typed windows", () => {
  it("turns 14d into a window instead of a search, and applies it on select", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "14d" } });

    // The presets step aside: "3d" must not surface Last 30 Days beside the typed row.
    expect(screen.queryByText("Last 30 Days")).toBeNull();
    fireEvent.click(screen.getByText("Last 14 days"));

    expect(onApply).toHaveBeenCalledTimes(1);
    const applied = onApply.mock.calls[0][0];
    // 14 days is a dropped preset, so the typed window keeps its identity.
    expect(applied).toMatchObject({ mode: "range", wellKnown: "last-14-days" });
    expect(DateTime.fromISO(applied.endDate).diff(DateTime.fromISO(applied.startDate), "days").days).toBe(13);
  });

  it("applies a count no preset covers as a plain range", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "20d" } });
    fireEvent.click(screen.getByText("Last 20 days"));

    const applied = onApply.mock.calls[0][0];
    expect(applied.mode).toBe("range");
    expect(applied.wellKnown).toBeUndefined();
    expect(DateTime.fromISO(applied.endDate).diff(DateTime.fromISO(applied.startDate), "days").days).toBe(19);
  });

  it("serves hours from the realtime path", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "5h" } });
    fireEvent.click(screen.getByText("Last 5 hours"));

    expect(onApply.mock.calls[0][0]).toEqual({ mode: "past-minutes", pastMinutesStart: 300, pastMinutesEnd: 0 });
  });

  it("applies the typed row on Enter", () => {
    renderPanel(DATE_RANGE);

    const input = screen.getByPlaceholderText(/Search/);
    fireEvent.change(input, { target: { value: "20d" } });
    fireEvent.keyDown(input, { key: "Enter" });

    expect(onApply).toHaveBeenCalledTimes(1);
    expect(onApply.mock.calls[0][0].wellKnown).toBeUndefined();
  });

  it("still lets Enter pick the first row when the current preset is no longer in the rail", () => {
    // A stored default of a dropped preset used to become cmdk's defaultValue
    // with nothing to match, leaving no row selected and Enter dead.
    const zone = "America/New_York";
    const today = DateTime.now().setZone(zone);
    renderPanel({
      mode: "range",
      startDate: today.minus({ days: 13 }).toISODate()!,
      endDate: today.toISODate()!,
      wellKnown: "last-14-days",
    });

    fireEvent.keyDown(screen.getByPlaceholderText(/Search/), { key: "Enter" });

    expect(onApply).toHaveBeenCalledTimes(1);
    expect(onApply.mock.calls[0][0]).toMatchObject({ wellKnown: "last-30-minutes" });
  });

  it("names the unit letters while only the digits are typed", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "14" } });

    expect(screen.getByText("Last 14 …")).toBeTruthy();
    expect(within(screen.getAllByRole("listbox")[0]).queryAllByRole("option").length).toBe(0);
  });

  it("does not offer minutes or hours where realtime windows are disabled", () => {
    renderPanel(DATE_RANGE, { pastMinutesEnabled: false });

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "6h" } });
    expect(screen.queryByText("Last 6 hours")).toBeNull();
    expect(screen.getByText("No matching range")).toBeTruthy();

    fireEvent.change(screen.getByPlaceholderText(/Search/), { target: { value: "14" } });
    expect(screen.queryByText("m")).toBeNull();
    expect(screen.getByText("d")).toBeTruthy();
  });
});

describe("RangePanel draft", () => {
  it("seeds the bound fields from the current window", () => {
    renderPanel(DATE_RANGE);

    expect(screen.getByLabelText<HTMLInputElement>("Start date").value).toBe("2024-03-08");
    // the stored end is exclusive; the field shows the last day actually included
    expect(screen.getByLabelText<HTMLInputElement>("End date").value).toBe("2024-03-14");
    expect(screen.getByLabelText<HTMLInputElement>("Start time").value).toBe("");
  });

  it("picking days on the calendar does not apply anything until Apply is pressed", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(day("3/20/2024"));
    fireEvent.click(day("3/25/2024"));

    expect(onApply).not.toHaveBeenCalled();
    expect(screen.getByLabelText<HTMLInputElement>("Start date").value).toBe("2024-03-20");
    expect(screen.getByLabelText<HTMLInputElement>("End date").value).toBe("2024-03-25");

    fireEvent.click(screen.getByRole("button", { name: "Apply" }));

    expect(onApply).toHaveBeenCalledWith(
      { mode: "range", startDate: "2024-03-20", endDate: "2024-03-25" },
      DEFAULT_COMPARISON
    );
  });

  it("a fresh click starts a new range rather than extending the old one", () => {
    renderPanel(DATE_RANGE);

    // react-day-picker's own range mode would keep 2024-03-08 as the start here
    fireEvent.click(day("3/20/2024"));

    expect(screen.getByLabelText<HTMLInputElement>("Start date").value).toBe("2024-03-20");
    expect(screen.getByLabelText<HTMLInputElement>("End date").value).toBe("2024-03-20");
  });

  it("highlights the drafted range on the calendar, not react-day-picker's own guess", () => {
    renderPanel(DATE_RANGE);

    // react-day-picker's own range mode would extend 3/8–3/14 to 3/8–3/20 here
    fireEvent.click(day("3/20/2024"));
    expect(day("3/20/2024").dataset.rangeStart).toBe("true");
    expect(day("3/20/2024").dataset.rangeEnd).toBe("true");
    expect(day("3/8/2024").closest("[data-selected]")).toBeNull();

    // ...and then shrink that to 3/8–3/18 instead of drawing 3/18–3/20
    fireEvent.click(day("3/18/2024"));
    expect(day("3/18/2024").dataset.rangeStart).toBe("true");
    expect(day("3/19/2024").dataset.rangeMiddle).toBe("true");
    expect(day("3/20/2024").dataset.rangeEnd).toBe("true");
    expect(day("3/8/2024").closest("[data-selected]")).toBeNull();
  });

  it("follows a typed date on the calendar", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByLabelText("End date"), { target: { value: "2024-03-11" } });

    expect(day("3/11/2024").dataset.rangeEnd).toBe("true");
    expect(day("3/14/2024").closest("[data-selected]")).toBeNull();
  });

  it("one click is a single day, which is what gets an hourly bucket", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(day("3/20/2024"));
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));

    expect(onApply).toHaveBeenCalledWith({ mode: "day", day: "2024-03-20" }, DEFAULT_COMPARISON);
  });

  it("typing a clock promotes the window to an exact datetime range", () => {
    renderPanel(DATE_RANGE);

    fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "09:30" } });
    fireEvent.change(screen.getByLabelText("End time"), { target: { value: "17:00" } });
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));

    expect(onApply).toHaveBeenCalledWith(
      {
        mode: "range",
        startDate: "2024-03-08",
        startTime: "09:30:00",
        endDate: "2024-03-14",
        endTime: "17:00:00",
      },
      DEFAULT_COMPARISON
    );
  });

  it("typing a date is not clobbered mid-edit by an unparseable intermediate value", () => {
    renderPanel(DATE_RANGE);

    const endDate = screen.getByLabelText<HTMLInputElement>("End date");
    fireEvent.change(endDate, { target: { value: "" } });
    expect(endDate.value).toBe("");

    fireEvent.change(endDate, { target: { value: "2024-03-11" } });
    expect(endDate.value).toBe("2024-03-11");

    fireEvent.click(screen.getByRole("button", { name: "Apply" }));
    expect(onApply).toHaveBeenCalledWith(
      { mode: "range", startDate: "2024-03-08", endDate: "2024-03-11" },
      DEFAULT_COMPARISON
    );
  });

  it("keeps the clocks when the days move under them", () => {
    renderPanel({
      mode: "range",
      startDate: "2024-03-08",
      startTime: "09:30:00",
      endDate: "2024-03-09",
      endTime: "17:00:00",
    });

    fireEvent.click(day("3/20/2024"));
    fireEvent.click(day("3/25/2024"));
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));

    expect(onApply).toHaveBeenCalledWith(
      {
        mode: "range",
        startDate: "2024-03-20",
        startTime: "09:30:00",
        endDate: "2024-03-25",
        endTime: "17:00:00",
      },
      DEFAULT_COMPARISON
    );
  });

  it("keeps an overnight clock through the first of the two clicks", () => {
    // 17:00 -> 09:00 is inverted on a single day, so collapsing the selection to
    // the anchor day would have thrown the clocks away before the second click
    renderPanel({
      mode: "range",
      startDate: "2024-03-08",
      startTime: "17:00:00",
      endDate: "2024-03-09",
      endTime: "09:00:00",
    });

    fireEvent.click(day("3/20/2024"));
    fireEvent.click(day("3/25/2024"));
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));

    expect(onApply).toHaveBeenCalledWith(
      {
        mode: "range",
        startDate: "2024-03-20",
        startTime: "17:00:00",
        endDate: "2024-03-25",
        endTime: "09:00:00",
      },
      DEFAULT_COMPARISON
    );
  });

  it("refuses to apply while the fields do not describe a window", () => {
    renderPanel(DATE_RANGE);

    // an end before the start: the previous draft is still what Apply would send
    fireEvent.change(screen.getByLabelText("End date"), { target: { value: "2024-03-01" } });

    const apply = screen.getByRole("button", { name: "Apply" }) as HTMLButtonElement;
    expect(apply.disabled).toBe(true);
    fireEvent.click(apply);
    expect(onApply).not.toHaveBeenCalled();

    // and it recovers once the range makes sense again
    fireEvent.change(screen.getByLabelText("End date"), { target: { value: "2024-03-12" } });
    expect((screen.getByRole("button", { name: "Apply" }) as HTMLButtonElement).disabled).toBe(false);
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));
    expect(onApply).toHaveBeenCalledWith(
      { mode: "range", startDate: "2024-03-08", endDate: "2024-03-12" },
      DEFAULT_COMPARISON
    );
  });

  it("refuses a typed date past today, which the calendar already disables", () => {
    renderPanel(DATE_RANGE);

    const future = DateTime.now().plus({ years: 1 }).toISODate()!;
    fireEvent.change(screen.getByLabelText("End date"), { target: { value: future } });

    expect((screen.getByRole("button", { name: "Apply" }) as HTMLButtonElement).disabled).toBe(true);
  });

  it("refuses a wall time that daylight saving skipped", () => {
    renderPanel({ mode: "range", startDate: "2024-03-10", endDate: "2024-03-10" });

    fireEvent.change(screen.getByLabelText("Start time"), { target: { value: "02:30" } });

    expect((screen.getByRole("button", { name: "Apply" }) as HTMLButtonElement).disabled).toBe(true);
  });

  it("Cancel discards the draft", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(day("3/20/2024"));
    fireEvent.click(screen.getByRole("button", { name: "Cancel" }));

    expect(onApply).not.toHaveBeenCalled();
    expect(onCancel).toHaveBeenCalledTimes(1);
  });
});

describe("RangePanel footer", () => {
  it("names the comparison window the dashboard will chart against", () => {
    renderPanel(DATE_RANGE);

    expect(screen.getByText("Compares against Mar 1 – Mar 7")).toBeTruthy();
  });

  it("says so when there is no comparison period", () => {
    renderPanel({ mode: "all-time" });

    expect(screen.getByText("No comparison period")).toBeTruthy();
  });

  it("changes what the dashboard compares against, and applies it with the period", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(screen.getByRole("button", { name: /Compares against/ }));
    fireEvent.click(screen.getByRole("button", { name: /Same period last year/ }));

    // drafted, not applied: the dashboard does not move until Apply
    expect(onApply).not.toHaveBeenCalled();
    expect(screen.getByText("Compares against Mar 8 – Mar 14, 2023")).toBeTruthy();

    fireEvent.click(screen.getByRole("button", { name: "Apply" }));
    expect(onApply).toHaveBeenCalledWith(DATE_RANGE, { mode: "year" });
  });

  it("turns the comparison off entirely", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(screen.getByRole("button", { name: /Compares against/ }));
    fireEvent.click(screen.getByRole("button", { name: /No comparison/ }));

    expect(screen.getByText("No comparison period")).toBeTruthy();
    fireEvent.click(screen.getByRole("button", { name: "Apply" }));
    expect(onApply).toHaveBeenCalledWith(DATE_RANGE, { mode: "none" });
  });

  it("offers only the comparisons a realtime window can answer", () => {
    renderPanel({ mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 });

    fireEvent.click(screen.getByRole("button", { name: /Compares against/ }));

    expect(screen.getByRole("button", { name: /Previous period/ })).toBeTruthy();
    expect(screen.queryByRole("button", { name: /Same period last year/ })).toBeNull();
    expect(screen.queryByRole("button", { name: /Matching weekdays/ })).toBeNull();
  });

  it("compares against a window typed by hand", () => {
    renderPanel(DATE_RANGE);

    fireEvent.click(screen.getByRole("button", { name: /Compares against/ }));
    fireEvent.click(screen.getByRole("button", { name: /Custom range/ }));

    fireEvent.change(screen.getByLabelText("Comparison start date"), { target: { value: "2023-12-01" } });
    fireEvent.change(screen.getByLabelText("Comparison end date"), { target: { value: "2023-12-07" } });

    fireEvent.click(screen.getByRole("button", { name: "Apply" }));
    expect(onApply).toHaveBeenCalledWith(DATE_RANGE, {
      mode: "custom",
      customTime: { mode: "range", startDate: "2023-12-01", endDate: "2023-12-07" },
    });
  });

  it("surfaces the timezone and lets it be searched", async () => {
    renderPanel(DATE_RANGE);

    const trigger = screen.getByRole("button", { name: /America\/New_York/ });
    fireEvent.click(trigger);

    const search = await screen.findByPlaceholderText("Search timezones");
    fireEvent.change(search, { target: { value: "tokyo" } });

    const option = await screen.findByText(/Asia\/Tokyo/);
    fireEvent.click(option);

    expect(setTimezone).toHaveBeenCalledWith("Asia/Tokyo");
  });
});

describe("RangePanel calendar", () => {
  it("does not let a future day be picked", () => {
    const { container } = renderPanel({ mode: "day", day: new Date().toISOString().slice(0, 10) });

    const disabled = container.querySelectorAll("button[disabled][data-day]");
    expect(disabled.length).toBeGreaterThan(0);
  });

  it("offers month and year dropdowns rather than only chevrons", () => {
    const { container } = renderPanel(DATE_RANGE);

    const dropdowns = container.querySelectorAll("select");
    expect(dropdowns.length).toBeGreaterThan(0);
  });

  it("opens on the month the current window ends in", () => {
    const { container } = renderPanel(DATE_RANGE);

    const captions = within(container).getAllByText(/March 2024|Mar 2024/);
    expect(captions.length).toBeGreaterThan(0);
  });
});