Simpl-Resultat/src/utils/dateRange.test.ts
le king fu ce25ddc191 feat: open the Reports on « Cette année » and let « Tout » reach every date (#479)
Without any period in the URL, the Reports opened on the whole calendar
year (January 1st to December 31st, months to come included) under the
« Personnalisé » button. They now open on « Cette année », the year to
date (January 1st to today), as the Dashboard does, with its button lit
(decision 1 of Max, 2026-10-20). « Tout » showed the current calendar
year too: resolveReportsPeriod replaced its empty range by that year.
It now leaves `from` and `to` absent, and the services add no date
clause (decision 2).

- computeDateRange takes `today` as a last optional parameter (default:
  the current day); no existing caller changes, the Dashboard included.
- resolveReportsPeriod: a bookmark with two valid dates is kept as it
  is; a valid preset other than `custom` gets its range for `today`
  (`all`: none); anything else (no parameter, an invalid period,
  `custom` without two valid dates) falls back to « Cette année ».
  currentYearRange is gone; `from` and `to` are optional in
  UseReportsPeriodResult.
- getCategoryZoom takes optional bounds: its four queries skip a bound
  bound to NULL, `($2 IS NULL OR t.date >= $2) AND ($3 IS NULL OR
  t.date <= $3)`, with the same parameters in the same order
  ([id, from ?? null, to ?? null]). In the per-child query the
  condition stays in the LEFT JOIN's ON, so a subcategory without any
  transaction in the period keeps its row (total 0).
- useTrends and useCategoryZoom pass absent bounds through.
- useCompare: syncReferenceOnPeriodChange takes an absent `to`; on
  « Tout » the reference month shown does not move, and a later switch
  to a dated period syncs it to the end of that period (decision of
  Max, 2026-10-27). The ref is typed `string | undefined`, so a direct
  opening on ?period=all works.
- Texts fixed by Max (2026-10-27): reports.help.tips[2] and
  reports.hub.trendsDescription, FR and EN, no longer say "12 months":
  Trends follows the period selector.

Tests: the default, a custom bookmark, `custom` without dates, a preset
and `all` resolved for a fixed day; computeDateRange with `today`;
getCategoryZoom without bounds ([42, null, null] in every query, no
bare date condition), with bounds, and the per-child bounds in the ON;
the sync on « Tout »; wiring tests (src/test/hookHarness.ts) of
useCompare (mount on « Cette année » or « Tout », « Tout » then a dated
period, a user-picked month kept), useTrends (both views) and
useCategoryZoom (absent bounds reach the service).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 23:04:27 -04:00

135 lines
4.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { computeDateRange, buildMonthOptions } from "./dateRange";
describe("computeDateRange", () => {
beforeEach(() => {
// Fix "now" to 2025-07-15 for deterministic tests
vi.useFakeTimers();
vi.setSystemTime(new Date(2025, 6, 15)); // July 15, 2025
});
afterEach(() => {
vi.useRealTimers();
});
it('returns empty object for "all" period', () => {
expect(computeDateRange("all")).toEqual({});
});
it('returns custom range for "custom" period', () => {
expect(computeDateRange("custom", "2025-01-01", "2025-06-30")).toEqual({
dateFrom: "2025-01-01",
dateTo: "2025-06-30",
});
});
it('falls back to default when "custom" has missing dates', () => {
const result = computeDateRange("custom");
// Should fall through to default (same as "month")
expect(result.dateFrom).toBe("2025-07-01");
expect(result.dateTo).toBe("2025-07-15");
});
it('computes "month" period (first of current month to today)', () => {
const result = computeDateRange("month");
expect(result).toEqual({ dateFrom: "2025-07-01", dateTo: "2025-07-15" });
});
it('computes "3months" period (3 months back)', () => {
const result = computeDateRange("3months");
expect(result).toEqual({ dateFrom: "2025-05-01", dateTo: "2025-07-15" });
});
it('computes "6months" period (6 months back)', () => {
const result = computeDateRange("6months");
expect(result).toEqual({ dateFrom: "2025-02-01", dateTo: "2025-07-15" });
});
it('computes "year" period (Jan 1st of current year)', () => {
const result = computeDateRange("year");
expect(result).toEqual({ dateFrom: "2025-01-01", dateTo: "2025-07-15" });
});
it('computes "12months" period (12 months back)', () => {
const result = computeDateRange("12months");
expect(result).toEqual({ dateFrom: "2024-08-01", dateTo: "2025-07-15" });
});
// `today` is injectable (#479): the Reports resolve their default, the year
// to date, from a fixed day in tests, whatever the clock says.
it('computes "year" from the given today, not the clock (#479)', () => {
const result = computeDateRange("year", undefined, undefined, new Date(2026, 3, 14));
expect(result).toEqual({ dateFrom: "2026-01-01", dateTo: "2026-04-14" });
});
it("computes a rolling preset from the given today too (#479)", () => {
const result = computeDateRange("6months", undefined, undefined, new Date(2026, 0, 20));
expect(result).toEqual({ dateFrom: "2025-08-01", dateTo: "2026-01-20" });
});
it("handles January rollover for 3months period", () => {
vi.setSystemTime(new Date(2025, 1, 10)); // Feb 10, 2025
const result = computeDateRange("3months");
expect(result).toEqual({ dateFrom: "2024-12-01", dateTo: "2025-02-10" });
});
it("handles January rollover for 6months period", () => {
vi.setSystemTime(new Date(2025, 0, 20)); // Jan 20, 2025
const result = computeDateRange("6months");
expect(result).toEqual({ dateFrom: "2024-08-01", dateTo: "2025-01-20" });
});
it("handles January rollover for 12months period", () => {
vi.setSystemTime(new Date(2025, 0, 5)); // Jan 5, 2025
const result = computeDateRange("12months");
expect(result).toEqual({ dateFrom: "2024-02-01", dateTo: "2025-01-05" });
});
});
describe("buildMonthOptions", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2025, 6, 15)); // July 15, 2025
});
afterEach(() => {
vi.useRealTimers();
});
it("returns 24 month options", () => {
const options = buildMonthOptions("fr");
expect(options).toHaveLength(24);
});
it("starts with the current month", () => {
const options = buildMonthOptions("en");
expect(options[0].key).toBe("2025-7");
expect(options[0].value).toBe("2025-7");
});
it("ends 23 months ago", () => {
const options = buildMonthOptions("en");
expect(options[23].key).toBe("2023-8");
expect(options[23].value).toBe("2023-8");
});
it("handles January rollover correctly", () => {
vi.setSystemTime(new Date(2025, 0, 15)); // Jan 15, 2025
const options = buildMonthOptions("en");
expect(options[0].key).toBe("2025-1");
expect(options[1].key).toBe("2024-12");
expect(options[12].key).toBe("2024-1");
});
it("capitalizes the first letter of labels", () => {
const options = buildMonthOptions("fr");
for (const opt of options) {
expect(opt.label[0]).toBe(opt.label[0].toUpperCase());
}
});
it("labels contain year information", () => {
const options = buildMonthOptions("en");
expect(options[0].label).toContain("2025");
});
});