Simpl-Resultat/src/hooks/useCategoryZoom.wiring.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

59 lines
2.4 KiB
TypeScript

// useCategoryZoom — the period's bounds reach the service as they are, so
// « Tout » asks for every date (#479).
//
// React's hooks come from the shared harness (src/test/hookHarness.ts, #409):
// the hook runs as a plain function, its reducer is applied, and `commit()`
// runs its effects. The service and the reports period are mocked: on
// « Tout », `from` and `to` are absent (resolveReportsPeriod). What is pinned:
// the zoom hands those absent bounds to `getCategoryZoom`, which binds them as
// NULL and skips them (reportService.test.ts), instead of a range of its own.
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("react", async (orig) => (await import("../test/hookHarness")).fakeReact(await orig()));
const period = vi.hoisted(() => ({
from: undefined as string | undefined,
to: undefined as string | undefined,
}));
vi.mock("./useReportsPeriod", () => ({
useReportsPeriod: () => ({ from: period.from, to: period.to }),
}));
const services = vi.hoisted(() => ({ getCategoryZoom: vi.fn() }));
vi.mock("../services/reportService", () => services);
import { hookHarness } from "../test/hookHarness";
import { useCategoryZoom } from "./useCategoryZoom";
const harness = hookHarness();
/** Mounts the hook, picks a category, and runs the effects of that render. */
function zoomOn(categoryId: number) {
harness.render(useCategoryZoom);
harness.commit(); // no category yet: nothing is asked
harness.render(useCategoryZoom).setCategory(categoryId);
harness.render(useCategoryZoom);
harness.commit();
}
beforeEach(() => {
harness.clear();
period.from = undefined;
period.to = undefined;
// Never settles: only the arguments are read, and a late dispatch would
// land in the next test's mount.
services.getCategoryZoom.mockReset().mockReturnValue(new Promise(() => {}));
});
describe("useCategoryZoom — the period's bounds reach the service (#479)", () => {
it("« Tout »: the zoom asks for no date bound", () => {
zoomOn(42);
expect(services.getCategoryZoom).toHaveBeenCalledTimes(1);
expect(services.getCategoryZoom).toHaveBeenCalledWith(42, undefined, undefined, true);
});
it("a dated period passes its bounds as they are", () => {
period.from = "2026-01-01";
period.to = "2026-04-14";
zoomOn(42);
expect(services.getCategoryZoom).toHaveBeenCalledWith(42, "2026-01-01", "2026-04-14", true);
});
});