fix(ui): default customer usage range to month to date (#51923)

This commit is contained in:
moe-berri 2026-11-16 04:03:02 +08:00 • committed by GitHub
parent 28c892497c
commit 93c3074ecb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 7 deletions

View file

@ -34,16 +34,29 @@ describe("customer usage calendar ranges", () => {
["?range=2024-02-01..2024-02-29&period=previous", "2024-02-01..2024-02-29", "previous"],
["?from=2024-02-01&to=2024-02-29", "2024-02-01..2024-02-29", null],
["?range=MTD&from=2024-01-01&to=2024-01-03", "2024-03-01..2024-03-01", "MTD"],
["?range=garbage", "2024-03-01..2024-03-31", "month"],
["?from=2024-02-30&to=2024-03-02", "2024-03-01..2024-03-31", "month"],
["?from=2024-03-02&to=2024-03-01", "2024-03-01..2024-03-31", "month"],
["?from=2024-01-01&to=2025-02-05", "2024-03-01..2024-03-31", "month"],
["?range=month", "2024-03-01..2024-03-31", "month"],
["?range=garbage", "2024-03-01..2024-03-01", "MTD"],
["?from=2024-02-30&to=2024-03-02", "2024-03-01..2024-03-01", "MTD"],
["?from=2024-03-02&to=2024-03-01", "2024-03-01..2024-03-01", "MTD"],
["?from=2024-01-01&to=2025-02-05", "2024-03-01..2024-03-01", "MTD"],
])("resolves saved links and falls back for invalid dates in %s", (query, expected, preset) => {
const selection = customerDateSelection(loadDates(query), new Date("2024-03-01T00:30:00Z"));
expect(customerRangeParser.serialize(selection.dateValue)).toBe(expected);
expect(selection.preset).toBe(preset);
});
it.each([
["2026-11-15T12:00:00Z", "2026-11-01..2026-11-15"],
["2026-11-01T00:30:00+02:00", "2026-10-01..2026-10-31"],
["2026-10-31T23:30:00-02:00", "2026-11-01..2026-11-01"],
])("defaults an absent date query to UTC month to date at %s", (now, expected) => {
const selection = customerDateSelection(loadDates(""), new Date(now));
const expectedPatch = { range: "MTD", period: null, from: null, to: null };
expect(customerRangeParser.serialize(selection.dateValue)).toBe(expected);
expect(selection.preset).toBe("MTD");
expect(customerDatePatch(selection.dateValue, selection.preset)).toEqual(expectedPatch);
});
it("keeps saved calendar dates when their preset label is stale", () => {
const now = new Date("2024-06-01T12:00:00Z");
const selection = customerDateSelection(loadDates("?range=2024-02-01..2024-02-29&period=previous"), now);

View file

@ -92,7 +92,7 @@ export const customerDateSelection = (params: DateParams, now = new Date()) => {
}
const custom = params.from && params.to ? customerRangeParser.parse(`${params.from}..${params.to}`) : null;
if (custom) return { dateValue: custom, preset: null };
return { dateValue: customerPresetRange("month", now), preset: "month" };
return { dateValue: customerPresetRange("MTD", now), preset: "MTD" };
};
export const customerDatePatch = (value: DateRangePickerValue, preset: string | null) => {

View file

@ -122,6 +122,10 @@ describe("customer organization overview", () => {
renderWithProviders(<CustomersPage />);
await screen.findByRole("link", { name: "Customer 29" });
expect(customerLinks()[0]).toHaveTextContent("Customer 29");
expect(customerLinks()[0]).toHaveAttribute(
"href",
"/ui/customers/usage?org=customer-29&from=2024-03-01&to=2024-03-15",
);
const overview = screen.getByRole("region", { name: "Overall customer usage" });
expect(within(overview).getByText("$100.00")).toBeInTheDocument();
expect(within(overview).getByText("20")).toBeInTheDocument();

View file

@ -217,8 +217,9 @@ describe("CustomerHierarchyUsagePage", () => {
});
it.each([
["2026-11-15T00:30:00Z", "", "2026-11-01", "2026-11-30"],
["2027-01-01T00:30:00Z", "", "2027-01-01", "2027-01-31"],
["2026-11-15T00:30:00Z", "", "2026-11-01", "2026-11-15"],
["2027-01-01T00:30:00Z", "", "2027-01-01", "2027-01-01"],
["2026-11-15T00:30:00Z", "&range=month", "2026-11-01", "2026-11-30"],
["2026-11-15T00:30:00Z", "&range=MTD", "2026-11-01", "2026-11-15"],
["2026-11-30T23:30:00Z", "&range=today", "2026-11-30", "2026-11-30"],
])("uses UTC calendar dates at %s with %s", async (now, query, startDate, endDate) => {