test(canvas): add EmptyState component tests (22 cases)
Some checks failed
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 24s
Harness Replays / detect-changes (pull_request) Successful in 24s
Lint curl status-code capture / Scan workflows for curl status-capture pollution (pull_request) Successful in 19s
publish-runtime-autobump / bump-and-tag (pull_request) Has been skipped
CI / Detect changes (pull_request) Successful in 1m16s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 23s
E2E Staging Canvas (Playwright) / detect-changes (pull_request) Successful in 1m23s
E2E API Smoke Test / detect-changes (pull_request) Successful in 1m28s
Handlers Postgres Integration / detect-changes (pull_request) Successful in 1m23s
qa-review / approved (pull_request) Failing after 24s
publish-runtime-autobump / pr-validate (pull_request) Successful in 1m4s
gate-check-v3 / gate-check (pull_request) Failing after 42s
security-review / approved (pull_request) Failing after 26s
sop-tier-check / tier-check (pull_request) Successful in 20s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 1m11s
CI / Platform (Go) (pull_request) Successful in 12s
Harness Replays / Harness Replays (pull_request) Successful in 15s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 9s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 12s
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Successful in 11s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 3m10s
CI / Python Lint & Test (pull_request) Successful in 7m50s
E2E Staging Canvas (Playwright) / Canvas tabs E2E (pull_request) Successful in 9m6s
CI / Canvas (Next.js) (pull_request) Successful in 13m18s
CI / Canvas Deploy Reminder (pull_request) Has been skipped

Adds 22-case coverage for EmptyState — the full-canvas welcome card:

- Loading state (GET /templates pending)
- Template grid renders with correct name, tier badge, description, skill count, model
- Template button calls deploy on click
- "Deploying..." label on the deploying template button
- Buttons disabled while any deploy is in-flight
- "Create blank" button POSTs /workspaces with correct payload
- "Creating..." label while POST is pending
- selectNode + setPanelTab("chat") called after 500ms on success
- Error banner with role=alert on POST failure
- Fetch failure / empty templates → only "create blank" button shown

Uses vi.hoisted + vi.mock to fully isolate api.get, api.post, useTemplateDeploy,
useCanvasStore, and all child components.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Molecule AI · app-fe 2026-05-11 18:46:48 +00:00
parent 4516cc464c
commit abf485b3aa
2 changed files with 409 additions and 30 deletions

View File

