D1 of Max: showsEmptyState of Transactions reads totalInDb === 0 only, like Budget. On an empty profile the card comes up as soon as the count is loaded, instead of the filter bar, the totals and the loading line until the first page is back. A null count (not loaded, or failed) keeps the regular layout. The mount reads of useTransactions and useBudget run under Promise.allSettled: each read that succeeds is dispatched, the first failure becomes the page's error, and a failed count leaves totalInDb null instead of emptying the category, source and account filters. Tests: the reducer test of the loading page rewritten for D1, the page's empty-profile case rendered (card in place of the filter bar, table and pagination), and both hooks' mount reads run through the shared harness. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.8 KiB
TypeScript
97 lines
3.8 KiB
TypeScript
// useBudget — the reads of the mount stand alone (#384).
|
|
//
|
|
// 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 year's budget, then the accounts and the count). The
|
|
// services and the reports period are mocked. What is pinned: a failed count
|
|
// no longer drops the accounts of the filter, leaves `totalInDb` unknown (the
|
|
// grid, never the "import first" card), and the first failure, in the order
|
|
// accounts, count, is the page's error.
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
vi.mock("react", async (orig) => (await import("../test/hookHarness")).fakeReact(await orig()));
|
|
const services = vi.hoisted(() => ({
|
|
getAllImportSources: vi.fn(),
|
|
getTransactionCount: vi.fn(),
|
|
}));
|
|
vi.mock("../services/transactionService", () => services);
|
|
vi.mock("../services/budgetService", () => ({
|
|
getAllActiveCategories: vi.fn(async () => []),
|
|
getBudgetEntriesForYear: vi.fn(async () => []),
|
|
getActualTotalsForYear: vi.fn(async () => []),
|
|
getAllTemplates: vi.fn(async () => []),
|
|
upsertBudgetEntry: vi.fn(),
|
|
upsertBudgetEntriesForYear: vi.fn(),
|
|
saveAsTemplate: vi.fn(),
|
|
applyTemplate: vi.fn(),
|
|
deleteTemplate: vi.fn(),
|
|
}));
|
|
// One array for every render: a new one would read as a changed filter.
|
|
const { NO_ACCOUNT } = vi.hoisted(() => ({ NO_ACCOUNT: [] as number[] }));
|
|
vi.mock("./useReportsPeriod", () => ({
|
|
useReportsPeriod: () => ({ accountIds: NO_ACCOUNT, setAccountIds: () => undefined }),
|
|
}));
|
|
|
|
import { hookHarness } from "../test/hookHarness";
|
|
import { useBudget, showsEmptyState } from "./useBudget";
|
|
import type { ImportSource } from "../shared/types";
|
|
|
|
const harness = hookHarness();
|
|
|
|
const ACCOUNTS = [{ id: 7, name: "Desjardins" }] as ImportSource[];
|
|
|
|
/** Lets the mount reads settle. */
|
|
const flush = async () => {
|
|
for (let i = 0; i < 10; i++) await Promise.resolve();
|
|
};
|
|
|
|
/** Mounts the hook, runs its effects, lets them settle, and renders again. */
|
|
async function mount() {
|
|
harness.render(useBudget);
|
|
harness.commit();
|
|
await flush();
|
|
return harness.render(useBudget).state;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
harness.clear();
|
|
services.getAllImportSources.mockReset().mockResolvedValue(ACCOUNTS);
|
|
services.getTransactionCount.mockReset().mockResolvedValue(0);
|
|
});
|
|
|
|
describe("useBudget — the reads of the mount (#384)", () => {
|
|
it("both succeed: the accounts and the count, no error", async () => {
|
|
const state = await mount();
|
|
expect(state.accounts).toBe(ACCOUNTS);
|
|
expect(state.totalInDb).toBe(0);
|
|
expect(state.error).toBeNull();
|
|
expect(showsEmptyState(state)).toBe(true);
|
|
});
|
|
|
|
it("a failed count still loads the accounts, and leaves the count unknown", async () => {
|
|
services.getTransactionCount.mockRejectedValue(new Error("count failed"));
|
|
const state = await mount();
|
|
expect(state.accounts).toBe(ACCOUNTS);
|
|
expect(state.totalInDb).toBeNull();
|
|
expect(state.error).toBe("count failed");
|
|
// Unknown count: the grid, never the "import first" card.
|
|
expect(showsEmptyState(state)).toBe(false);
|
|
});
|
|
|
|
it("failed accounts still load the count", async () => {
|
|
services.getAllImportSources.mockRejectedValue(new Error("accounts failed"));
|
|
services.getTransactionCount.mockResolvedValue(41);
|
|
const state = await mount();
|
|
expect(state.accounts).toEqual([]);
|
|
expect(state.totalInDb).toBe(41);
|
|
expect(state.error).toBe("accounts failed");
|
|
});
|
|
|
|
it("both fail: the accounts' failure, the first one, is the error", async () => {
|
|
services.getAllImportSources.mockRejectedValue(new Error("accounts failed"));
|
|
services.getTransactionCount.mockRejectedValue(new Error("count failed"));
|
|
const state = await mount();
|
|
expect(state.totalInDb).toBeNull();
|
|
expect(state.error).toBe("accounts failed");
|
|
});
|
|
});
|