Files
Triple-C/app/src/components/projects/AddProjectDialog.test.tsx
T
shadowdaoandClaude Opus 5 1eb91a35eb
Secret Scan / scan (push) Successful in 10s
Build App (Preview) / compute-version (pull_request) Successful in 6s
Secret Scan / scan (pull_request) Successful in 10s
Build App (Preview) / create-release (pull_request) Successful in 3s
Build App (Preview) / build-macos (pull_request) Successful in 2m41s
Build App (Preview) / build-windows (pull_request) Successful in 4m56s
Build App (Preview) / build-linux (pull_request) Successful in 6m47s
Build App (Preview) / prune-previews (pull_request) Successful in 1s
Give the terminal and Add Project buttons a reason a screen reader can hear
Both were the defect the new hook exists for. The sidebar's Claude terminal
button is disabled whenever the container is not running and never said so —
its `title` names the action, so the precondition appeared nowhere in the
accessibility tree at all. Add Project's submit button is disabled while an
add is in flight, and its only signal is the label swapping to "Adding…" on
an element a screen reader can no longer reach.

The submit button needs a second guard the hook cannot supply: Enter inside
a text field submits a form without touching the submit button, so
`handleSubmit` now returns early while loading. Without it, swapping
`disabled` for `aria-disabled` would have turned an accessibility fix into a
double-submit bug.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011YPqHpjV4EL6RNEwrRKqQm
2026-09-02 09:08:57 -07:00

119 lines
3.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent, waitFor, act } from "@testing-library/react";
import AddProjectDialog from "./AddProjectDialog";
const add = vi.fn();
vi.mock("../../hooks/useProjects", () => ({
useProjects: () => ({ add }),
}));
vi.mock("@tauri-apps/plugin-dialog", () => ({
open: vi.fn(async () => null),
}));
/** A promise whose resolution this test controls, so `loading` can be held open. */
function deferred() {
let resolve!: (v: unknown) => void;
const promise = new Promise((r) => {
resolve = r;
});
return { promise, resolve };
}
function fillValidForm() {
fireEvent.change(screen.getByLabelText("Project name"), {
target: { value: "my-project" },
});
fireEvent.change(screen.getByLabelText("Folder 1 host path"), {
target: { value: "/home/user/my-project" },
});
}
function submitButton() {
return screen.getByRole("button", { name: /Add Project|Adding/ });
}
describe("AddProjectDialog", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("adds the project with the name and folder entered", async () => {
add.mockResolvedValue({ id: "p1" });
const onClose = vi.fn();
render(<AddProjectDialog onClose={onClose} />);
fillValidForm();
fireEvent.click(submitButton());
await waitFor(() =>
expect(add).toHaveBeenCalledWith("my-project", [
{ host_path: "/home/user/my-project", mount_name: "my-project" },
]),
);
await waitFor(() => expect(onClose).toHaveBeenCalled());
});
it("keeps the submit button announced, and explains why, while adding", async () => {
const { promise, resolve } = deferred();
add.mockReturnValue(promise);
render(<AddProjectDialog onClose={vi.fn()} />);
fillValidForm();
fireEvent.click(submitButton());
// Native `disabled` would remove the button from the accessibility tree
// exactly when it has something to say.
await waitFor(() =>
expect(submitButton()).toHaveAttribute("aria-disabled", "true"),
);
expect(submitButton()).not.toBeDisabled();
expect(submitButton()).toHaveAccessibleDescription(/being added/i);
await act(async () => resolve({ id: "p1" }));
});
it("ignores clicks and Enter/Space on the submit button while adding", async () => {
const { promise, resolve } = deferred();
add.mockReturnValue(promise);
render(<AddProjectDialog onClose={vi.fn()} />);
fillValidForm();
fireEvent.click(submitButton());
await waitFor(() =>
expect(submitButton()).toHaveAttribute("aria-disabled", "true"),
);
fireEvent.click(submitButton());
fireEvent.keyDown(submitButton(), { key: "Enter" });
fireEvent.keyDown(submitButton(), { key: " " });
expect(add).toHaveBeenCalledTimes(1);
await act(async () => resolve({ id: "p1" }));
});
it("ignores a form submit raised from elsewhere while adding", async () => {
const { promise, resolve } = deferred();
add.mockReturnValue(promise);
render(<AddProjectDialog onClose={vi.fn()} />);
fillValidForm();
fireEvent.click(submitButton());
await waitFor(() =>
expect(submitButton()).toHaveAttribute("aria-disabled", "true"),
);
// Enter in a text field submits a form regardless of the submit button's
// state, so the handler has to guard itself too.
// Modal portals to document.body, so the form is not under `container`.
const form = document.querySelector("form");
expect(form).not.toBeNull();
fireEvent.submit(form!);
expect(add).toHaveBeenCalledTimes(1);
await act(async () => resolve({ id: "p1" }));
});
it("leaves the submit button plainly available when idle", () => {
render(<AddProjectDialog onClose={vi.fn()} />);
expect(submitButton()).not.toHaveAttribute("aria-disabled");
expect(submitButton()).toHaveAccessibleDescription("");
});
});