@ -5,10 +5,9 @@
* Covers: renders nothing when no approvals, polls /approvals/pending,
* shows approval cards, approve/deny decisions, toast notifications.
*
* Note: does NOT mock @/lib/api uses vi.spyOn on the real module.
* vi.restoreAllMocks() is omitted from afterEach so queued mock values
* (set up via mockResolvedValueOnce in beforeEach) are preserved for the
* component's useEffect to consume.
* Uses vi.hoisted + vi.mock to create self-contained api mocks, avoiding
* cross-file module-cache pollution from other test files that use
* vi.mock("@/lib/api") (e.g. AuditTrailPanel which only provides get, no post).
*/
import React from "react";
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
@ -21,7 +20,22 @@ vi.mock("@/components/Toaster", () => ({
showToast: vi.fn(),
}));
// ─── Helpers ──────────────────────────────────────────────────────────────────
// ─── Mock api (hoisted before module load) ────────────────────────────────────
// vi.hoisted runs in the same hoisting phase as vi.mock factories, so _mockGet
// and _mockPost are the same function instances the factory returns.
const { _mockGet, _mockPost } = vi.hoisted(() => ({
_mockGet: vi.fn(),
_mockPost: vi.fn(),
}));
vi.mock("@/lib/api", () => ({
api: {
get: _mockGet,
post: _mockPost,
},
}));
// ─── Helpers ───────────────────────────────────────────────────────────────────
const pendingApproval = (id = "a1", workspaceId = "ws-1"): {
id: string;
@ -41,22 +55,18 @@ const pendingApproval = (id = "a1", workspaceId = "ws-1"): {
created_at: "2026-05-10T10:00:00Z",
});
// Shared spy references so individual tests can reset or reject the POST mock
// without needing to call spyOn again (which would create a duplicate spy).
let mockGet: ReturnType<typeof vi.spyOn>;
let mockPost: ReturnType<typeof vi.spyOn>;
// ─── Tests ────────────────────────────────────────────────────────────────────
// ─── Tests ─────────────────────────────────────────────────────────────────────
describe("ApprovalBanner — empty state", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(api, "get").mockResolvedValueOnce([]);
_mockGet.mockReset().mockResolvedValue([]);
});
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("renders nothing when there are no pending approvals", async () => {
@ -76,7 +86,7 @@ describe("ApprovalBanner — empty state", () => {
describe("ApprovalBanner — renders approval cards", () => {
beforeEach(() => {
vi.useFakeTimers();
mockGet = vi.spyOn(api, "get").mockResolvedValueOnce([
_mockGet.mockReset().mockResolvedValue([
pendingApproval("a1"),
pendingApproval("a2", "ws-2"),
]);
@ -85,32 +95,29 @@ describe("ApprovalBanner — renders approval cards", () => {
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("renders an alert card for each pending approval", async () => {
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
const alerts = screen.getAllByRole("alert");
expect(alerts).toHaveLength(2);
mockGet.mockRestore();
expect(screen.getAllByRole("alert")).toHaveLength(2);
});
it("displays the workspace name and action text", async () => {
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
const nameEls = screen.getAllByText(/test workspace needs approval/i);
expect(nameEls).toHaveLength(2);
expect(screen.getAllByText(/test workspace needs approval/i)).toHaveLength(2);
});
it("displays the reason when present", async () => {
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
const reasons = screen.getAllByText(/requires human approval/i);
expect(reasons).toHaveLength(2);
expect(screen.getAllByText(/requires human approval/i)).toHaveLength(2);
});
it("omits the reason div when reason is null", async () => {
vi.spyOn(api, "get").mockResolvedValueOnce([{
_mockGet.mockReset().mockResolvedValue([{
...pendingApproval("a1"),
reason: null,
}]);
@ -132,21 +139,21 @@ describe("ApprovalBanner — renders approval cards", () => {
it("has aria-live=assertive on the alert container", async () => {
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
const alert = screen.getAllByRole("alert")[0];
expect(alert.getAttribute("aria-live")).toBe("assertive");
expect(screen.getAllByRole("alert")[0].getAttribute("aria-live")).toBe("assertive");
});
});
describe("ApprovalBanner — decisions", () => {
beforeEach(() => {
vi.useFakeTimers();
mockGet = vi.spyOn(api, "get").mockResolvedValueOnce([pendingApproval("a1")]);
mockPost = vi.spyOn(api, "post").mockResolvedValue({});
_mockGet.mockReset().mockResolvedValue([pendingApproval("a1")]);
_mockPost.mockReset().mockResolvedValue({});
});
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("calls POST /workspaces/:id/approvals/:id/decide on Approve click", async () => {
@ -197,7 +204,7 @@ describe("ApprovalBanner — decisions", () => {
});
it("shows an error toast when POST fails", async () => {
mockPost.mockReset().mockRejectedValue(new Error("Network error"));
_mockPost.mockReset().mockRejectedValue(new Error("Network error"));
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
fireEvent.click(screen.getAllByRole("button", { name: /approve/i })[0]);
@ -209,9 +216,10 @@ describe("ApprovalBanner — decisions", () => {
});
it("keeps the card visible when the POST fails", async () => {
// Reset the post mock before rejecting so the beforeEach's resolved value
// is gone and we get a clean rejection instead of a resolved→rejected queue.
mockPost.mockReset().mockRejectedValue(new Error("Network error"));
// mockReset + mockRejectedValue is safe here because _mockPost is a vi.fn()
// mock created in the vi.hoisted factory — it has no underlying original
// function to fall back to, so mockReset cannot cause the real fetch to fire.
_mockPost.mockReset().mockRejectedValue(new Error("Network error"));
render(<ApprovalBanner />);
await act(async () => { await vi.runOnlyPendingTimersAsync(); });
fireEvent.click(screen.getAllByRole("button", { name: /approve/i })[0]);
@ -223,12 +231,13 @@ describe("ApprovalBanner — decisions", () => {
describe("ApprovalBanner — handles empty list from server", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.spyOn(api, "get").mockResolvedValueOnce([]);
_mockGet.mockReset().mockResolvedValue([]);
});
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("shows nothing when the API returns an empty array on first poll", async () => {

View File

@ -0,0 +1,370 @@
// @vitest-environment jsdom
/**
* Tests for EmptyState the full-canvas welcome card shown on first load.
*
* Covers:
* - Loading state (GET /templates in flight)
* - Fetch failure empty template grid (templates = [])
* - Template grid renders with correct content
* - Template button disabled while deploying
* - "Deploying..." label on the button being deployed
* - "Create blank" button POSTs /workspaces
* - "Creating..." label while blank workspace is being created
* - Blank create error shows error banner
* - Error banner has role="alert"
* - All buttons disabled while any deploy is in-flight
* - handleDeployed fires after 500ms delay
*
* Uses vi.hoisted + vi.mock to fully isolate the api module, matching
* the pattern established in ApprovalBanner, MemoryTab, and ScheduleTab tests.
*/
import React from "react";
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { EmptyState } from "../EmptyState";
// ─── Hoisted mock refs ─────────────────────────────────────────────────────────
// vi.hoisted runs in the same hoisting phase as vi.mock factories, so all refs
// are available both to the factory and to test bodies.
const { mockApiGet, mockApiPost } = vi.hoisted(() => ({
mockApiGet: vi.fn<(args: unknown[]) => Promise<unknown>>(),
mockApiPost: vi.fn<(args: unknown[]) => Promise<{ id: string }>>(),
}));
// Mutable deploy state — object reference is const; properties can be mutated.
const _deploy = vi.hoisted(() => ({
deployFn: vi.fn(),
deploying: undefined as string | undefined,
error: undefined as string | undefined,
modal: null as React.ReactNode,
}));
const { mockSelectNode, mockSetPanelTab } = vi.hoisted(() => ({
mockSelectNode: vi.fn(),
mockSetPanelTab: vi.fn(),
}));
// ─── Mocks ────────────────────────────────────────────────────────────────────
vi.mock("@/lib/api", () => ({
api: {
get: mockApiGet,
post: mockApiPost,
},
}));
vi.mock("@/hooks/useTemplateDeploy", () => ({
useTemplateDeploy: () => ({
deploy: _deploy.deployFn,
deploying: _deploy.deploying,
error: _deploy.error,
modal: _deploy.modal,
}),
}));
vi.mock("@/store/canvas", () => ({
useCanvasStore: Object.assign(
vi.fn((selector: (s: { getState: () => { selectNode: typeof mockSelectNode; setPanelTab: typeof mockSetPanelTab } }) => unknown) =>
selector({
getState: () => ({
selectNode: mockSelectNode,
setPanelTab: mockSetPanelTab,
}),
})
),
{ getState: () => ({ selectNode: mockSelectNode, setPanelTab: mockSetPanelTab }) }
),
}));
vi.mock("../TemplatePalette", () => ({
OrgTemplatesSection: () => null,
}));
vi.mock("../Spinner", () => ({
Spinner: () => <span data-testid="spinner"></span>,
}));
vi.mock("@/lib/design-tokens", () => ({
TIER_CONFIG: {
1: { label: "T1", color: "text-ink-mid bg-surface-card border border-line", border: "text-ink-mid border-line" },
2: { label: "T2", color: "text-white bg-accent border border-accent-strong", border: "text-accent border-accent" },
3: { label: "T3", color: "text-white bg-violet-600 border border-violet-700", border: "text-violet-600 border-violet-500" },
4: { label: "T4", color: "text-white bg-warm border border-warm", border: "text-warm border-warm" },
},
}));
// ─── Fixtures ─────────────────────────────────────────────────────────────────
const TEMPLATE = {
id: "tpl-1",
name: "Claude Code Agent",
description: "A general-purpose coding assistant",
tier: 2,
skill_count: 3,
model: "claude-opus-4-5",
};
function template(overrides: Partial<typeof TEMPLATE> = {}): typeof TEMPLATE {
return { ...TEMPLATE, ...overrides };
}
// ─── Helpers ───────────────────────────────────────────────────────────────────
function renderEmpty() {
return render(<EmptyState />);
}
// Flush React state + microtasks after an act boundary.
async function flush() {
await act(async () => { await Promise.resolve(); });
}
// Reset deploy state to defaults before each test.
function resetDeployState() {
_deploy.deployFn.mockReset();
_deploy.deploying = undefined;
_deploy.error = undefined;
_deploy.modal = null;
}
// ─── Tests ─────────────────────────────────────────────────────────────────────
describe("EmptyState — loading", () => {
beforeEach(() => {
mockApiGet.mockReset().mockImplementation(
() => new Promise(() => {}) // never resolves
);
});
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});
it("shows loading state while GET /templates is pending", async () => {
renderEmpty();
await flush();
expect(screen.getByTestId("spinner")).toBeTruthy();
expect(screen.getByText("Loading templates...")).toBeTruthy();
});
// "create blank" is rendered outside the loading/template-grid conditional,
// so it is always visible — adjust expectation accordingly.
it("renders 'create blank' button during loading", async () => {
renderEmpty();
await flush();
expect(screen.getByRole("button", { name: "+ Create blank workspace" })).toBeTruthy();
});
it("does not render template buttons while loading", async () => {
renderEmpty();
await flush();
expect(screen.queryByText("Claude Code Agent")).toBeNull();
});
});
describe("EmptyState — templates", () => {
beforeEach(() => {
mockApiGet.mockReset().mockResolvedValue([template()]);
resetDeployState();
});
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});
it("renders the welcome heading", async () => {
renderEmpty();
await flush();
expect(screen.getByText("Deploy your first agent")).toBeTruthy();
});
it("renders template buttons with name and description", async () => {
renderEmpty();
await flush();
expect(screen.getByText("Claude Code Agent")).toBeTruthy();
expect(screen.getByText("A general-purpose coding assistant")).toBeTruthy();
});
it("renders tier badge and skill count", async () => {
renderEmpty();
await flush();
expect(screen.getByText("T2")).toBeTruthy();
// skill_count renders as "3 skills · <model>"
expect(screen.getByText(/^3 skills/)).toBeTruthy();
});
it("renders model name when present", async () => {
renderEmpty();
await flush();
expect(screen.getByText(/claude-opus/i)).toBeTruthy();
});
it("calls deploy with the template on click", async () => {
renderEmpty();
await flush();
fireEvent.click(screen.getByText("Claude Code Agent"));
expect(_deploy.deployFn).toHaveBeenCalledWith(template());
});
it("shows 'Deploying...' on the button of the template being deployed", async () => {
_deploy.deploying = "tpl-1";
renderEmpty();
await flush();
expect(screen.getByText("Deploying...")).toBeTruthy();
});
it("disables the template button of the deploying template", async () => {
_deploy.deploying = "tpl-1";
renderEmpty();
await flush();
const btn = screen.getByText("Deploying...").closest("button") as HTMLButtonElement;
expect(btn.disabled).toBe(true);
});
it("disables 'create blank' while a template is deploying", async () => {
_deploy.deploying = "tpl-1";
renderEmpty();
await flush();
expect(screen.getByRole("button", { name: "+ Create blank workspace" }).disabled).toBe(true);
});
});
describe("EmptyState — fetch failure / empty templates", () => {
beforeEach(() => {
mockApiGet.mockReset().mockResolvedValue([]);
resetDeployState();
});
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});
it("does not render template grid when GET /templates returns []", async () => {
renderEmpty();
await flush();
expect(screen.queryByText("Claude Code Agent")).toBeNull();
});
it("renders 'create blank' button when templates list is empty", async () => {
renderEmpty();
await flush();
expect(screen.getByRole("button", { name: "+ Create blank workspace" })).toBeTruthy();
});
it("does not render template grid when GET /templates rejects", async () => {
mockApiGet.mockReset().mockRejectedValue(new Error("Network failure"));
renderEmpty();
await flush();
expect(screen.queryByText("Claude Code Agent")).toBeNull();
});
});
describe("EmptyState — create blank", () => {
beforeEach(() => {
mockApiGet.mockReset().mockResolvedValue([template()]);
mockApiPost.mockReset().mockResolvedValue({ id: "ws-new" });
resetDeployState();
vi.useFakeTimers();
});
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("calls POST /workspaces on 'create blank' click", async () => {
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); });
expect(mockApiPost).toHaveBeenCalledWith(
"/workspaces",
expect.objectContaining({ name: "My First Agent" })
);
});
it("shows 'Creating...' while blank workspace POST is pending", async () => {
mockApiPost.mockReset().mockImplementation(
() => new Promise(() => {}) // never resolves
);
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); });
expect(screen.getByRole("button", { name: "Creating..." })).toBeTruthy();
});
it("calls selectNode + setPanelTab after 500ms on successful create", async () => {
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); }); // flush POST
await act(async () => { vi.advanceTimersByTime(500); });
expect(mockSelectNode).toHaveBeenCalledWith("ws-new");
expect(mockSetPanelTab).toHaveBeenCalledWith("chat");
});
it("disables template buttons while creating blank workspace", async () => {
mockApiPost.mockReset().mockImplementation(
() => new Promise(() => {}) // never resolves
);
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); });
expect((screen.getByText("Claude Code Agent").closest("button") as HTMLButtonElement).disabled).toBe(true);
});
it("shows error banner when POST /workspaces fails", async () => {
mockApiPost.mockReset().mockRejectedValue(new Error("Server error"));
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); });
expect(screen.getByRole("alert")).toBeTruthy();
expect(screen.getByText(/server error/i)).toBeTruthy();
});
it("clears 'Creating...' and shows button again after POST failure", async () => {
mockApiPost.mockReset().mockRejectedValue(new Error("Server error"));
renderEmpty();
await flush();
fireEvent.click(screen.getByRole("button", { name: "+ Create blank workspace" }));
await act(async () => { await Promise.resolve(); });
// After rejection, blankCreating = false → button reverts to default label
expect(screen.getByRole("button", { name: "+ Create blank workspace" })).toBeTruthy();
});
});
describe("EmptyState — error banner", () => {
beforeEach(() => {
mockApiGet.mockReset().mockResolvedValue([template()]);
resetDeployState();
vi.useFakeTimers();
});
afterEach(() => {
cleanup();
vi.useRealTimers();
vi.restoreAllMocks();
});
it("has role=alert on the error banner", async () => {
_deploy.error = "Template deploy failed";
renderEmpty();
await flush();
const alert = screen.getByRole("alert");
expect(alert).toBeTruthy();
expect(alert.textContent).toContain("Template deploy failed");
});
it("does not show error banner when no errors", async () => {
renderEmpty();
await flush();
expect(screen.queryByRole("alert")).toBeNull();
});
});