Files
kandev/apps/web/hooks/use-tasks.test.ts
T
José Almeida 21202ef437 fix(web): mobile task switcher sheet skeleton while snapshot loads (#860)
* fix(web): show skeleton in mobile task switcher sheet while snapshot loads

When the user opens the mobile task switcher inside a task page before the
workflow snapshot has hydrated kanban, the sheet rendered "No tasks yet."
instead of a loading state — and stayed that way if the fetch silently
failed.

useWorkflowSnapshot now drives kanban.isLoading, useTasks exposes it as
{ tasks, isLoading }, and the sheet wires that into TaskSwitcher's existing
skeleton. Fetch errors are surfaced via console.warn so an indefinite empty
list isn't silent anymore.

* fix(web): guard snapshot finally on cancel and drop redundant subscription

Skip the workflow-snapshot fetch's finally block when the effect was superseded by a newer workflowId — otherwise the old fetch's settle would clear the loading flag the new effect just set, briefly flashing the empty-state UI mid-switch.

Also drop the redundant kanbanIsLoading subscription in useSheetData (useTasks already encapsulates it) and make the setState mock in use-tasks.test.ts actually mutate state so useWorkflowSnapshot's side effects are exercised by the test.

* fix(web): scope snapshot finally clear to the effect that raised the flag

If kanban already matched the requested workflowId, the effect skipped raising isLoading=true — but its finally still cleared it on settle, collapsing a skeleton a concurrent caller (e.g. workspace switch) had set independently. Track whether this effect raised the flag and only clear in that case.

Also collapse the multi-line useTasks comment per CLAUDE.md style.

* fix(web): suppress superseded snapshot fetch warnings

Skip the catch-handler console.warn when the effect has been cancelled — a stale fetch shouldn't surface noise after a newer workflow has taken over.

* test(web): flush microtasks explicitly in race test

waitFor on a never-true assertion resolves immediately, hiding the intent. Use explicit Promise.resolve() flushes to land the superseded fetch's .then/.finally chain before asserting.

* style(web): collapse multi-line comments per CLAUDE.md
2026-05-10 17:02:40 +01:00

122 lines
4.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderHook } from "@testing-library/react";
const mockHydrate = vi.fn();
const mockSetState = vi.fn();
const mockFetchWorkflowSnapshot = vi.fn();
type Task = { id: string; title: string };
type MockState = {
connection: { status: string };
kanban: { workflowId: string | null; tasks: Task[]; steps: unknown[]; isLoading?: boolean };
hydrate: typeof mockHydrate;
};
let mockState: MockState = {
connection: { status: "connected" },
kanban: { workflowId: null, tasks: [], steps: [] },
hydrate: mockHydrate,
};
vi.mock("@/components/state-provider", () => ({
useAppStore: (selector: (s: MockState) => unknown) => selector(mockState),
useAppStoreApi: () => ({
getState: () => mockState,
setState: (updater: (s: MockState) => MockState) => {
// Apply the updater so useWorkflowSnapshot's isLoading transitions are
// visible to subsequent reads in the same render cycle. Without this
// the mock would silently swallow side effects.
mockState = updater(mockState);
mockSetState(mockState);
},
}),
}));
vi.mock("@/lib/api", () => ({
fetchWorkflowSnapshot: (...args: unknown[]) => mockFetchWorkflowSnapshot(...args),
}));
vi.mock("@/lib/ssr/mapper", () => ({
snapshotToState: (snapshot: unknown) => ({ snapshot }),
}));
import { useTasks } from "./use-tasks";
function setMockState(patch: Partial<MockState["kanban"]> & { workflowId?: string | null }) {
mockState = {
...mockState,
kanban: { ...mockState.kanban, ...patch },
};
}
describe("useTasks — loading and matching", () => {
beforeEach(() => {
vi.clearAllMocks();
// Never resolves so we observe pre-resolution loading state.
mockFetchWorkflowSnapshot.mockReturnValue(new Promise(() => {}));
mockState = {
connection: { status: "connected" },
kanban: { workflowId: null, tasks: [], steps: [], isLoading: false },
hydrate: mockHydrate,
};
});
it("returns empty list and not-loading when workflowId is null", () => {
const { result } = renderHook(() => useTasks(null));
expect(result.current.tasks).toEqual([]);
expect(result.current.isLoading).toBe(false);
});
it("reports loading once useWorkflowSnapshot has flipped kanban.isLoading", () => {
setMockState({ workflowId: null, isLoading: true });
const { result } = renderHook(() => useTasks("wf-1"));
expect(result.current.tasks).toEqual([]);
expect(result.current.isLoading).toBe(true);
});
it("does not report loading when fetch has settled but workflowId mismatches", () => {
// Simulates a failed snapshot fetch — `kanban.isLoading` is back to false
// and `kanban.workflowId` never advanced to the requested id. Caller
// shows the empty-state UI rather than an infinite skeleton.
setMockState({ workflowId: null, isLoading: false });
const { result } = renderHook(() => useTasks("wf-1"));
expect(result.current.tasks).toEqual([]);
expect(result.current.isLoading).toBe(false);
});
it("returns tasks and not-loading once kanban.workflowId matches", () => {
setMockState({
workflowId: "wf-1",
tasks: [
{ id: "t1", title: "One" },
{ id: "t2", title: "Two" },
],
isLoading: false,
});
const { result } = renderHook(() => useTasks("wf-1"));
expect(result.current.tasks).toHaveLength(2);
expect(result.current.isLoading).toBe(false);
});
it("filters out tasks that belong to a different workflow snapshot", () => {
setMockState({
workflowId: "wf-other",
tasks: [{ id: "t1", title: "One" }],
isLoading: false,
});
const { result } = renderHook(() => useTasks("wf-1"));
expect(result.current.tasks).toEqual([]);
expect(result.current.isLoading).toBe(false);
});
it("respects kanban.isLoading even when workflowId already matches", () => {
setMockState({
workflowId: "wf-1",
tasks: [{ id: "t1", title: "One" }],
isLoading: true,
});
const { result } = renderHook(() => useTasks("wf-1"));
expect(result.current.isLoading).toBe(true);
});
});